(t *testing.T)
| 16 | ) |
| 17 | |
| 18 | func TestProxyWSUpstream(t *testing.T) { |
| 19 | wsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | switch r.RequestURI { |
| 21 | case "/ws", "/wss", "/insecure", "/strip", "/foo/bar/baz", "/new/world": |
| 22 | websocket.Handler(wsEchoHandler).ServeHTTP(w, r) |
| 23 | default: |
| 24 | w.WriteHeader(404) |
| 25 | } |
| 26 | })) |
| 27 | defer wsServer.Close() |
| 28 | t.Log("Started WS server: ", wsServer.URL) |
| 29 | |
| 30 | wssServer := httptest.NewUnstartedServer(websocket.Handler(wsEchoHandler)) |
| 31 | wssServer.TLS = tlsServerConfig() |
| 32 | wssServer.StartTLS() |
| 33 | defer wssServer.Close() |
| 34 | t.Log("Started WSS server: ", wssServer.URL) |
| 35 | |
| 36 | routes := "route add ws /ws " + wsServer.URL + "\n" |
| 37 | routes += "route add ws /wss " + wssServer.URL + ` opts "proto=https"` + "\n" |
| 38 | routes += "route add ws /insecure " + wssServer.URL + ` opts "proto=https tlsskipverify=true"` + "\n" |
| 39 | routes += "route add ws /foo/strip " + wsServer.URL + ` opts "strip=/foo"` + "\n" |
| 40 | routes += "route add ws /bar/baz " + wsServer.URL + ` opts "prepend=/foo"` + "\n" |
| 41 | routes += "route add ws /old/world " + wsServer.URL + ` opts "prepend=/new strip=/old"` + "\n" |
| 42 | |
| 43 | httpProxy := httptest.NewServer(&HTTPProxy{ |
| 44 | Config: config.Proxy{NoRouteStatus: 404, GZIPContentTypes: regexp.MustCompile(".*")}, |
| 45 | Transport: &http.Transport{TLSClientConfig: tlsClientConfig()}, |
| 46 | InsecureTransport: &http.Transport{TLSClientConfig: tlsInsecureConfig()}, |
| 47 | Lookup: func(r *http.Request) *route.Target { |
| 48 | tbl, _ := route.NewTable(bytes.NewBufferString(routes)) |
| 49 | return tbl.Lookup(r, route.Picker["rr"], route.Matcher["prefix"], globCache, globEnabled) |
| 50 | }, |
| 51 | }) |
| 52 | defer httpProxy.Close() |
| 53 | t.Log("Started HTTP proxy: ", httpProxy.URL) |
| 54 | |
| 55 | httpsProxy := httptest.NewUnstartedServer(&HTTPProxy{ |
| 56 | Config: config.Proxy{NoRouteStatus: 404}, |
| 57 | Transport: &http.Transport{TLSClientConfig: tlsClientConfig()}, |
| 58 | InsecureTransport: &http.Transport{TLSClientConfig: tlsInsecureConfig()}, |
| 59 | Lookup: func(r *http.Request) *route.Target { |
| 60 | tbl, _ := route.NewTable(bytes.NewBufferString(routes)) |
| 61 | return tbl.Lookup(r, route.Picker["rr"], route.Matcher["prefix"], globCache, globEnabled) |
| 62 | }, |
| 63 | }) |
| 64 | httpsProxy.TLS = tlsServerConfig() |
| 65 | httpsProxy.StartTLS() |
| 66 | defer httpsProxy.Close() |
| 67 | t.Log("Started HTTPS proxy: ", httpsProxy.URL) |
| 68 | |
| 69 | wsServerURL := wsServer.URL[len("http://"):] |
| 70 | wssServerURL := wssServer.URL[len("https://"):] |
| 71 | httpProxyURL := httpProxy.URL[len("http://"):] |
| 72 | httpsProxyURL := httpsProxy.URL[len("https://"):] |
| 73 | |
| 74 | t.Run("ws-ws direct", func(t *testing.T) { testWSEcho(t, "ws://"+wsServerURL+"/ws", nil) }) |
| 75 | t.Run("wss-wss direct", func(t *testing.T) { testWSEcho(t, "wss://"+wssServerURL+"/wss", nil) }) |
nothing calls this directly
no test coverage detected