From 18d11e02d03ff21a42fceed7b0c1f3c774f7948c Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 1 Jun 2017 22:09:36 +0200 Subject: [PATCH] test: simplify stripPrefix* tests. --- middlewares/replace_path_test.go | 9 ++++---- middlewares/stripPrefixRegex_test.go | 31 ++++++++++++++-------------- middlewares/stripPrefix_test.go | 18 +++++++++------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/middlewares/replace_path_test.go b/middlewares/replace_path_test.go index 7c1204f8a..2812a3590 100644 --- a/middlewares/replace_path_test.go +++ b/middlewares/replace_path_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestReplacePath(t *testing.T) { @@ -27,12 +28,12 @@ func TestReplacePath(t *testing.T) { }), } - req, err := http.NewRequest("GET", "http://localhost"+path, nil) - assert.NoError(t, err, "%s: unexpected error.", path) + req, err := http.NewRequest(http.MethodGet, "http://localhost"+path, nil) + require.NoError(t, err, "%s: unexpected error.", path) handler.ServeHTTP(nil, req) - assert.Equal(t, expectedPath, replacementPath, "%s: unexpected path.", path) - assert.Equal(t, path, actualHeader, "%s: unexpected '%s' header.", path, ReplacedPathHeader) + assert.Equal(t, expectedPath, replacementPath, "Unexpected path.") + assert.Equal(t, path, actualHeader, "Unexpected '%s' header.", ReplacedPathHeader) }) } } diff --git a/middlewares/stripPrefixRegex_test.go b/middlewares/stripPrefixRegex_test.go index 03c8c8b44..72afa556a 100644 --- a/middlewares/stripPrefixRegex_test.go +++ b/middlewares/stripPrefixRegex_test.go @@ -21,45 +21,45 @@ func TestStripPrefixRegex(t *testing.T) { }{ { path: "/a/test", - expectedStatusCode: 404, + expectedStatusCode: http.StatusNotFound, }, { path: "/a/api/test", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedPath: "test", expectedHeader: "/a/api/", }, { path: "/b/api/", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedHeader: "/b/api/", }, { path: "/b/api/test1", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedPath: "test1", expectedHeader: "/b/api/", }, { path: "/b/api2/test2", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedPath: "test2", expectedHeader: "/b/api2/", }, { path: "/c/api/123/", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedHeader: "/c/api/123/", }, { path: "/c/api/123/test3", - expectedStatusCode: 200, + expectedStatusCode: http.StatusOK, expectedPath: "test3", expectedHeader: "/c/api/123/", }, { path: "/c/api/abc/test4", - expectedStatusCode: 404, + expectedStatusCode: http.StatusNotFound, }, } @@ -73,18 +73,17 @@ func TestStripPrefixRegex(t *testing.T) { actualPath = r.URL.Path actualHeader = r.Header.Get(ForwardedPrefixHeader) }) - handler := NewStripPrefixRegex(handlerPath, testPrefixRegex) - server := httptest.NewServer(handler) - defer server.Close() - resp, err := http.Get(server.URL + test.path) + req, err := http.NewRequest(http.MethodGet, "http://localhost"+test.path, nil) require.NoError(t, err, "%s: unexpected error.", test.path) - assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.path) - assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.path) - assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.path, ForwardedPrefixHeader) + resp := &httptest.ResponseRecorder{Code: http.StatusOK} + handler.ServeHTTP(resp, req) + + assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.") + assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.") + assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", ForwardedPrefixHeader) }) } - } diff --git a/middlewares/stripPrefix_test.go b/middlewares/stripPrefix_test.go index 807de926a..4b4cb1ae4 100644 --- a/middlewares/stripPrefix_test.go +++ b/middlewares/stripPrefix_test.go @@ -94,21 +94,23 @@ func TestStripPrefix(t *testing.T) { t.Parallel() var actualPath, actualHeader string - server := httptest.NewServer(&StripPrefix{ + handler := &StripPrefix{ Prefixes: test.prefixes, Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { actualPath = r.URL.Path actualHeader = r.Header.Get(ForwardedPrefixHeader) }), - }) - defer server.Close() + } - resp, err := http.Get(server.URL + test.path) - require.NoError(t, err, "%s: failed to send GET request.", test.desc) + req, err := http.NewRequest(http.MethodGet, "http://localhost"+test.path, nil) + require.NoError(t, err, "%s: unexpected error.", test.desc) - assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.desc) - assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.desc) - assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.desc, ForwardedPrefixHeader) + resp := &httptest.ResponseRecorder{Code: http.StatusOK} + handler.ServeHTTP(resp, req) + + assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.") + assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.") + assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", ForwardedPrefixHeader) }) } }