From 19982fbf090015256817a6867769435a6112fb9f Mon Sep 17 00:00:00 2001 From: Chris Banks Date: Sat, 19 Aug 2023 19:41:59 +0100 Subject: [PATCH] Get rid of varargs in test helpers. Unnecessary abstraction in test helpers can easily undermine the value of the tests. Get rid of unnecessary logic and just be explicit. Most of this was generated with some sed-foo. --- integration_tests/disabled_routes_test.go | 6 +- integration_tests/error_handling_test.go | 12 +-- integration_tests/gone_test.go | 10 +- integration_tests/http_request_helpers.go | 8 +- integration_tests/integration_test.go | 4 +- integration_tests/metrics_test.go | 2 +- integration_tests/performance_test.go | 16 ++-- integration_tests/proxy_function_test.go | 58 ++++++------ integration_tests/redirect_test.go | 70 +++++++------- integration_tests/reload_api_test.go | 34 +++---- integration_tests/route_loading_test.go | 30 +++--- integration_tests/route_selection_test.go | 110 +++++++++++----------- integration_tests/router_support.go | 22 ++--- 13 files changed, 187 insertions(+), 195 deletions(-) diff --git a/integration_tests/disabled_routes_test.go b/integration_tests/disabled_routes_test.go index 714df121..7c1ae267 100644 --- a/integration_tests/disabled_routes_test.go +++ b/integration_tests/disabled_routes_test.go @@ -11,16 +11,16 @@ var _ = Describe("marking routes as disabled", func() { BeforeEach(func() { addRoute("/unavailable", Route{Handler: "gone", Disabled: true}) addRoute("/something-live", NewRedirectRoute("/somewhere-else")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should return a 503 to the client", func() { - resp := routerRequest("/unavailable") + resp := routerRequest(routerPort, "/unavailable") Expect(resp.StatusCode).To(Equal(503)) }) It("should continue to route other requests", func() { - resp := routerRequest("/something-live") + resp := routerRequest(routerPort, "/something-live") Expect(resp.StatusCode).To(Equal(301)) Expect(resp.Header.Get("Location")).To(Equal("/somewhere-else")) }) diff --git a/integration_tests/error_handling_test.go b/integration_tests/error_handling_test.go index 2241bf19..1c8cf534 100644 --- a/integration_tests/error_handling_test.go +++ b/integration_tests/error_handling_test.go @@ -11,14 +11,14 @@ var _ = Describe("error handling", func() { Describe("handling an empty routing table", func() { BeforeEach(func() { - reloadRoutes() + reloadRoutes(apiPort) }) It("should return a 503 error to the client", func() { - resp := routerRequest("/") + resp := routerRequest(routerPort, "/") Expect(resp.StatusCode).To(Equal(503)) - resp = routerRequest("/foo") + resp = routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(503)) }) }) @@ -26,16 +26,16 @@ var _ = Describe("error handling", func() { Describe("handling a panic", func() { BeforeEach(func() { addRoute("/boom", Route{Handler: "boom"}) - reloadRoutes() + reloadRoutes(apiPort) }) It("should return a 500 error to the client", func() { - resp := routerRequest("/boom") + resp := routerRequest(routerPort, "/boom") Expect(resp.StatusCode).To(Equal(500)) }) It("should log the fact", func() { - routerRequest("/boom") + routerRequest(routerPort, "/boom") logDetails := lastRouterErrorLogEntry() Expect(logDetails.Fields).To(Equal(map[string]interface{}{ diff --git a/integration_tests/gone_test.go b/integration_tests/gone_test.go index 18479bfe..990606cd 100644 --- a/integration_tests/gone_test.go +++ b/integration_tests/gone_test.go @@ -10,25 +10,25 @@ var _ = Describe("Gone routes", func() { BeforeEach(func() { addRoute("/foo", NewGoneRoute()) addRoute("/bar", NewGoneRoute("prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should support an exact gone route", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(410)) Expect(readBody(resp)).To(Equal("410 Gone\n")) - resp = routerRequest("/foo/bar") + resp = routerRequest(routerPort, "/foo/bar") Expect(resp.StatusCode).To(Equal(404)) Expect(readBody(resp)).To(Equal("404 page not found\n")) }) It("should support a prefix gone route", func() { - resp := routerRequest("/bar") + resp := routerRequest(routerPort, "/bar") Expect(resp.StatusCode).To(Equal(410)) Expect(readBody(resp)).To(Equal("410 Gone\n")) - resp = routerRequest("/bar/baz") + resp = routerRequest(routerPort, "/bar/baz") Expect(resp.StatusCode).To(Equal(410)) Expect(readBody(resp)).To(Equal("410 Gone\n")) }) diff --git a/integration_tests/http_request_helpers.go b/integration_tests/http_request_helpers.go index a1eead92..9822f2fa 100644 --- a/integration_tests/http_request_helpers.go +++ b/integration_tests/http_request_helpers.go @@ -15,12 +15,12 @@ import ( // revive:enable:dot-imports ) -func routerRequest(path string, optionalPort ...int) *http.Response { - return doRequest(newRequest("GET", routerURL(path, optionalPort...))) +func routerRequest(port int, path string) *http.Response { + return doRequest(newRequest("GET", routerURL(port, path))) } -func routerRequestWithHeaders(path string, headers map[string]string, optionalPort ...int) *http.Response { - return doRequest(newRequestWithHeaders("GET", routerURL(path, optionalPort...), headers)) +func routerRequestWithHeaders(port int, path string, headers map[string]string) *http.Response { + return doRequest(newRequestWithHeaders("GET", routerURL(port, path), headers)) } func newRequest(method, url string) *http.Request { diff --git a/integration_tests/integration_test.go b/integration_tests/integration_test.go index 853d311a..a5fcbd34 100644 --- a/integration_tests/integration_test.go +++ b/integration_tests/integration_test.go @@ -20,7 +20,7 @@ var _ = BeforeSuite(func() { if err != nil { Fail(err.Error()) } - err = startRouter(3169, 3168, nil) + err = startRouter(routerPort, apiPort, nil) if err != nil { Fail(err.Error()) } @@ -35,6 +35,6 @@ var _ = BeforeEach(func() { }) var _ = AfterSuite(func() { - stopRouter(3169) + stopRouter(routerPort) cleanupTempLogfile() }) diff --git a/integration_tests/metrics_test.go b/integration_tests/metrics_test.go index 7b4bfdc6..ba4812e9 100644 --- a/integration_tests/metrics_test.go +++ b/integration_tests/metrics_test.go @@ -10,7 +10,7 @@ var _ = Describe("/metrics API endpoint", func() { var responseBody string BeforeEach(func() { - resp := doRequest(newRequest("GET", routerAPIURL("/metrics"))) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/metrics"))) Expect(resp.StatusCode).To(Equal(200)) responseBody = readBody(resp) }) diff --git a/integration_tests/performance_test.go b/integration_tests/performance_test.go index f01c4248..08be778e 100644 --- a/integration_tests/performance_test.go +++ b/integration_tests/performance_test.go @@ -26,7 +26,7 @@ var _ = Describe("Performance", func() { addBackend("backend-2", backend2.URL) addRoute("/one", NewBackendRoute("backend-1")) addRoute("/two", NewBackendRoute("backend-2")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { backend1.Close() @@ -48,7 +48,7 @@ var _ = Describe("Performance", func() { case <-stopCh: return case <-ticker.C: - reloadRoutes() + reloadRoutes(apiPort) } }() @@ -62,9 +62,9 @@ var _ = Describe("Performance", func() { defer slowBackend.Close() addBackend("backend-slow", slowBackend.URL) addRoute("/slow", NewBackendRoute("backend-slow")) - reloadRoutes() + reloadRoutes(apiPort) - _, gen := generateLoad([]string{routerURL("/slow")}, 50) + _, gen := generateLoad([]string{routerURL(routerPort, "/slow")}, 50) defer gen.Stop() assertPerformantRouter(backend1, backend2, 50) @@ -75,9 +75,9 @@ var _ = Describe("Performance", func() { It("Router should not cause errors or much latency", func() { addBackend("backend-down", "http://127.0.0.1:3162/") addRoute("/down", NewBackendRoute("backend-down")) - reloadRoutes() + reloadRoutes(apiPort) - _, gen := generateLoad([]string{routerURL("/down")}, 50) + _, gen := generateLoad([]string{routerURL(routerPort, "/down")}, 50) defer gen.Stop() assertPerformantRouter(backend1, backend2, 50) @@ -102,7 +102,7 @@ var _ = Describe("Performance", func() { addBackend("backend-2", backend2.URL) addRoute("/one", NewBackendRoute("backend-1")) addRoute("/two", NewBackendRoute("backend-2")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { backend1.Close() @@ -117,7 +117,7 @@ var _ = Describe("Performance", func() { func assertPerformantRouter(backend1, backend2 *httptest.Server, rps int) { directResultsCh, _ := generateLoad([]string{backend1.URL + "/one", backend2.URL + "/two"}, rps) - routerResultsCh, _ := generateLoad([]string{routerURL("/one"), routerURL("/two")}, rps) + routerResultsCh, _ := generateLoad([]string{routerURL(routerPort, "/one"), routerURL(routerPort, "/two")}, rps) directResults := <-directResultsCh routerResults := <-routerResultsCh diff --git a/integration_tests/proxy_function_test.go b/integration_tests/proxy_function_test.go index bf30c1da..bb3fc002 100644 --- a/integration_tests/proxy_function_test.go +++ b/integration_tests/proxy_function_test.go @@ -20,9 +20,9 @@ var _ = Describe("Functioning as a reverse proxy", func() { It("should return a 502 if the connection to the backend is refused", func() { addBackend("not-running", "http://127.0.0.1:3164/") addRoute("/not-running", NewBackendRoute("not-running")) - reloadRoutes() + reloadRoutes(apiPort) - req, err := http.NewRequest(http.MethodGet, routerURL("/not-running"), nil) + req, err := http.NewRequest(http.MethodGet, routerURL(routerPort, "/not-running"), nil) Expect(err).NotTo(HaveOccurred()) req.Header.Set("X-Varnish", "12345678") @@ -50,7 +50,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { addRoute("/should-time-out", NewBackendRoute("black-hole")) reloadRoutes(3166) - req, err := http.NewRequest(http.MethodGet, routerURL("/should-time-out", 3167), nil) + req, err := http.NewRequest(http.MethodGet, routerURL(3167, "/should-time-out"), nil) Expect(err).NotTo(HaveOccurred()) req.Header.Set("X-Varnish", "12345678") @@ -98,7 +98,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should log and return a 504 if a backend takes longer than the configured response timeout to start returning a response", func() { - req := newRequest(http.MethodGet, routerURL("/tarpit1", 3167)) + req := newRequest(http.MethodGet, routerURL(3167, "/tarpit1")) req.Header.Set("X-Varnish", "12341112") resp := doRequest(req) Expect(resp.StatusCode).To(Equal(504)) @@ -117,7 +117,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should still return the response if the body takes longer than the header timeout", func() { - resp := routerRequest("/tarpit2", 3167) + resp := routerRequest(3167, "/tarpit2") Expect(resp.StatusCode).To(Equal(200)) Expect(readBody(resp)).To(Equal("Tarpit\n")) }) @@ -135,7 +135,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { recorderURL, _ = url.Parse(recorder.URL()) addBackend("backend", recorder.URL()) addRoute("/foo", NewBackendRoute("backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { @@ -143,7 +143,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should pass through most http headers to the backend", func() { - resp := routerRequestWithHeaders("/foo", map[string]string{ + resp := routerRequestWithHeaders(routerPort, "/foo", map[string]string{ "Foo": "bar", "User-Agent": "Router test suite 2.7182", }) @@ -156,7 +156,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should set the Host header to the backend hostname", func() { - resp := routerRequestWithHeaders("/foo", map[string]string{ + resp := routerRequestWithHeaders(routerPort, "/foo", map[string]string{ "Host": "www.example.com", }) Expect(resp.StatusCode).To(Equal(200)) @@ -168,7 +168,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { It("should not add a default User-Agent if there isn't one in the request", func() { // Most http libraries add a default User-Agent header. - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) @@ -178,14 +178,14 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should add the client IP to X-Forwardrd-For", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) beReq := recorder.ReceivedRequests()[0] Expect(beReq.Header.Get("X-Forwarded-For")).To(Equal("127.0.0.1")) - resp = routerRequestWithHeaders("/foo", map[string]string{ + resp = routerRequestWithHeaders(routerPort, "/foo", map[string]string{ "X-Forwarded-For": "10.9.8.7", }) Expect(resp.StatusCode).To(Equal(200)) @@ -199,14 +199,14 @@ var _ = Describe("Functioning as a reverse proxy", func() { // See https://tools.ietf.org/html/rfc2616#section-14.45 It("should add itself to the Via request header for an HTTP/1.1 request", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) beReq := recorder.ReceivedRequests()[0] Expect(beReq.Header.Get("Via")).To(Equal("1.1 router")) - resp = routerRequestWithHeaders("/foo", map[string]string{ + resp = routerRequestWithHeaders(routerPort, "/foo", map[string]string{ "Via": "1.0 fred, 1.1 barney", }) Expect(resp.StatusCode).To(Equal(200)) @@ -217,7 +217,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should add itself to the Via request header for an HTTP/1.0 request", func() { - req := newRequest(http.MethodGet, routerURL("/foo")) + req := newRequest(http.MethodGet, routerURL(routerPort, "/foo")) resp := doHTTP10Request(req) Expect(resp.StatusCode).To(Equal(200)) @@ -225,7 +225,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { beReq := recorder.ReceivedRequests()[0] Expect(beReq.Header.Get("Via")).To(Equal("1.0 router")) - req = newRequestWithHeaders("GET", routerURL("/foo"), map[string]string{ + req = newRequestWithHeaders("GET", routerURL(routerPort, "/foo"), map[string]string{ "Via": "1.0 fred, 1.1 barney", }) resp = doHTTP10Request(req) @@ -237,14 +237,14 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should add itself to the Via response heaver", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(200)) Expect(resp.Header.Get("Via")).To(Equal("1.1 router")) recorder.AppendHandlers(ghttp.RespondWith(200, "body", http.Header{ "Via": []string{"1.0 fred, 1.1 barney"}, })) - resp = routerRequest("/foo") + resp = routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(200)) Expect(resp.Header.Get("Via")).To(Equal("1.0 fred, 1.1 barney, 1.1 router")) }) @@ -260,7 +260,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { recorder = startRecordingBackend() addBackend("backend", recorder.URL()) addRoute("/foo", NewBackendRoute("backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { @@ -273,11 +273,11 @@ var _ = Describe("Functioning as a reverse proxy", func() { ghttp.VerifyRequest("DELETE", "/foo/bar/baz.json"), ) - req := newRequest("POST", routerURL("/foo")) + req := newRequest("POST", routerURL(routerPort, "/foo")) resp := doRequest(req) Expect(resp.StatusCode).To(Equal(200)) - req = newRequest("DELETE", routerURL("/foo/bar/baz.json")) + req = newRequest("DELETE", routerURL(routerPort, "/foo/bar/baz.json")) resp = doRequest(req) Expect(resp.StatusCode).To(Equal(200)) @@ -288,7 +288,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { recorder.AppendHandlers( ghttp.VerifyRequest("GET", "/foo/bar", "baz=qux"), ) - resp := routerRequest("/foo/bar?baz=qux") + resp := routerRequest(routerPort, "/foo/bar?baz=qux") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) @@ -302,7 +302,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { Expect(string(body)).To(Equal("I am the request body. Woohoo!")) }) - req := newRequest("POST", routerURL("/foo")) + req := newRequest("POST", routerURL(routerPort, "/foo")) req.Body = io.NopCloser(strings.NewReader("I am the request body. Woohoo!")) resp := doRequest(req) Expect(resp.StatusCode).To(Equal(200)) @@ -320,7 +320,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { recorder = startRecordingBackend() addBackend("backend", recorder.URL()+"/something") addRoute("/foo/bar", NewBackendRoute("backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { @@ -328,7 +328,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should merge the 2 paths", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) @@ -337,7 +337,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should preserve the request query string", func() { - resp := routerRequest("/foo/bar?baz=qux") + resp := routerRequest(routerPort, "/foo/bar?baz=qux") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) @@ -355,7 +355,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { recorder = startRecordingBackend() addBackend("backend", recorder.URL()) addRoute("/foo", NewBackendRoute("backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { @@ -363,7 +363,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should work with incoming HTTP/1.1 requests", func() { - req := newRequest("GET", routerURL("/foo")) + req := newRequest("GET", routerURL(routerPort, "/foo")) resp := doHTTP10Request(req) Expect(resp.StatusCode).To(Equal(200)) @@ -373,7 +373,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should proxy to the backend as HTTP/1.1 requests", func() { - req := newRequest("GET", routerURL("/foo")) + req := newRequest("GET", routerURL(routerPort, "/foo")) resp := doHTTP10Request(req) Expect(resp.StatusCode).To(Equal(200)) @@ -401,7 +401,7 @@ var _ = Describe("Functioning as a reverse proxy", func() { }) It("should correctly reverse proxy to a HTTPS backend", func() { - req := newRequest("GET", routerURL("/foo", 3167)) + req := newRequest("GET", routerURL(3167, "/foo")) resp := doRequest(req) Expect(resp.StatusCode).To(Equal(200)) diff --git a/integration_tests/redirect_test.go b/integration_tests/redirect_test.go index 92e935f3..91474c21 100644 --- a/integration_tests/redirect_test.go +++ b/integration_tests/redirect_test.go @@ -16,46 +16,46 @@ var _ = Describe("Redirection", func() { addRoute("/query-temp", NewRedirectRoute("/bar?query=true", "exact")) addRoute("/fragment", NewRedirectRoute("/bar#section", "exact")) addRoute("/preserve-query", NewRedirectRoute("/qux", "exact", "permanent", "preserve")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should redirect permanently by default", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(301)) }) It("should redirect temporarily when asked to", func() { - resp := routerRequest("/foo-temp") + resp := routerRequest(routerPort, "/foo-temp") Expect(resp.StatusCode).To(Equal(302)) }) It("should contain the redirect location", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.Header.Get("Location")).To(Equal("/bar")) }) It("should not preserve the query string for the source by default", func() { - resp := routerRequest("/foo?baz=qux") + resp := routerRequest(routerPort, "/foo?baz=qux") Expect(resp.Header.Get("Location")).To(Equal("/bar")) }) It("should preserve the query string for the source if specified", func() { - resp := routerRequest("/preserve-query?foo=bar") + resp := routerRequest(routerPort, "/preserve-query?foo=bar") Expect(resp.Header.Get("Location")).To(Equal("/qux?foo=bar")) }) It("should preserve the query string for the target", func() { - resp := routerRequest("/query-temp") + resp := routerRequest(routerPort, "/query-temp") Expect(resp.Header.Get("Location")).To(Equal("/bar?query=true")) }) It("should preserve the fragment for the target", func() { - resp := routerRequest("/fragment") + resp := routerRequest(routerPort, "/fragment") Expect(resp.Header.Get("Location")).To(Equal("/bar#section")) }) It("should contain cache headers of 30 mins", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.Header.Get("Cache-Control")).To(Equal("max-age=1800, public")) Expect( @@ -73,43 +73,43 @@ var _ = Describe("Redirection", func() { addRoute("/foo", NewRedirectRoute("/bar", "prefix")) addRoute("/foo-temp", NewRedirectRoute("/bar-temp", "prefix", "temporary")) addRoute("/qux", NewRedirectRoute("/baz", "prefix", "temporary", "ignore")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should redirect permanently to the destination", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.StatusCode).To(Equal(301)) Expect(resp.Header.Get("Location")).To(Equal("/bar")) }) It("should redirect temporarily to the destination when asked to", func() { - resp := routerRequest("/foo-temp") + resp := routerRequest(routerPort, "/foo-temp") Expect(resp.StatusCode).To(Equal(302)) Expect(resp.Header.Get("Location")).To(Equal("/bar-temp")) }) It("should preserve extra path sections when redirecting by default", func() { - resp := routerRequest("/foo/baz") + resp := routerRequest(routerPort, "/foo/baz") Expect(resp.Header.Get("Location")).To(Equal("/bar/baz")) }) It("should ignore extra path sections when redirecting if specified", func() { - resp := routerRequest("/qux/quux") + resp := routerRequest(routerPort, "/qux/quux") Expect(resp.Header.Get("Location")).To(Equal("/baz")) }) It("should preserve the query string when redirecting by default", func() { - resp := routerRequest("/foo?baz=qux") + resp := routerRequest(routerPort, "/foo?baz=qux") Expect(resp.Header.Get("Location")).To(Equal("/bar?baz=qux")) }) It("should not preserve the query string when redirecting if specified", func() { - resp := routerRequest("/qux/quux?foo=bar") + resp := routerRequest(routerPort, "/qux/quux?foo=bar") Expect(resp.Header.Get("Location")).To(Equal("/baz")) }) It("should contain cache headers of 30 mins", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.Header.Get("Cache-Control")).To(Equal("max-age=1800, public")) Expect( @@ -123,9 +123,9 @@ var _ = Describe("Redirection", func() { It("should handle path-preserving redirects with special characters", func() { addRoute("/foo%20bar", NewRedirectRoute("/bar%20baz", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) - resp := routerRequest("/foo bar/something") + resp := routerRequest(routerPort, "/foo bar/something") Expect(resp.StatusCode).To(Equal(301)) Expect(resp.Header.Get("Location")).To(Equal("/bar%20baz/something")) }) @@ -137,44 +137,44 @@ var _ = Describe("Redirection", func() { addRoute("/baz", NewRedirectRoute("http://foo.example.com/baz", "exact", "permanent", "preserve")) addRoute("/bar", NewRedirectRoute("http://bar.example.com/bar", "prefix")) addRoute("/qux", NewRedirectRoute("http://bar.example.com/qux", "prefix", "permanent", "ignore")) - reloadRoutes() + reloadRoutes(apiPort) }) Describe("exact redirect", func() { It("should redirect to the external URL", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/foo")) }) It("should not preserve the query string by default", func() { - resp := routerRequest("/foo?foo=qux") + resp := routerRequest(routerPort, "/foo?foo=qux") Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/foo")) }) It("should preserve the query string if specified", func() { - resp := routerRequest("/baz?foo=qux") + resp := routerRequest(routerPort, "/baz?foo=qux") Expect(resp.Header.Get("Location")).To(Equal("http://foo.example.com/baz?foo=qux")) }) }) Describe("prefix redirect", func() { It("should redirect to the external URL", func() { - resp := routerRequest("/bar") + resp := routerRequest(routerPort, "/bar") Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar")) }) It("should preserve extra path sections when redirecting by default", func() { - resp := routerRequest("/bar/baz") + resp := routerRequest(routerPort, "/bar/baz") Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar/baz")) }) It("should ignore extra path sections when redirecting if specified", func() { - resp := routerRequest("/qux/baz") + resp := routerRequest(routerPort, "/qux/baz") Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/qux")) }) It("should preserve the query string when redirecting", func() { - resp := routerRequest("/bar?baz=qux") + resp := routerRequest(routerPort, "/bar?baz=qux") Expect(resp.Header.Get("Location")).To(Equal("http://bar.example.com/bar?baz=qux")) }) }) @@ -188,41 +188,41 @@ var _ = Describe("Redirection", func() { addRoute("/pay-tax", NewRedirectRoute("https://tax.service.gov.uk/pay", "exact", "permanent", "ignore")) addRoute("/biz-bank", NewRedirectRoute("https://british-business-bank.co.uk", "prefix", "permanent", "ignore")) addRoute("/query-paramed", NewRedirectRoute("https://param.servicegov.uk?included-param=true", "exact", "permanent", "ignore")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should only preserve the _ga parameter when redirecting to service URLs that want to ignore query params", func() { - resp := routerRequest("/foo?_ga=identifier&blah=xyz") + resp := routerRequest(routerPort, "/foo?_ga=identifier&blah=xyz") Expect(resp.Header.Get("Location")).To(Equal("https://hmrc.service.gov.uk/pay?_ga=identifier")) }) It("should retain all params when redirecting to a route that wants them", func() { - resp := routerRequest("/bar?wanted=param&_ga=xyz&blah=xyz") + resp := routerRequest(routerPort, "/bar?wanted=param&_ga=xyz&blah=xyz") Expect(resp.Header.Get("Location")).To(Equal("https://bar.service.gov.uk/bar?wanted=param&_ga=xyz&blah=xyz")) }) It("should preserve the _ga parameter when redirecting to gov.uk URLs", func() { - resp := routerRequest("/baz?_ga=identifier") + resp := routerRequest(routerPort, "/baz?_ga=identifier") Expect(resp.Header.Get("Location")).To(Equal("https://gov.uk/baz-luhrmann?_ga=identifier")) }) It("should preserve the _ga parameter when redirecting to service.gov.uk URLs", func() { - resp := routerRequest("/pay-tax?_ga=12345") + resp := routerRequest(routerPort, "/pay-tax?_ga=12345") Expect(resp.Header.Get("Location")).To(Equal("https://tax.service.gov.uk/pay?_ga=12345")) }) It("should preserve only the first _ga parameter", func() { - resp := routerRequest("/pay-tax/?_ga=12345&_ga=6789") + resp := routerRequest(routerPort, "/pay-tax/?_ga=12345&_ga=6789") Expect(resp.Header.Get("Location")).To(Equal("https://tax.service.gov.uk/pay?_ga=12345")) }) It("should preserve the _ga param when redirecting to british business bank", func() { - resp := routerRequest("/biz-bank?unwanted=param&_ga=12345") + resp := routerRequest(routerPort, "/biz-bank?unwanted=param&_ga=12345") Expect(resp.Header.Get("Location")).To(Equal("https://british-business-bank.co.uk?_ga=12345")) }) It("should preserve the _ga param and any existing query string that the target URL has", func() { - resp := routerRequest("/query-paramed?unwanted_param=blah&_ga=12345") + resp := routerRequest(routerPort, "/query-paramed?unwanted_param=blah&_ga=12345") // https://param.servicegov.uk?included-param=true?unwanted_param=blah&_ga=12345 Expect(resp.Header.Get("Location")).To(Equal("https://param.servicegov.uk?_ga=12345&included-param=true")) }) diff --git a/integration_tests/reload_api_test.go b/integration_tests/reload_api_test.go index 47dd326b..e1cf5981 100644 --- a/integration_tests/reload_api_test.go +++ b/integration_tests/reload_api_test.go @@ -11,23 +11,23 @@ var _ = Describe("reload API endpoint", func() { Describe("request handling", func() { It("should return 202 for POST /reload", func() { - resp := doRequest(newRequest("POST", routerAPIURL("/reload"))) + resp := doRequest(newRequest("POST", routerURL(apiPort, "/reload"))) Expect(resp.StatusCode).To(Equal(202)) Expect(readBody(resp)).To(Equal("Reload queued")) }) It("should return 404 for POST /foo", func() { - resp := doRequest(newRequest("POST", routerAPIURL("/foo"))) + resp := doRequest(newRequest("POST", routerURL(apiPort, "/foo"))) Expect(resp.StatusCode).To(Equal(404)) }) It("should return 404 for POST /reload/foo", func() { - resp := doRequest(newRequest("POST", routerAPIURL("/reload/foo"))) + resp := doRequest(newRequest("POST", routerURL(apiPort, "/reload/foo"))) Expect(resp.StatusCode).To(Equal(404)) }) It("should return 405 for GET /reload", func() { - resp := doRequest(newRequest("GET", routerAPIURL("/reload"))) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/reload"))) Expect(resp.StatusCode).To(Equal(405)) Expect(resp.Header.Get("Allow")).To(Equal("POST")) }) @@ -36,34 +36,34 @@ var _ = Describe("reload API endpoint", func() { addRoute("/foo", NewRedirectRoute("/qux", "prefix")) start := time.Now() - doRequest(newRequest("POST", routerAPIURL("/reload"))) + doRequest(newRequest("POST", routerURL(apiPort, "/reload"))) end := time.Now() duration := end.Sub(start) Expect(duration.Nanoseconds()).To(BeNumerically("<", 5000000)) addRoute("/bar", NewRedirectRoute("/qux", "prefix")) - doRequest(newRequest("POST", routerAPIURL("/reload"))) + doRequest(newRequest("POST", routerURL(apiPort, "/reload"))) Eventually(func() int { - return routerRequest("/foo").StatusCode + return routerRequest(routerPort, "/foo").StatusCode }, time.Second*1).Should(Equal(301)) Eventually(func() int { - return routerRequest("/bar").StatusCode + return routerRequest(routerPort, "/bar").StatusCode }, time.Second*1).Should(Equal(301)) }) }) Describe("healthcheck", func() { It("should return 200 and sting 'OK' on /healthcheck", func() { - resp := doRequest(newRequest("GET", routerAPIURL("/healthcheck"))) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/healthcheck"))) Expect(resp.StatusCode).To(Equal(200)) Expect(readBody(resp)).To(Equal("OK")) }) It("should return 405 for other verbs", func() { - resp := doRequest(newRequest("POST", routerAPIURL("/healthcheck"))) + resp := doRequest(newRequest("POST", routerURL(apiPort, "/healthcheck"))) Expect(resp.StatusCode).To(Equal(405)) Expect(resp.Header.Get("Allow")).To(Equal("GET")) }) @@ -78,8 +78,8 @@ var _ = Describe("reload API endpoint", func() { addRoute("/foo", NewRedirectRoute("/bar", "prefix")) addRoute("/baz", NewRedirectRoute("/qux", "prefix")) addRoute("/foo", NewRedirectRoute("/bar/baz")) - reloadRoutes() - resp := doRequest(newRequest("GET", routerAPIURL("/stats"))) + reloadRoutes(apiPort) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/stats"))) Expect(resp.StatusCode).To(Equal(200)) readJSONBody(resp, &data) }) @@ -93,9 +93,9 @@ var _ = Describe("reload API endpoint", func() { var data map[string]map[string]interface{} BeforeEach(func() { - reloadRoutes() + reloadRoutes(apiPort) - resp := doRequest(newRequest("GET", routerAPIURL("/stats"))) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/stats"))) Expect(resp.StatusCode).To(Equal(200)) readJSONBody(resp, &data) }) @@ -106,7 +106,7 @@ var _ = Describe("reload API endpoint", func() { }) It("should return 405 for other verbs", func() { - resp := doRequest(newRequest("POST", routerAPIURL("/stats"))) + resp := doRequest(newRequest("POST", routerURL(apiPort, "/stats"))) Expect(resp.StatusCode).To(Equal(405)) Expect(resp.Header.Get("Allow")).To(Equal("GET")) }) @@ -117,9 +117,9 @@ var _ = Describe("reload API endpoint", func() { addRoute("/foo", NewRedirectRoute("/bar", "prefix")) addRoute("/baz", NewRedirectRoute("/qux", "prefix")) addRoute("/foo", NewRedirectRoute("/bar/baz")) - reloadRoutes() + reloadRoutes(apiPort) - resp := doRequest(newRequest("GET", routerAPIURL("/memory-stats"))) + resp := doRequest(newRequest("GET", routerURL(apiPort, "/memory-stats"))) Expect(resp.StatusCode).To(Equal(200)) var data map[string]interface{} diff --git a/integration_tests/route_loading_test.go b/integration_tests/route_loading_test.go index 6408b79c..827c1deb 100644 --- a/integration_tests/route_loading_test.go +++ b/integration_tests/route_loading_test.go @@ -31,19 +31,19 @@ var _ = Describe("loading routes from the db", func() { addRoute("/foo", NewBackendRoute("backend-1")) addRoute("/bar", Route{Handler: "fooey"}) addRoute("/baz", NewBackendRoute("backend-2")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should skip the invalid route", func() { - resp := routerRequest("/bar") + resp := routerRequest(routerPort, "/bar") Expect(resp.StatusCode).To(Equal(404)) }) It("should continue to load other routes", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("backend 1")) - resp = routerRequest("/baz") + resp = routerRequest(routerPort, "/baz") Expect(readBody(resp)).To(Equal("backend 2")) }) }) @@ -54,22 +54,22 @@ var _ = Describe("loading routes from the db", func() { addRoute("/bar", NewBackendRoute("backend-non-existent")) addRoute("/baz", NewBackendRoute("backend-2")) addRoute("/qux", NewBackendRoute("backend-1")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should skip the invalid route", func() { - resp := routerRequest("/bar") + resp := routerRequest(routerPort, "/bar") Expect(resp.StatusCode).To(Equal(404)) }) It("should continue to load other routes", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("backend 1")) - resp = routerRequest("/baz") + resp = routerRequest(routerPort, "/baz") Expect(readBody(resp)).To(Equal("backend 2")) - resp = routerRequest("/qux") + resp = routerRequest(routerPort, "/qux") Expect(readBody(resp)).To(Equal("backend 1")) }) }) @@ -82,23 +82,23 @@ var _ = Describe("loading routes from the db", func() { backend3 = startSimpleBackend("backend 3") addBackend("backend-3", blackHole) - stopRouter(3169) - err := startRouter(3169, 3168, []string{fmt.Sprintf("BACKEND_URL_backend-3=%s", backend3.URL)}) + stopRouter(routerPort) + err := startRouter(routerPort, apiPort, []string{fmt.Sprintf("BACKEND_URL_backend-3=%s", backend3.URL)}) Expect(err).NotTo(HaveOccurred()) addRoute("/oof", NewBackendRoute("backend-3")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { - stopRouter(3169) - err := startRouter(3169, 3168, nil) + stopRouter(routerPort) + err := startRouter(routerPort, apiPort, nil) Expect(err).NotTo(HaveOccurred()) backend3.Close() }) It("should send requests to the backend_url provided in the env var", func() { - resp := routerRequest("/oof") + resp := routerRequest(routerPort, "/oof") Expect(resp.StatusCode).To(Equal(200)) Expect(readBody(resp)).To(Equal("backend 3")) }) diff --git a/integration_tests/route_selection_test.go b/integration_tests/route_selection_test.go index c3c089ef..4b60e44b 100644 --- a/integration_tests/route_selection_test.go +++ b/integration_tests/route_selection_test.go @@ -24,7 +24,7 @@ var _ = Describe("Route selection", func() { addRoute("/foo", NewBackendRoute("backend-1")) addRoute("/bar", NewBackendRoute("backend-2")) addRoute("/baz", NewBackendRoute("backend-1")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { backend1.Close() @@ -32,29 +32,29 @@ var _ = Describe("Route selection", func() { }) It("should route a matching request to the corresponding backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("backend 1")) - resp = routerRequest("/bar") + resp = routerRequest(routerPort, "/bar") Expect(readBody(resp)).To(Equal("backend 2")) - resp = routerRequest("/baz") + resp = routerRequest(routerPort, "/baz") Expect(readBody(resp)).To(Equal("backend 1")) }) It("should 404 for children of the exact route", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(resp.StatusCode).To(Equal(404)) }) It("should 404 for non-matching requests", func() { - resp := routerRequest("/wibble") + resp := routerRequest(routerPort, "/wibble") Expect(resp.StatusCode).To(Equal(404)) - resp = routerRequest("/") + resp = routerRequest(routerPort, "/") Expect(resp.StatusCode).To(Equal(404)) - resp = routerRequest("/foo.json") + resp = routerRequest(routerPort, "/foo.json") Expect(resp.StatusCode).To(Equal(404)) }) }) @@ -73,7 +73,7 @@ var _ = Describe("Route selection", func() { addRoute("/foo", NewBackendRoute("backend-1", "prefix")) addRoute("/bar", NewBackendRoute("backend-2", "prefix")) addRoute("/baz", NewBackendRoute("backend-1", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { backend1.Close() @@ -81,35 +81,35 @@ var _ = Describe("Route selection", func() { }) It("should route requests for the prefix to the backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("backend 1")) - resp = routerRequest("/bar") + resp = routerRequest(routerPort, "/bar") Expect(readBody(resp)).To(Equal("backend 2")) - resp = routerRequest("/baz") + resp = routerRequest(routerPort, "/baz") Expect(readBody(resp)).To(Equal("backend 1")) }) It("should route requests for the children of the prefix to the backend", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(readBody(resp)).To(Equal("backend 1")) - resp = routerRequest("/bar/foo.json") + resp = routerRequest(routerPort, "/bar/foo.json") Expect(readBody(resp)).To(Equal("backend 2")) - resp = routerRequest("/baz/fooey/kablooie") + resp = routerRequest(routerPort, "/baz/fooey/kablooie") Expect(readBody(resp)).To(Equal("backend 1")) }) It("should 404 for non-matching requests", func() { - resp := routerRequest("/wibble") + resp := routerRequest(routerPort, "/wibble") Expect(resp.StatusCode).To(Equal(404)) - resp = routerRequest("/") + resp = routerRequest(routerPort, "/") Expect(resp.StatusCode).To(Equal(404)) - resp = routerRequest("/foo.json") + resp = routerRequest(routerPort, "/foo.json") Expect(resp.StatusCode).To(Equal(404)) }) }) @@ -126,7 +126,7 @@ var _ = Describe("Route selection", func() { addBackend("outer-backend", outer.URL) addBackend("inner-backend", inner.URL) addRoute("/foo", NewBackendRoute("outer-backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { outer.Close() @@ -136,21 +136,21 @@ var _ = Describe("Route selection", func() { Describe("with an exact child", func() { BeforeEach(func() { addRoute("/foo/bar", NewBackendRoute("inner-backend")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should route the prefix to the outer backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("outer")) }) It("should route the exact child to the inner backend", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(readBody(resp)).To(Equal("inner")) }) It("should route the children of the exact child to the outer backend", func() { - resp := routerRequest("/foo/bar/baz") + resp := routerRequest(routerPort, "/foo/bar/baz") Expect(readBody(resp)).To(Equal("outer")) }) }) @@ -158,29 +158,29 @@ var _ = Describe("Route selection", func() { Describe("with a prefix child", func() { BeforeEach(func() { addRoute("/foo/bar", NewBackendRoute("inner-backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) It("should route the outer prefix to the outer backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("outer")) }) It("should route the inner prefix to the inner backend", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(readBody(resp)).To(Equal("inner")) }) It("should route the children of the inner prefix to the inner backend", func() { - resp := routerRequest("/foo/bar/baz") + resp := routerRequest(routerPort, "/foo/bar/baz") Expect(readBody(resp)).To(Equal("inner")) }) It("should route other children of the outer prefix to the outer backend", func() { - resp := routerRequest("/foo/baz") + resp := routerRequest(routerPort, "/foo/baz") Expect(readBody(resp)).To(Equal("outer")) - resp = routerRequest("/foo/bar.json") + resp = routerRequest(routerPort, "/foo/bar.json") Expect(readBody(resp)).To(Equal("outer")) }) }) @@ -194,43 +194,43 @@ var _ = Describe("Route selection", func() { addBackend("innerer-backend", innerer.URL) addRoute("/foo/bar", NewBackendRoute("inner-backend")) addRoute("/foo/bar/baz", NewBackendRoute("innerer-backend", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { innerer.Close() }) It("should route the outer prefix to the outer backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("outer")) - resp = routerRequest("/foo/baz") + resp = routerRequest(routerPort, "/foo/baz") Expect(readBody(resp)).To(Equal("outer")) - resp = routerRequest("/foo/bar.json") + resp = routerRequest(routerPort, "/foo/bar.json") Expect(readBody(resp)).To(Equal("outer")) }) It("should route the exact route to the inner backend", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(readBody(resp)).To(Equal("inner")) }) It("should route other children of the exact route to the outer backend", func() { - resp := routerRequest("/foo/bar/wibble") + resp := routerRequest(routerPort, "/foo/bar/wibble") Expect(readBody(resp)).To(Equal("outer")) - resp = routerRequest("/foo/bar/baz.json") + resp = routerRequest(routerPort, "/foo/bar/baz.json") Expect(readBody(resp)).To(Equal("outer")) }) It("should route the inner prefix route to the innerer backend", func() { - resp := routerRequest("/foo/bar/baz") + resp := routerRequest(routerPort, "/foo/bar/baz") Expect(readBody(resp)).To(Equal("innerer")) }) It("should route children of the inner prefix route to the innerer backend", func() { - resp := routerRequest("/foo/bar/baz/wibble") + resp := routerRequest(routerPort, "/foo/bar/baz/wibble") Expect(readBody(resp)).To(Equal("innerer")) }) }) @@ -249,7 +249,7 @@ var _ = Describe("Route selection", func() { addBackend("backend-2", backend2.URL) addRoute("/foo", NewBackendRoute("backend-1", "prefix")) addRoute("/foo", NewBackendRoute("backend-2")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { backend1.Close() @@ -257,12 +257,12 @@ var _ = Describe("Route selection", func() { }) It("should route the exact route to the exact backend", func() { - resp := routerRequest("/foo") + resp := routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("backend 2")) }) It("should route children of the route to the prefix backend", func() { - resp := routerRequest("/foo/bar") + resp := routerRequest(routerPort, "/foo/bar") Expect(readBody(resp)).To(Equal("backend 1")) }) }) @@ -287,29 +287,29 @@ var _ = Describe("Route selection", func() { It("should handle an exact route at the root level", func() { addRoute("/", NewBackendRoute("root")) - reloadRoutes() + reloadRoutes(apiPort) - resp := routerRequest("/") + resp := routerRequest(routerPort, "/") Expect(readBody(resp)).To(Equal("root backend")) - resp = routerRequest("/foo") + resp = routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("other backend")) - resp = routerRequest("/bar") + resp = routerRequest(routerPort, "/bar") Expect(resp.StatusCode).To(Equal(404)) }) It("should handle a prefix route at the root level", func() { addRoute("/", NewBackendRoute("root", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) - resp := routerRequest("/") + resp := routerRequest(routerPort, "/") Expect(readBody(resp)).To(Equal("root backend")) - resp = routerRequest("/foo") + resp = routerRequest(routerPort, "/foo") Expect(readBody(resp)).To(Equal("other backend")) - resp = routerRequest("/bar") + resp = routerRequest(routerPort, "/bar") Expect(readBody(resp)).To(Equal("root backend")) }) }) @@ -327,7 +327,7 @@ var _ = Describe("Route selection", func() { addBackend("other", recorder.URL()) addRoute("/", NewBackendRoute("root", "prefix")) addRoute("/foo/bar", NewBackendRoute("other", "prefix")) - reloadRoutes() + reloadRoutes(apiPort) }) AfterEach(func() { root.Close() @@ -335,19 +335,19 @@ var _ = Describe("Route selection", func() { }) It("should not be redirected by our simple test backend", func() { - resp := routerRequest("//") + resp := routerRequest(routerPort, "//") Expect(readBody(resp)).To(Equal("fallthrough")) }) It("should not be redirected by our recorder backend", func() { - resp := routerRequest("/foo/bar/baz//qux") + resp := routerRequest(routerPort, "/foo/bar/baz//qux") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) Expect(recorder.ReceivedRequests()[0].URL.Path).To(Equal("/foo/bar/baz//qux")) }) It("should collapse double slashes when looking up route, but pass request as-is", func() { - resp := routerRequest("/foo//bar") + resp := routerRequest(routerPort, "/foo//bar") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) Expect(recorder.ReceivedRequests()[0].URL.Path).To(Equal("/foo//bar")) @@ -367,9 +367,9 @@ var _ = Describe("Route selection", func() { It("should handle spaces (%20) in paths", func() { addRoute("/foo%20bar", NewBackendRoute("backend")) - reloadRoutes() + reloadRoutes(apiPort) - resp := routerRequest("/foo bar") + resp := routerRequest(routerPort, "/foo bar") Expect(resp.StatusCode).To(Equal(200)) Expect(recorder.ReceivedRequests()).To(HaveLen(1)) Expect(recorder.ReceivedRequests()[0].RequestURI).To(Equal("/foo%20bar")) diff --git a/integration_tests/router_support.go b/integration_tests/router_support.go index a963f133..d03d526f 100644 --- a/integration_tests/router_support.go +++ b/integration_tests/router_support.go @@ -16,24 +16,16 @@ import ( // revive:enable:dot-imports ) -func routerURL(path string, optionalPort ...int) string { - port := 3169 - if len(optionalPort) > 0 { - port = optionalPort[0] - } - return fmt.Sprintf("http://127.0.0.1:%d%s", port, path) -} +const ( + routerPort = 3169 + apiPort = 3168 +) -func routerAPIURL(path string) string { - return routerURL(path, 3168) +func routerURL(port int, path string) string { + return fmt.Sprintf("http://127.0.0.1:%d%s", port, path) } -func reloadRoutes(optionalPort ...int) { - port := 3168 - if len(optionalPort) > 0 { - port = optionalPort[0] - } - +func reloadRoutes(port int) { req, err := http.NewRequestWithContext( context.Background(), http.MethodPost,