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"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectHTTPSRedirect(t *testing.T) {
|
2016-08-27 22:03:40 +02:00
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
|
|
|
|
res := httptest.NewRecorder()
|
2016-08-27 22:03:40 +02:00
|
|
|
c := e.NewContext(req, res)
|
|
|
|
HTTPSRedirect()(next)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2016-08-27 22:03:40 +02:00
|
|
|
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectHTTPSWWWRedirect(t *testing.T) {
|
2016-08-27 22:03:40 +02:00
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
|
|
|
|
res := httptest.NewRecorder()
|
2016-08-27 22:03:40 +02:00
|
|
|
c := e.NewContext(req, res)
|
|
|
|
HTTPSWWWRedirect()(next)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2016-08-27 22:03:40 +02:00
|
|
|
assert.Equal(t, "https://www.labstack.com", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:02 +02:00
|
|
|
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
|
|
|
req, _ := http.NewRequest(echo.GET, "http://www.labstack.com", nil)
|
|
|
|
res := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, res)
|
|
|
|
HTTPSNonWWWRedirect()(next)(c)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
|
|
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectWWWRedirect(t *testing.T) {
|
2016-08-27 22:03:40 +02:00
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "http://labstack.com", nil)
|
|
|
|
res := httptest.NewRecorder()
|
2016-08-27 22:03:40 +02:00
|
|
|
c := e.NewContext(req, res)
|
|
|
|
WWWRedirect()(next)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2016-08-27 22:03:40 +02:00
|
|
|
assert.Equal(t, "http://www.labstack.com", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
func TestRedirectNonWWWRedirect(t *testing.T) {
|
2016-08-27 22:03:40 +02:00
|
|
|
e := echo.New()
|
|
|
|
next := func(c echo.Context) (err error) {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
req, _ := http.NewRequest(echo.GET, "http://www.labstack.com", nil)
|
|
|
|
res := httptest.NewRecorder()
|
2016-08-27 22:03:40 +02:00
|
|
|
c := e.NewContext(req, res)
|
|
|
|
NonWWWRedirect()(next)(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
2016-08-27 22:03:40 +02:00
|
|
|
assert.Equal(t, "http://labstack.com", res.Header().Get(echo.HeaderLocation))
|
|
|
|
}
|