1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-28 08:38:39 +02:00

New redirect middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-08-27 13:03:40 -07:00
parent 6dab126f64
commit 86ae297e23
5 changed files with 141 additions and 6 deletions

View File

@ -41,7 +41,10 @@ func TestJWT(t *testing.T) {
hdrCookie string // test.Request doesn't provide SetCookie(); use name=val
info string
}{
{expPanic: true, info: "No signing key provided"},
{
expPanic: true,
info: "No signing key provided",
},
{
expErrCode: http.StatusBadRequest,
config: JWTConfig{
@ -141,7 +144,6 @@ func TestJWT(t *testing.T) {
info: "Empty cookie",
},
} {
if tc.reqURL == "" {
tc.reqURL = "/"
}
@ -173,8 +175,8 @@ func TestJWT(t *testing.T) {
case jwt.MapClaims:
assert.Equal(t, claims["name"], "John Doe", tc.info)
case *jwtCustomClaims:
assert.Equal(t, claims.Name, "John Doe")
assert.Equal(t, claims.Admin, true)
assert.Equal(t, claims.Name, "John Doe", tc.info)
assert.Equal(t, claims.Admin, true, tc.info)
default:
panic("unexpected type of claims")
}

73
middleware/redirect.go Normal file
View File

@ -0,0 +1,73 @@
package middleware
import (
"net/http"
"github.com/labstack/echo"
)
// HTTPSRedirect redirects HTTP requests to HTTPS.
// For example, http://labstack.com will be redirect to https://labstack.com.
func HTTPSRedirect() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
host := req.Host()
uri := req.URI()
if !req.IsTLS() {
return c.Redirect(http.StatusMovedPermanently, "https://"+host+uri)
}
return next(c)
}
}
}
// HTTPSWWWRedirect redirects HTTP requests to WWW HTTPS.
// For example, http://labstack.com will be redirect to https://www.labstack.com.
func HTTPSWWWRedirect() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
host := req.Host()
uri := req.URI()
if !req.IsTLS() && host[:3] != "www" {
return c.Redirect(http.StatusMovedPermanently, "https://www."+host+uri)
}
return next(c)
}
}
}
// WWWRedirect redirects non WWW requests to WWW.
// For example, http://labstack.com will be redirect to http://www.labstack.com.
func WWWRedirect() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
scheme := req.Scheme()
host := req.Host()
if host[:3] != "www" {
uri := req.URI()
return c.Redirect(http.StatusMovedPermanently, scheme+"://www."+host+uri)
}
return next(c)
}
}
}
// NonWWWRedirect redirects WWW request to non WWW.
// For example, http://www.labstack.com will be redirect to http://labstack.com.
func NonWWWRedirect() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
req := c.Request()
scheme := req.Scheme()
host := req.Host()
if host[:3] == "www" {
uri := req.URI()
return c.Redirect(http.StatusMovedPermanently, scheme+"://"+host[4:]+uri)
}
return next(c)
}
}
}

View File

@ -0,0 +1,62 @@
package middleware
import (
"net/http"
"testing"
"github.com/labstack/echo"
"github.com/labstack/echo/test"
"github.com/stretchr/testify/assert"
)
func TestHTTPSRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req := test.NewRequest(echo.GET, "http://labstack.com", nil)
res := test.NewResponseRecorder()
c := e.NewContext(req, res)
HTTPSRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Status())
assert.Equal(t, "https://labstack.com", res.Header().Get(echo.HeaderLocation))
}
func TestHTTPSWWWRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req := test.NewRequest(echo.GET, "http://labstack.com", nil)
res := test.NewResponseRecorder()
c := e.NewContext(req, res)
HTTPSWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Status())
assert.Equal(t, "https://www.labstack.com", res.Header().Get(echo.HeaderLocation))
}
func TestWWWRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req := test.NewRequest(echo.GET, "http://labstack.com", nil)
res := test.NewResponseRecorder()
c := e.NewContext(req, res)
WWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Status())
assert.Equal(t, "http://www.labstack.com", res.Header().Get(echo.HeaderLocation))
}
func TestNonWWWRedirect(t *testing.T) {
e := echo.New()
next := func(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}
req := test.NewRequest(echo.GET, "http://www.labstack.com", nil)
res := test.NewResponseRecorder()
c := e.NewContext(req, res)
NonWWWRedirect()(next)(c)
assert.Equal(t, http.StatusMovedPermanently, res.Status())
assert.Equal(t, "http://labstack.com", res.Header().Get(echo.HeaderLocation))
}

View File

@ -69,5 +69,4 @@ func TestRemoveTrailingSlash(t *testing.T) {
})
h(c)
assert.Equal(t, "", req.URL().Path())
assert.Equal(t, "http://localhost", req.URI())
}

View File

@ -26,7 +26,6 @@ const (
func NewRequest(method, url string, body io.Reader) engine.Request {
r, _ := http.NewRequest(method, url, body)
r.RequestURI = url
return &Request{
request: r,
url: &URL{url: r.URL},