fix: stripPrefix middleware with empty resulting path.
This commit is contained in:
parent
cdb2446e32
commit
7afd2dbd20
14 changed files with 426 additions and 245 deletions
|
@ -31,42 +31,66 @@ func TestStripPrefixRegex(t *testing.T) {
|
|||
expectedPath: "/a/test",
|
||||
},
|
||||
{
|
||||
path: "/a/test",
|
||||
path: "/a/test/",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "/a/test",
|
||||
expectedPath: "/a/test/",
|
||||
},
|
||||
{
|
||||
path: "/a/api/",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "",
|
||||
expectedHeader: "/a/api/",
|
||||
},
|
||||
{
|
||||
path: "/a/api/test",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "test",
|
||||
expectedPath: "/test",
|
||||
expectedHeader: "/a/api/",
|
||||
},
|
||||
{
|
||||
path: "/a/api/test/",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "/test/",
|
||||
expectedHeader: "/a/api/",
|
||||
},
|
||||
{
|
||||
path: "/b/api/",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "",
|
||||
expectedHeader: "/b/api/",
|
||||
},
|
||||
{
|
||||
path: "/b/api",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "/b/api",
|
||||
},
|
||||
{
|
||||
path: "/b/api/test1",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "test1",
|
||||
expectedPath: "/test1",
|
||||
expectedHeader: "/b/api/",
|
||||
},
|
||||
{
|
||||
path: "/b/api2/test2",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "test2",
|
||||
expectedPath: "/test2",
|
||||
expectedHeader: "/b/api2/",
|
||||
},
|
||||
{
|
||||
path: "/c/api/123/",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "",
|
||||
expectedHeader: "/c/api/123/",
|
||||
},
|
||||
{
|
||||
path: "/c/api/123",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "/c/api/123",
|
||||
},
|
||||
{
|
||||
path: "/c/api/123/test3",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "test3",
|
||||
expectedPath: "/test3",
|
||||
expectedHeader: "/c/api/123/",
|
||||
},
|
||||
{
|
||||
|
@ -77,8 +101,8 @@ func TestStripPrefixRegex(t *testing.T) {
|
|||
{
|
||||
path: "/a/api/a%2Fb",
|
||||
expectedStatusCode: http.StatusOK,
|
||||
expectedPath: "a/b",
|
||||
expectedRawPath: "a%2Fb",
|
||||
expectedPath: "/a/b",
|
||||
expectedRawPath: "/a%2Fb",
|
||||
expectedHeader: "/a/api/",
|
||||
},
|
||||
}
|
||||
|
@ -88,11 +112,12 @@ func TestStripPrefixRegex(t *testing.T) {
|
|||
t.Run(test.path, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var actualPath, actualRawPath, actualHeader string
|
||||
var actualPath, actualRawPath, actualHeader, requestURI string
|
||||
handlerPath := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
actualPath = r.URL.Path
|
||||
actualRawPath = r.URL.RawPath
|
||||
actualHeader = r.Header.Get(stripprefix.ForwardedPrefixHeader)
|
||||
requestURI = r.RequestURI
|
||||
})
|
||||
handler, err := New(context.Background(), handlerPath, testPrefixRegex, "foo-strip-prefix-regex")
|
||||
require.NoError(t, err)
|
||||
|
@ -106,6 +131,18 @@ func TestStripPrefixRegex(t *testing.T) {
|
|||
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
|
||||
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
|
||||
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", stripprefix.ForwardedPrefixHeader)
|
||||
|
||||
if test.expectedPath != test.path {
|
||||
expectedRequestURI := test.expectedPath
|
||||
if test.expectedRawPath != "" {
|
||||
// go HTTP uses the raw path when existent in the RequestURI
|
||||
expectedRequestURI = test.expectedRawPath
|
||||
}
|
||||
if test.expectedPath == "" {
|
||||
expectedRequestURI = "/"
|
||||
}
|
||||
assert.Equal(t, expectedRequestURI, requestURI, "Unexpected request URI.")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue