Commit 20483c23 authored by Leonard Hecker's avatar Leonard Hecker

Added end-to-end test case for #1292

parent 9f9ad21a
...@@ -123,7 +123,7 @@ func TestWebSocketReverseProxyNonHijackerPanic(t *testing.T) { ...@@ -123,7 +123,7 @@ func TestWebSocketReverseProxyNonHijackerPanic(t *testing.T) {
defer wsNop.Close() defer wsNop.Close()
// Get proxy to use for the test // Get proxy to use for the test
p := newWebSocketTestProxy(wsNop.URL) p := newWebSocketTestProxy(wsNop.URL, false)
// Create client request // Create client request
r := httptest.NewRequest("GET", "/", nil) r := httptest.NewRequest("GET", "/", nil)
...@@ -148,7 +148,7 @@ func TestWebSocketReverseProxyServeHTTPHandler(t *testing.T) { ...@@ -148,7 +148,7 @@ func TestWebSocketReverseProxyServeHTTPHandler(t *testing.T) {
defer wsNop.Close() defer wsNop.Close()
// Get proxy to use for the test // Get proxy to use for the test
p := newWebSocketTestProxy(wsNop.URL) p := newWebSocketTestProxy(wsNop.URL, false)
// Create client request // Create client request
r := httptest.NewRequest("GET", "/", nil) r := httptest.NewRequest("GET", "/", nil)
...@@ -189,7 +189,7 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) { ...@@ -189,7 +189,7 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) {
defer wsEcho.Close() defer wsEcho.Close()
// Get proxy to use for the test // Get proxy to use for the test
p := newWebSocketTestProxy(wsEcho.URL) p := newWebSocketTestProxy(wsEcho.URL, false)
// This is a full end-end test, so the proxy handler // This is a full end-end test, so the proxy handler
// has to be part of a server listening on a port. Our // has to be part of a server listening on a port. Our
...@@ -228,6 +228,52 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) { ...@@ -228,6 +228,52 @@ func TestWebSocketReverseProxyFromWSClient(t *testing.T) {
} }
} }
func TestWebSocketReverseProxyFromWSSClient(t *testing.T) {
wsEcho := newTLSServer(websocket.Handler(func(ws *websocket.Conn) {
io.Copy(ws, ws)
}))
defer wsEcho.Close()
p := newWebSocketTestProxy(wsEcho.URL, true)
echoProxy := newTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p.ServeHTTP(w, r)
}))
defer echoProxy.Close()
// Set up WebSocket client
url := strings.Replace(echoProxy.URL, "https://", "wss://", 1)
wsCfg, err := websocket.NewConfig(url, echoProxy.URL)
if err != nil {
t.Fatal(err)
}
wsCfg.TlsConfig = &tls.Config{InsecureSkipVerify: true}
ws, err := websocket.DialConfig(wsCfg)
if err != nil {
t.Fatal(err)
}
defer ws.Close()
// Send test message
trialMsg := "Is it working?"
if sendErr := websocket.Message.Send(ws, trialMsg); sendErr != nil {
t.Fatal(sendErr)
}
// It should be echoed back to us
var actualMsg string
if rcvErr := websocket.Message.Receive(ws, &actualMsg); rcvErr != nil {
t.Fatal(rcvErr)
}
if actualMsg != trialMsg {
t.Errorf("Expected '%s' but got '%s' instead", trialMsg, actualMsg)
}
}
func TestUnixSocketProxy(t *testing.T) { func TestUnixSocketProxy(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return return
...@@ -264,7 +310,7 @@ func TestUnixSocketProxy(t *testing.T) { ...@@ -264,7 +310,7 @@ func TestUnixSocketProxy(t *testing.T) {
defer ts.Close() defer ts.Close()
url := strings.Replace(ts.URL, "http://", "unix:", 1) url := strings.Replace(ts.URL, "http://", "unix:", 1)
p := newWebSocketTestProxy(url) p := newWebSocketTestProxy(url, false)
echoProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { echoProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p.ServeHTTP(w, r) p.ServeHTTP(w, r)
...@@ -982,10 +1028,14 @@ func (u *fakeUpstream) GetTryInterval() time.Duration { return 250 * time. ...@@ -982,10 +1028,14 @@ func (u *fakeUpstream) GetTryInterval() time.Duration { return 250 * time.
// redirect to the specified backendAddr. The function // redirect to the specified backendAddr. The function
// also sets up the rules/environment for testing WebSocket // also sets up the rules/environment for testing WebSocket
// proxy. // proxy.
func newWebSocketTestProxy(backendAddr string) *Proxy { func newWebSocketTestProxy(backendAddr string, insecure bool) *Proxy {
return &Proxy{ return &Proxy{
Next: httpserver.EmptyNext, // prevents panic in some cases when test fails Next: httpserver.EmptyNext, // prevents panic in some cases when test fails
Upstreams: []Upstream{&fakeWsUpstream{name: backendAddr, without: ""}}, Upstreams: []Upstream{&fakeWsUpstream{
name: backendAddr,
without: "",
insecure: insecure,
}},
} }
} }
...@@ -997,8 +1047,9 @@ func newPrefixedWebSocketTestProxy(backendAddr string, prefix string) *Proxy { ...@@ -997,8 +1047,9 @@ func newPrefixedWebSocketTestProxy(backendAddr string, prefix string) *Proxy {
} }
type fakeWsUpstream struct { type fakeWsUpstream struct {
name string name string
without string without string
insecure bool
} }
func (u *fakeWsUpstream) From() string { func (u *fakeWsUpstream) From() string {
...@@ -1007,13 +1058,17 @@ func (u *fakeWsUpstream) From() string { ...@@ -1007,13 +1058,17 @@ func (u *fakeWsUpstream) From() string {
func (u *fakeWsUpstream) Select(r *http.Request) *UpstreamHost { func (u *fakeWsUpstream) Select(r *http.Request) *UpstreamHost {
uri, _ := url.Parse(u.name) uri, _ := url.Parse(u.name)
return &UpstreamHost{ host := &UpstreamHost{
Name: u.name, Name: u.name,
ReverseProxy: NewSingleHostReverseProxy(uri, u.without, http.DefaultMaxIdleConnsPerHost), ReverseProxy: NewSingleHostReverseProxy(uri, u.without, http.DefaultMaxIdleConnsPerHost),
UpstreamHeaders: http.Header{ UpstreamHeaders: http.Header{
"Connection": {"{>Connection}"}, "Connection": {"{>Connection}"},
"Upgrade": {"{>Upgrade}"}}, "Upgrade": {"{>Upgrade}"}},
} }
if u.insecure {
host.ReverseProxy.UseInsecureTransport()
}
return host
} }
func (u *fakeWsUpstream) AllowedPath(requestPath string) bool { return true } func (u *fakeWsUpstream) AllowedPath(requestPath string) bool { return true }
......
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