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