mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
6ef5f77bf2
WIP: make default logger implemented custom writer for jsonlike logs WIP: improve examples WIP: defaultErrorHandler use errors.As to unwrap errors. Update readme WIP: default logger logs json, restore e.Start method WIP: clean router.Match a bit WIP: func types/fields have echo.Context has first element WIP: remove yaml tags as functions etc can not be serialized anyway WIP: change BindPathParams,BindQueryParams,BindHeaders from methods to functions and reverse arguments to be like DefaultBinder.Bind is WIP: improved comments, logger now extracts status from error WIP: go mod tidy WIP: rebase with 4.5.0 WIP: * removed todos. * removed StartAutoTLS and StartH2CServer methods from `StartConfig` * KeyAuth middleware errorhandler can swallow the error and resume next middleware WIP: add RouterConfig.UseEscapedPathForMatching to use escaped path for matching request against routes WIP: FIXMEs WIP: upgrade golang-jwt/jwt to `v4` WIP: refactor http methods to return RouteInfo WIP: refactor static not creating multiple routes WIP: refactor route and middleware adding functions not to return error directly WIP: Use 401 for problematic/missing headers for key auth and JWT middleware (#1552, #1402). > In summary, a 401 Unauthorized response should be used for missing or bad authentication WIP: replace `HTTPError.SetInternal` with `HTTPError.WithInternal` so we could not mutate global error variables WIP: add RouteInfo and RouteMatchType into Context what we could know from in middleware what route was matched and/or type of that match (200/404/405) WIP: make notFoundHandler and methodNotAllowedHandler private. encourage that all errors be handled in Echo.HTTPErrorHandler WIP: server cleanup ideas WIP: routable.ForGroup WIP: note about logger middleware WIP: bind should not default values on second try. use crypto rand for better randomness WIP: router add route as interface and returns info as interface WIP: improve flaky test (remains still flaky) WIP: add notes about bind default values WIP: every route can have their own path params names WIP: routerCreator and different tests WIP: different things WIP: remove route implementation WIP: support custom method types WIP: extractor tests WIP: v5.0.x proposal over v4.4.0
315 lines
8.9 KiB
Go
315 lines
8.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestRewriteAfterRouting(t *testing.T) {
|
|
e := echo.New()
|
|
// middlewares added with `Use()` are executed after routing is done and do not affect which route handler is matched
|
|
e.Use(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{
|
|
"/old": "/new",
|
|
"/api/*": "/$1",
|
|
"/js/*": "/public/javascripts/$1",
|
|
"/users/*/orders/*": "/user/$1/order/$2",
|
|
},
|
|
}))
|
|
e.GET("/public/*", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, c.PathParam("*"))
|
|
})
|
|
e.GET("/*", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, c.PathParam("*"))
|
|
})
|
|
|
|
var testCases = []struct {
|
|
whenPath string
|
|
expectRoutePath string
|
|
expectRequestPath string
|
|
expectRequestRawPath string
|
|
}{
|
|
{
|
|
whenPath: "/api/users",
|
|
expectRoutePath: "api/users",
|
|
expectRequestPath: "/users",
|
|
expectRequestRawPath: "",
|
|
},
|
|
{
|
|
whenPath: "/js/main.js",
|
|
expectRoutePath: "js/main.js",
|
|
expectRequestPath: "/public/javascripts/main.js",
|
|
expectRequestRawPath: "",
|
|
},
|
|
{
|
|
whenPath: "/users/jack/orders/1",
|
|
expectRoutePath: "users/jack/orders/1",
|
|
expectRequestPath: "/user/jack/order/1",
|
|
expectRequestRawPath: "",
|
|
},
|
|
{ // no rewrite rule matched. already encoded URL should not be double encoded or changed in any way
|
|
whenPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
|
|
expectRoutePath: "user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
|
|
expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
|
|
expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
|
|
},
|
|
{ // just rewrite but do not touch encoding. already encoded URL should not be double encoded
|
|
whenPath: "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
|
|
expectRoutePath: "users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
|
|
expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
|
|
expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
|
|
},
|
|
{ // ` ` (space) is encoded by httpClient to `%20` when doing request to Echo. `%20` should not be double escaped or changed in any way when rewriting request
|
|
whenPath: "/api/new users",
|
|
expectRoutePath: "api/new users",
|
|
expectRequestPath: "/new users",
|
|
expectRequestRawPath: "",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.whenPath, func(t *testing.T) {
|
|
target, _ := url.Parse(tc.whenPath)
|
|
req := httptest.NewRequest(http.MethodGet, target.String(), nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, tc.expectRoutePath, rec.Body.String())
|
|
assert.Equal(t, tc.expectRequestPath, req.URL.Path)
|
|
assert.Equal(t, tc.expectRequestRawPath, req.URL.RawPath)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMustRewriteWithConfig_emptyRulesPanics(t *testing.T) {
|
|
assert.Panics(t, func() {
|
|
RewriteWithConfig(RewriteConfig{})
|
|
})
|
|
}
|
|
|
|
func TestMustRewriteWithConfig_skipper(t *testing.T) {
|
|
var testCases = []struct {
|
|
name string
|
|
givenSkipper func(c echo.Context) bool
|
|
whenURL string
|
|
expectURL string
|
|
expectStatus int
|
|
}{
|
|
{
|
|
name: "not skipped",
|
|
whenURL: "/old",
|
|
expectURL: "/new",
|
|
expectStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "skipped",
|
|
givenSkipper: func(c echo.Context) bool {
|
|
return true
|
|
},
|
|
whenURL: "/old",
|
|
expectURL: "/old",
|
|
expectStatus: http.StatusNotFound,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
e.Pre(RewriteWithConfig(
|
|
RewriteConfig{
|
|
Skipper: tc.givenSkipper,
|
|
Rules: map[string]string{"/old": "/new"}},
|
|
))
|
|
|
|
e.GET("/new", func(c echo.Context) error {
|
|
return c.NoContent(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, tc.whenURL, nil)
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, tc.expectURL, req.URL.EscapedPath())
|
|
assert.Equal(t, tc.expectStatus, rec.Code)
|
|
})
|
|
}
|
|
}
|
|
|
|
// Issue #1086
|
|
func TestEchoRewritePreMiddleware(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
// Rewrite old url to new one
|
|
// middlewares added with `Pre()` are executed before routing is done and therefore change which handler matches
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{"/old": "/new"}}),
|
|
)
|
|
|
|
// Route
|
|
e.Add(http.MethodGet, "/new", func(c echo.Context) error {
|
|
return c.NoContent(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/old", nil)
|
|
rec := httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/new", req.URL.EscapedPath())
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
}
|
|
|
|
// Issue #1143
|
|
func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
// middlewares added with `Pre()` are executed before routing is done and therefore change which handler matches
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{
|
|
"/api/*/mgmt/proj/*/agt": "/api/$1/hosts/$2",
|
|
"/api/*/mgmt/proj": "/api/$1/eng",
|
|
},
|
|
}))
|
|
|
|
e.Add(http.MethodGet, "/api/:version/hosts/:name", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "hosts")
|
|
})
|
|
e.Add(http.MethodGet, "/api/:version/eng", func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "eng")
|
|
})
|
|
|
|
for i := 0; i < 100; i++ {
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/mgmt/proj/test/agt", nil)
|
|
rec := httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/api/v1/hosts/test", req.URL.EscapedPath())
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
defer rec.Result().Body.Close()
|
|
bodyBytes, _ := ioutil.ReadAll(rec.Result().Body)
|
|
assert.Equal(t, "hosts", string(bodyBytes))
|
|
}
|
|
}
|
|
|
|
// Issue #1573
|
|
func TestEchoRewriteWithCaret(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{
|
|
"^/abc/*": "/v1/abc/$1",
|
|
},
|
|
}))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
var req *http.Request
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/abc/test", nil)
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/v1/abc/test", req.URL.Path)
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/v1/abc/test", nil)
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/v1/abc/test", req.URL.Path)
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/v2/abc/test", nil)
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/v2/abc/test", req.URL.Path)
|
|
}
|
|
|
|
// Verify regex used with rewrite
|
|
func TestEchoRewriteWithRegexRules(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{
|
|
"^/a/*": "/v1/$1",
|
|
"^/b/*/c/*": "/v2/$2/$1",
|
|
"^/c/*/*": "/v3/$2",
|
|
},
|
|
RegexRules: map[*regexp.Regexp]string{
|
|
regexp.MustCompile("^/x/.+?/(.*)"): "/v4/$1",
|
|
regexp.MustCompile("^/y/(.+?)/(.*)"): "/v5/$2/$1",
|
|
},
|
|
}))
|
|
|
|
var rec *httptest.ResponseRecorder
|
|
var req *http.Request
|
|
|
|
testCases := []struct {
|
|
requestPath string
|
|
expectPath string
|
|
}{
|
|
{"/unmatched", "/unmatched"},
|
|
{"/a/test", "/v1/test"},
|
|
{"/b/foo/c/bar/baz", "/v2/bar/baz/foo"},
|
|
{"/c/ignore/test", "/v3/test"},
|
|
{"/c/ignore1/test/this", "/v3/test/this"},
|
|
{"/x/ignore/test", "/v4/test"},
|
|
{"/y/foo/bar", "/v5/bar/foo"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.requestPath, func(t *testing.T) {
|
|
req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
|
|
rec = httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, tc.expectPath, req.URL.EscapedPath())
|
|
})
|
|
}
|
|
}
|
|
|
|
// Ensure correct escaping as defined in replacement (issue #1798)
|
|
func TestEchoRewriteReplacementEscaping(t *testing.T) {
|
|
e := echo.New()
|
|
|
|
// NOTE: these are incorrect regexps as they do not factor in that URI we are replacing could contain ? (query) and # (fragment) parts
|
|
// so in reality they append query and fragment part as `$1` matches everything after that prefix
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
Rules: map[string]string{
|
|
"^/a/*": "/$1?query=param",
|
|
"^/b/*": "/$1;part#one",
|
|
},
|
|
RegexRules: map[*regexp.Regexp]string{
|
|
regexp.MustCompile("^/x/(.*)"): "/$1?query=param",
|
|
regexp.MustCompile("^/y/(.*)"): "/$1;part#one",
|
|
regexp.MustCompile("^/z/(.*)"): "/$1?test=1#escaped%20test",
|
|
},
|
|
}))
|
|
|
|
var rec *httptest.ResponseRecorder
|
|
var req *http.Request
|
|
|
|
testCases := []struct {
|
|
requestPath string
|
|
expect string
|
|
}{
|
|
{"/unmatched", "/unmatched"},
|
|
{"/a/test", "/test?query=param"},
|
|
{"/b/foo/bar", "/foo/bar;part#one"},
|
|
{"/x/test", "/test?query=param"},
|
|
{"/y/foo/bar", "/foo/bar;part#one"},
|
|
{"/z/foo/b%20ar", "/foo/b%20ar?test=1#escaped%20test"},
|
|
{"/z/foo/b%20ar?nope=1#yes", "/foo/b%20ar?nope=1#yes?test=1%23escaped%20test"}, // example of appending
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.requestPath, func(t *testing.T) {
|
|
req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
|
|
rec = httptest.NewRecorder()
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, tc.expect, req.URL.String())
|
|
})
|
|
}
|
|
}
|