Commit 99d40e17 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Add integration test for cable backend

Tests a single backend setup and a separate cable backend setup
parent 509d9b4b
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"testing"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/config"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/testhelper"
)
const cablePath = "/-/cable"
func TestSingleBackend(t *testing.T) {
cableServerConns, cableBackendServer := startCableServer()
defer cableBackendServer.Close()
config := newUpstreamWithCableConfig(cableBackendServer.URL, "")
workhorse := startWorkhorseServerWithConfig(config)
defer workhorse.Close()
cableURL := websocketURL(workhorse.URL, cablePath)
client, _, err := dialWebsocket(cableURL, nil)
require.NoError(t, err)
defer client.Close()
server := (<-cableServerConns).conn
defer server.Close()
require.NoError(t, say(client, "hello"))
assertReadMessage(t, server, websocket.TextMessage, "hello")
require.NoError(t, say(server, "world"))
assertReadMessage(t, client, websocket.TextMessage, "world")
}
func TestSeparateCableBackend(t *testing.T) {
authBackendServer := testhelper.TestServerWithHandler(regexp.MustCompile(`.`), http.HandlerFunc(http.NotFound))
defer authBackendServer.Close()
cableServerConns, cableBackendServer := startCableServer()
defer cableBackendServer.Close()
config := newUpstreamWithCableConfig(authBackendServer.URL, cableBackendServer.URL)
workhorse := startWorkhorseServerWithConfig(config)
defer workhorse.Close()
cableURL := websocketURL(workhorse.URL, cablePath)
client, _, err := dialWebsocket(cableURL, nil)
require.NoError(t, err)
defer client.Close()
server := (<-cableServerConns).conn
defer server.Close()
require.NoError(t, say(client, "hello"))
assertReadMessage(t, server, websocket.TextMessage, "hello")
require.NoError(t, say(server, "world"))
assertReadMessage(t, client, websocket.TextMessage, "world")
}
func startCableServer() (chan connWithReq, *httptest.Server) {
upgrader := &websocket.Upgrader{}
connCh := make(chan connWithReq, 1)
server := testhelper.TestServerWithHandler(regexp.MustCompile(cablePath), webSocketHandler(upgrader, connCh))
return connCh, server
}
func newUpstreamWithCableConfig(authBackend string, cableBackend string) *config.Config {
var cableBackendURL *url.URL
if cableBackend != "" {
cableBackendURL = helper.URLMustParse(cableBackend)
}
return &config.Config{
Version: "123",
DocumentRoot: testDocumentRoot,
Backend: helper.URLMustParse(authBackend),
CableBackend: cableBackendURL,
}
}
......@@ -171,7 +171,13 @@ func startWebsocketServer(subprotocols ...string) (chan connWithReq, *httptest.S
upgrader := &websocket.Upgrader{Subprotocols: subprotocols}
connCh := make(chan connWithReq, 1)
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
server := httptest.NewTLSServer(webSocketHandler(upgrader, connCh))
return connCh, server
}
func webSocketHandler(upgrader *websocket.Upgrader, connCh chan connWithReq) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logEntry := log.WithFields(log.Fields{
"method": r.Method,
"url": r.URL,
......@@ -186,9 +192,7 @@ func startWebsocketServer(subprotocols ...string) (chan connWithReq, *httptest.S
}
connCh <- connWithReq{conn, r}
// The connection has been hijacked so it's OK to end here
}))
return connCh, server
})
}
func channelOkBody(remote *httptest.Server, header http.Header, subprotocols ...string) *api.Response {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment