test: simplify stripPrefix* tests.
This commit is contained in:
parent
a71d69cc3c
commit
18d11e02d0
3 changed files with 30 additions and 28 deletions
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue