mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +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
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAddTrailingSlash(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/add-slash", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
h := AddTrailingSlash()(func(c echo.Context) error {
|
|
return nil
|
|
})
|
|
h(c)
|
|
|
|
assert := assert.New(t)
|
|
assert.Equal("/add-slash/", req.URL.Path)
|
|
assert.Equal("/add-slash/", req.RequestURI)
|
|
|
|
// With config
|
|
req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
|
|
RedirectCode: http.StatusMovedPermanently,
|
|
})(func(c echo.Context) error {
|
|
return nil
|
|
})
|
|
h(c)
|
|
assert.Equal(http.StatusMovedPermanently, rec.Code)
|
|
assert.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
|
|
}
|
|
|
|
func TestRemoveTrailingSlash(t *testing.T) {
|
|
e := echo.New()
|
|
req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil)
|
|
rec := httptest.NewRecorder()
|
|
c := e.NewContext(req, rec)
|
|
h := RemoveTrailingSlash()(func(c echo.Context) error {
|
|
return nil
|
|
})
|
|
h(c)
|
|
|
|
assert := assert.New(t)
|
|
|
|
assert.Equal("/remove-slash", req.URL.Path)
|
|
assert.Equal("/remove-slash", req.RequestURI)
|
|
|
|
// With config
|
|
req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
|
|
RedirectCode: http.StatusMovedPermanently,
|
|
})(func(c echo.Context) error {
|
|
return nil
|
|
})
|
|
h(c)
|
|
assert.Equal(http.StatusMovedPermanently, rec.Code)
|
|
assert.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
|
|
|
|
// With bare URL
|
|
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
|
rec = httptest.NewRecorder()
|
|
c = e.NewContext(req, rec)
|
|
h = RemoveTrailingSlash()(func(c echo.Context) error {
|
|
return nil
|
|
})
|
|
h(c)
|
|
assert.Equal("", req.URL.Path)
|
|
}
|