1
0
Fork 0

refactor: Enhance rules tests.

- refactor: change incorrect package.
- refactor: test readability.
This commit is contained in:
Fernandez Ludovic 2017-05-28 05:50:03 +02:00 committed by Ludovic Fernandez
parent cbccdd51c5
commit b6c5c14447
7 changed files with 171 additions and 150 deletions

View file

@ -16,6 +16,7 @@ func TestStripPrefix(t *testing.T) {
path string
expectedStatusCode int
expectedPath string
expectedHeader string
}{
{
desc: "no prefixes configured",
@ -29,6 +30,7 @@ func TestStripPrefix(t *testing.T) {
path: "/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/",
},
{
desc: "prefix and path matching",
@ -36,6 +38,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
desc: "path prefix on exactly matching path",
@ -43,6 +46,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat/",
},
{
desc: "path prefix on matching longer path",
@ -50,6 +54,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat/",
},
{
desc: "path prefix on mismatching path",
@ -63,6 +68,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
desc: "earlier prefix matching",
@ -70,6 +76,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat",
},
{
desc: "later prefix matching",
@ -77,6 +84,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
}
@ -84,20 +92,23 @@ func TestStripPrefix(t *testing.T) {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
var gotPath string
var actualPath, actualHeader string
server := httptest.NewServer(&StripPrefix{
Prefixes: test.prefixes,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
actualPath = r.URL.Path
actualHeader = r.Header.Get(ForwardedPrefixHeader)
}),
})
defer server.Close()
resp, err := http.Get(server.URL + test.path)
require.NoError(t, err, "Failed to send GET request")
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "Unexpected status code")
require.NoError(t, err, "%s: failed to send GET request.", test.desc)
assert.Equal(t, test.expectedPath, gotPath, "Unexpected path")
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)
})
}
}