traefik/pkg/middlewares/stripprefixregex/strip_prefix_regex_test.go

105 lines
2.9 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package stripprefixregex
2017-03-24 11:07:59 +00:00
import (
2018-11-14 09:18:03 +00:00
"context"
2017-03-24 11:07:59 +00:00
"net/http"
"net/http/httptest"
"testing"
"github.com/containous/traefik/pkg/config/dynamic"
2019-03-15 08:42:03 +00:00
"github.com/containous/traefik/pkg/middlewares/stripprefix"
"github.com/containous/traefik/pkg/testhelpers"
"github.com/stretchr/testify/assert"
2018-11-14 09:18:03 +00:00
"github.com/stretchr/testify/require"
2017-03-24 11:07:59 +00:00
)
func TestStripPrefixRegex(t *testing.T) {
testPrefixRegex := dynamic.StripPrefixRegex{
2018-11-14 09:18:03 +00:00
Regex: []string{"/a/api/", "/b/{regex}/", "/c/{category}/{id:[0-9]+}/"},
}
2017-03-24 11:07:59 +00:00
2018-11-14 09:18:03 +00:00
testCases := []struct {
path string
expectedStatusCode int
expectedPath string
2017-11-21 13:28:03 +00:00
expectedRawPath string
expectedHeader string
2017-03-24 11:07:59 +00:00
}{
{
path: "/a/test",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusNotFound,
},
{
path: "/a/api/test",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "test",
expectedHeader: "/a/api/",
},
{
path: "/b/api/",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedHeader: "/b/api/",
},
{
path: "/b/api/test1",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "test1",
expectedHeader: "/b/api/",
},
{
path: "/b/api2/test2",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "test2",
expectedHeader: "/b/api2/",
},
{
path: "/c/api/123/",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedHeader: "/c/api/123/",
},
{
path: "/c/api/123/test3",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusOK,
expectedPath: "test3",
expectedHeader: "/c/api/123/",
},
{
path: "/c/api/abc/test4",
2017-06-01 20:09:36 +00:00
expectedStatusCode: http.StatusNotFound,
},
2017-11-21 13:28:03 +00:00
{
path: "/a/api/a%2Fb",
expectedStatusCode: http.StatusOK,
expectedPath: "a/b",
expectedRawPath: "a%2Fb",
expectedHeader: "/a/api/",
},
2017-03-24 11:07:59 +00:00
}
2018-11-14 09:18:03 +00:00
for _, test := range testCases {
test := test
t.Run(test.path, func(t *testing.T) {
t.Parallel()
2017-11-21 13:28:03 +00:00
var actualPath, actualRawPath, actualHeader string
handlerPath := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actualPath = r.URL.Path
2017-11-21 13:28:03 +00:00
actualRawPath = r.URL.RawPath
2018-11-14 09:18:03 +00:00
actualHeader = r.Header.Get(stripprefix.ForwardedPrefixHeader)
})
2018-11-14 09:18:03 +00:00
handler, err := New(context.Background(), handlerPath, testPrefixRegex, "foo-strip-prefix-regex")
require.NoError(t, err)
2017-06-03 12:58:35 +00:00
req := testhelpers.MustNewRequest(http.MethodGet, "http://localhost"+test.path, nil)
2017-06-01 20:09:36 +00:00
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
2017-06-03 12:58:35 +00:00
2017-06-01 20:09:36 +00:00
handler.ServeHTTP(resp, req)
assert.Equal(t, test.expectedStatusCode, resp.Code, "Unexpected status code.")
assert.Equal(t, test.expectedPath, actualPath, "Unexpected path.")
2017-11-21 13:28:03 +00:00
assert.Equal(t, test.expectedRawPath, actualRawPath, "Unexpected raw path.")
2018-11-14 09:18:03 +00:00
assert.Equal(t, test.expectedHeader, actualHeader, "Unexpected '%s' header.", stripprefix.ForwardedPrefixHeader)
})
2017-03-24 11:07:59 +00:00
}
}