mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
6d9e043284
This reintroduces support for Go modules, as v4. CloseNotifier() is removed as it has been obsoleted, see https://golang.org/doc/go1.11#net/http It was already NOT working (not sending signals) as of 1.11 the functionality was gone, we merely deleted the functions that exposed it. If anyone still relies on it they should migrate to using `c.Request().Context().Done()` instead. Closes #1268, #1255
90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type middlewareGenerator func() echo.MiddlewareFunc
|
|
|
|
func TestRedirectHTTPSRedirect(t *testing.T) {
|
|
res := redirectTest(HTTPSRedirect, "labstack.com", nil)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestRedirectHTTPSWWWRedirect(t *testing.T) {
|
|
res := redirectTest(HTTPSWWWRedirect, "labstack.com", nil)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
assert.Equal(t, "https://www.labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestRedirectHTTPSNonWWWRedirect(t *testing.T) {
|
|
res := redirectTest(HTTPSNonWWWRedirect, "www.labstack.com", nil)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
assert.Equal(t, "https://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func TestRedirectWWWRedirect(t *testing.T) {
|
|
res := redirectTest(WWWRedirect, "labstack.com", nil)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
assert.Equal(t, "http://www.labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
func TestRedirectNonWWWRedirect(t *testing.T) {
|
|
res := redirectTest(NonWWWRedirect, "www.labstack.com", nil)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, res.Code)
|
|
assert.Equal(t, "http://labstack.com/", res.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
func redirectTest(fn middlewareGenerator, host string, header http.Header) *httptest.ResponseRecorder {
|
|
e := echo.New()
|
|
next := func(c echo.Context) (err error) {
|
|
return c.NoContent(http.StatusOK)
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
req.Host = host
|
|
if header != nil {
|
|
req.Header = header
|
|
}
|
|
res := httptest.NewRecorder()
|
|
c := e.NewContext(req, res)
|
|
|
|
fn()(next)(c)
|
|
|
|
return res
|
|
}
|