2016-08-27 22:03:40 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-08-27 22:03:40 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-08-27 22:03:40 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-01-30 20:02:26 +02:00
|
|
|
type middlewareGenerator func() echo.MiddlewareFunc
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectHTTPSRedirect(t *testing.T) {
|
2021-04-06 09:11:31 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
whenHost: "labstack.com",
|
|
|
|
expectLocation: "https://labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "labstack.com",
|
|
|
|
whenHeader: map[string][]string{echo.HeaderXForwardedProto: {"https"}},
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2016-08-27 22:03:40 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
res := redirectTest(HTTPSRedirect, tc.whenHost, tc.whenHeader)
|
2018-02-02 17:31:07 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
2018-02-02 17:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectHTTPSWWWRedirect(t *testing.T) {
|
2021-04-06 09:11:31 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
whenHost: "labstack.com",
|
|
|
|
expectLocation: "https://www.labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "a.com",
|
|
|
|
expectLocation: "https://www.a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "ip",
|
|
|
|
expectLocation: "https://www.ip/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "labstack.com",
|
|
|
|
whenHeader: map[string][]string{echo.HeaderXForwardedProto: {"https"}},
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2016-08-27 22:03:40 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
res := redirectTest(HTTPSWWWRedirect, tc.whenHost, tc.whenHeader)
|
2018-02-02 17:31:07 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
2018-02-02 17:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
|
2021-04-06 09:11:31 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "https://labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "a.com",
|
|
|
|
expectLocation: "https://a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "ip",
|
|
|
|
expectLocation: "https://ip/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
whenHeader: map[string][]string{echo.HeaderXForwardedProto: {"https"}},
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
2016-09-28 19:26:02 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
res := redirectTest(HTTPSNonWWWRedirect, tc.whenHost, tc.whenHeader)
|
2018-02-02 17:31:07 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
2018-02-02 17:31:07 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectWWWRedirect(t *testing.T) {
|
2021-04-06 09:11:31 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
whenHost: "labstack.com",
|
|
|
|
expectLocation: "http://www.labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "a.com",
|
|
|
|
expectLocation: "http://www.a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "ip",
|
|
|
|
expectLocation: "http://www.ip/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "a.com",
|
|
|
|
whenHeader: map[string][]string{echo.HeaderXForwardedProto: {"https"}},
|
|
|
|
expectLocation: "https://www.a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "www.ip",
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
res := redirectTest(WWWRedirect, tc.whenHost, tc.whenHeader)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectNonWWWRedirect(t *testing.T) {
|
2021-04-06 09:11:31 +02:00
|
|
|
var testCases = []struct {
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "http://labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "www.a.com",
|
|
|
|
expectLocation: "http://a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "www.a.com",
|
|
|
|
whenHeader: map[string][]string{echo.HeaderXForwardedProto: {"https"}},
|
|
|
|
expectLocation: "https://a.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
whenHost: "ip",
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
res := redirectTest(NonWWWRedirect, tc.whenHost, tc.whenHeader)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNonWWWRedirectWithConfig(t *testing.T) {
|
|
|
|
var testCases = []struct {
|
|
|
|
name string
|
|
|
|
givenCode int
|
|
|
|
givenSkipFunc func(c echo.Context) bool
|
|
|
|
whenHost string
|
|
|
|
whenHeader http.Header
|
|
|
|
expectLocation string
|
|
|
|
expectStatusCode int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "usual redirect",
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "http://labstack.com/",
|
|
|
|
expectStatusCode: http.StatusMovedPermanently,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "redirect is skipped",
|
|
|
|
givenSkipFunc: func(c echo.Context) bool {
|
|
|
|
return true // skip always
|
|
|
|
},
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "",
|
|
|
|
expectStatusCode: http.StatusOK,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "redirect with custom status code",
|
|
|
|
givenCode: http.StatusSeeOther,
|
|
|
|
whenHost: "www.labstack.com",
|
|
|
|
expectLocation: "http://labstack.com/",
|
|
|
|
expectStatusCode: http.StatusSeeOther,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.whenHost, func(t *testing.T) {
|
|
|
|
middleware := func() echo.MiddlewareFunc {
|
|
|
|
return NonWWWRedirectWithConfig(RedirectConfig{
|
|
|
|
Skipper: tc.givenSkipFunc,
|
|
|
|
Code: tc.givenCode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
res := redirectTest(middleware, tc.whenHost, tc.whenHeader)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2021-04-06 09:11:31 +02:00
|
|
|
assert.Equal(t, tc.expectStatusCode, res.Code)
|
|
|
|
assert.Equal(t, tc.expectLocation, res.Header().Get(echo.HeaderLocation))
|
|
|
|
})
|
|
|
|
}
|
2018-01-30 20:02:26 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:31:07 +02:00
|
|
|
func redirectTest(fn middlewareGenerator, host string, header http.Header) *httptest.ResponseRecorder {
|
2016-08-27 22:03:40 +02:00
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
req.Host = host
|
2018-02-02 17:31:07 +02:00
|
|
|
if header != nil {
|
|
|
|
req.Header = header
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
res := httptest.NewRecorder()
|
2016-08-27 22:03:40 +02:00
|
|
|
c := e.NewContext(req, res)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
|
|
|
fn()(next)(c)
|
|
|
|
|
|
|
|
return res
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|