Redirection: permanent move option.
This commit is contained in:
parent
c944d203fb
commit
58d6681824
83 changed files with 622 additions and 8611 deletions
|
@ -923,7 +923,7 @@ func TestServerResponseEmptyBackend(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestBuildEntryPointRedirect(t *testing.T) {
|
||||
func TestBuildRedirectHandler(t *testing.T) {
|
||||
srv := Server{
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
|
@ -1016,133 +1016,6 @@ func TestBuildEntryPointRedirect(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestServerBuildEntryPointRedirect(t *testing.T) {
|
||||
srv := Server{
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
"http": &configuration.EntryPoint{Address: ":80"},
|
||||
"https": &configuration.EntryPoint{Address: ":443", TLS: &tls.TLS{}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
testCases := []struct {
|
||||
desc string
|
||||
srcEntryPointName string
|
||||
redirectEntryPoint string
|
||||
url string
|
||||
expectedURL string
|
||||
errorExpected bool
|
||||
}{
|
||||
{
|
||||
desc: "existing redirect entry point",
|
||||
srcEntryPointName: "http",
|
||||
redirectEntryPoint: "https",
|
||||
url: "http://foo:80",
|
||||
expectedURL: "https://foo:443",
|
||||
},
|
||||
{
|
||||
desc: "non-existing redirect entry point",
|
||||
srcEntryPointName: "http",
|
||||
redirectEntryPoint: "foo",
|
||||
url: "http://foo:80",
|
||||
errorExpected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
rewrite, err := srv.buildEntryPointRedirect(test.srcEntryPointName, test.redirectEntryPoint)
|
||||
if test.errorExpected {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
r := testhelpers.MustNewRequest(http.MethodGet, test.url, nil)
|
||||
rewrite.ServeHTTP(recorder, r, nil)
|
||||
|
||||
location, err := recorder.Result().Location()
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, test.expectedURL, location.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerBuildRedirect(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
globalConfiguration configuration.GlobalConfiguration
|
||||
redirectEntryPointName string
|
||||
expectedReplacement string
|
||||
errorExpected bool
|
||||
}{
|
||||
{
|
||||
desc: "Redirect endpoint http to https with HTTPS protocol",
|
||||
redirectEntryPointName: "https",
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
"http": &configuration.EntryPoint{Address: ":80"},
|
||||
"https": &configuration.EntryPoint{Address: ":443", TLS: &tls.TLS{}},
|
||||
},
|
||||
},
|
||||
expectedReplacement: "https://$1:443$2",
|
||||
},
|
||||
{
|
||||
desc: "Redirect endpoint http to http02 with HTTP protocol",
|
||||
redirectEntryPointName: "http02",
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
"http": &configuration.EntryPoint{Address: ":80"},
|
||||
"http02": &configuration.EntryPoint{Address: ":88"},
|
||||
},
|
||||
},
|
||||
expectedReplacement: "http://$1:88$2",
|
||||
},
|
||||
{
|
||||
desc: "Redirect endpoint to non-existent entry point",
|
||||
redirectEntryPointName: "foobar",
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
"http": &configuration.EntryPoint{Address: ":80"},
|
||||
"http02": &configuration.EntryPoint{Address: ":88"},
|
||||
},
|
||||
},
|
||||
errorExpected: true,
|
||||
},
|
||||
{
|
||||
desc: "Redirect endpoint to an entry point with a malformed address",
|
||||
redirectEntryPointName: "http02",
|
||||
globalConfiguration: configuration.GlobalConfiguration{
|
||||
EntryPoints: configuration.EntryPoints{
|
||||
"http": &configuration.EntryPoint{Address: ":80"},
|
||||
"http02": &configuration.EntryPoint{Address: "88"},
|
||||
},
|
||||
},
|
||||
errorExpected: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.desc, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := Server{globalConfiguration: test.globalConfiguration}
|
||||
|
||||
_, replacement, err := srv.buildRedirect(test.redirectEntryPointName)
|
||||
|
||||
require.Equal(t, test.errorExpected, err != nil, "Expected an error but don't have error, or Expected no error but have an error: %v", err)
|
||||
assert.Equal(t, test.expectedReplacement, replacement, "build redirect does not return the right replacement pattern")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func buildDynamicConfig(dynamicConfigBuilders ...func(*types.Configuration)) *types.Configuration {
|
||||
config := &types.Configuration{
|
||||
Frontends: make(map[string]*types.Frontend),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue