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) {
|
2018-02-02 17:31:07 +02:00
|
|
|
res := redirectTest(HTTPSRedirect, "labstack.com", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2017-02-23 01:57:12 +02:00
|
|
|
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:31:07 +02:00
|
|
|
func TestHTTPSRedirectBehindTLSTerminationProxy(t *testing.T) {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Set(echo.HeaderXForwardedProto, "https")
|
|
|
|
res := redirectTest(HTTPSRedirect, "labstack.com", header)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, res.Code)
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectHTTPSWWWRedirect(t *testing.T) {
|
2018-02-02 17:31:07 +02:00
|
|
|
res := redirectTest(HTTPSWWWRedirect, "labstack.com", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2017-02-23 01:57:12 +02:00
|
|
|
assert.Equal(t, "https://www.labstack.com/", res.Header().Get(echo.HeaderLocation))
|
2016-08-27 22:03:40 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:31:07 +02:00
|
|
|
func TestRedirectHTTPSWWWRedirectBehindTLSTerminationProxy(t *testing.T) {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Set(echo.HeaderXForwardedProto, "https")
|
|
|
|
res := redirectTest(HTTPSWWWRedirect, "labstack.com", header)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, res.Code)
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
|
2018-02-02 17:31:07 +02:00
|
|
|
res := redirectTest(HTTPSNonWWWRedirect, "www.labstack.com", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2017-02-23 01:57:12 +02:00
|
|
|
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
2016-09-28 19:26:02 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 17:31:07 +02:00
|
|
|
func TestRedirectHTTPSNonWWWRedirectBehindTLSTerminationProxy(t *testing.T) {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Set(echo.HeaderXForwardedProto, "https")
|
|
|
|
res := redirectTest(HTTPSNonWWWRedirect, "www.labstack.com", header)
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, res.Code)
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectWWWRedirect(t *testing.T) {
|
2018-02-02 17:31:07 +02:00
|
|
|
res := redirectTest(WWWRedirect, "labstack.com", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2017-02-23 01:57:12 +02:00
|
|
|
assert.Equal(t, "http://www.labstack.com/", 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) {
|
2018-02-02 17:31:07 +02:00
|
|
|
res := redirectTest(NonWWWRedirect, "www.labstack.com", nil)
|
2018-01-30 20:02:26 +02:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
|
|
assert.Equal(t, "http://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|