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"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestReplacePath(t *testing.T) {
|
func TestReplacePath(t *testing.T) {
|
||||||
|
@ -27,12 +28,12 @@ func TestReplacePath(t *testing.T) {
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", "http://localhost"+path, nil)
|
req, err := http.NewRequest(http.MethodGet, "http://localhost"+path, nil)
|
||||||
assert.NoError(t, err, "%s: unexpected error.", path)
|
require.NoError(t, err, "%s: unexpected error.", path)
|
||||||
|
|
||||||
handler.ServeHTTP(nil, req)
|
handler.ServeHTTP(nil, req)
|
||||||
assert.Equal(t, expectedPath, replacementPath, "%s: unexpected path.", path)
|
assert.Equal(t, expectedPath, replacementPath, "Unexpected path.")
|
||||||
assert.Equal(t, path, actualHeader, "%s: unexpected '%s' header.", path, ReplacedPathHeader)
|
assert.Equal(t, path, actualHeader, "Unexpected '%s' header.", ReplacedPathHeader)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,45 +21,45 @@ func TestStripPrefixRegex(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
path: "/a/test",
|
path: "/a/test",
|
||||||
expectedStatusCode: 404,
|
expectedStatusCode: http.StatusNotFound,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/a/api/test",
|
path: "/a/api/test",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedPath: "test",
|
expectedPath: "test",
|
||||||
expectedHeader: "/a/api/",
|
expectedHeader: "/a/api/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/b/api/",
|
path: "/b/api/",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedHeader: "/b/api/",
|
expectedHeader: "/b/api/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/b/api/test1",
|
path: "/b/api/test1",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedPath: "test1",
|
expectedPath: "test1",
|
||||||
expectedHeader: "/b/api/",
|
expectedHeader: "/b/api/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/b/api2/test2",
|
path: "/b/api2/test2",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedPath: "test2",
|
expectedPath: "test2",
|
||||||
expectedHeader: "/b/api2/",
|
expectedHeader: "/b/api2/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/c/api/123/",
|
path: "/c/api/123/",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedHeader: "/c/api/123/",
|
expectedHeader: "/c/api/123/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/c/api/123/test3",
|
path: "/c/api/123/test3",
|
||||||
expectedStatusCode: 200,
|
expectedStatusCode: http.StatusOK,
|
||||||
expectedPath: "test3",
|
expectedPath: "test3",
|
||||||
expectedHeader: "/c/api/123/",
|
expectedHeader: "/c/api/123/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/c/api/abc/test4",
|
path: "/c/api/abc/test4",
|
||||||
expectedStatusCode: 404,
|
expectedStatusCode: http.StatusNotFound,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,18 +73,17 @@ func TestStripPrefixRegex(t *testing.T) {
|
||||||
actualPath = r.URL.Path
|
actualPath = r.URL.Path
|
||||||
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
||||||
})
|
})
|
||||||
|
|
||||||
handler := NewStripPrefixRegex(handlerPath, testPrefixRegex)
|
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)
|
require.NoError(t, err, "%s: unexpected error.", test.path)
|
||||||
|
|
||||||
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.path)
|
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
|
||||||
assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.path)
|
handler.ServeHTTP(resp, req)
|
||||||
assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.path, ForwardedPrefixHeader)
|
|
||||||
|
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()
|
t.Parallel()
|
||||||
|
|
||||||
var actualPath, actualHeader string
|
var actualPath, actualHeader string
|
||||||
server := httptest.NewServer(&StripPrefix{
|
handler := &StripPrefix{
|
||||||
Prefixes: test.prefixes,
|
Prefixes: test.prefixes,
|
||||||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
actualPath = r.URL.Path
|
actualPath = r.URL.Path
|
||||||
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
actualHeader = r.Header.Get(ForwardedPrefixHeader)
|
||||||
}),
|
}),
|
||||||
})
|
}
|
||||||
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: failed to send GET request.", test.desc)
|
require.NoError(t, err, "%s: unexpected error.", test.desc)
|
||||||
|
|
||||||
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.desc)
|
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
|
||||||
assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.desc)
|
handler.ServeHTTP(resp, req)
|
||||||
assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.desc, ForwardedPrefixHeader)
|
|
||||||
|
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