2017-12-28 21:24:34 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2018-08-15 16:35:27 +02:00
|
|
|
"io/ioutil"
|
2018-10-14 17:16:58 +02:00
|
|
|
"net/http"
|
2017-12-28 21:24:34 +02:00
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2017-12-28 21:24:34 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-08-28 21:47:02 +02:00
|
|
|
//Assert expected with url.EscapedPath method to obtain the path.
|
2017-12-28 21:24:34 +02:00
|
|
|
func TestRewrite(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(RewriteWithConfig(RewriteConfig{
|
|
|
|
Rules: map[string]string{
|
|
|
|
"/old": "/new",
|
|
|
|
"/api/*": "/$1",
|
|
|
|
"/js/*": "/public/javascripts/$1",
|
|
|
|
"/users/*/orders/*": "/user/$1/order/$2",
|
|
|
|
},
|
|
|
|
}))
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2017-12-28 21:24:34 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req.URL.Path = "/api/users"
|
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/users", req.URL.EscapedPath())
|
2017-12-28 21:24:34 +02:00
|
|
|
req.URL.Path = "/js/main.js"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2017-12-28 21:24:34 +02:00
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/public/javascripts/main.js", req.URL.EscapedPath())
|
2017-12-28 21:24:34 +02:00
|
|
|
req.URL.Path = "/old"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2017-12-28 21:24:34 +02:00
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/new", req.URL.EscapedPath())
|
2017-12-28 21:24:34 +02:00
|
|
|
req.URL.Path = "/users/jack/orders/1"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2017-12-28 21:24:34 +02:00
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/user/jack/order/1", req.URL.EscapedPath())
|
2018-06-25 14:45:59 +02:00
|
|
|
req.URL.Path = "/api/new users"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2018-06-25 14:45:59 +02:00
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/new%20users", req.URL.EscapedPath())
|
2020-08-23 05:18:31 +02:00
|
|
|
req.URL.Path = "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", req.URL.EscapedPath())
|
2020-08-23 05:18:31 +02:00
|
|
|
req.URL.Path = "/users/jill/orders/%%%%"
|
2020-08-28 21:47:02 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, rec.Code)
|
2017-12-28 21:24:34 +02:00
|
|
|
}
|
2018-03-16 16:05:22 +02:00
|
|
|
|
2018-03-16 19:00:59 +02:00
|
|
|
// Issue #1086
|
2018-03-16 16:05:22 +02:00
|
|
|
func TestEchoRewritePreMiddleware(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
r := e.Router()
|
|
|
|
|
|
|
|
// Rewrite old url to new one
|
|
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
|
|
Rules: map[string]string{
|
|
|
|
"/old": "/new",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Route
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/new", func(c echo.Context) error {
|
2020-08-23 05:18:31 +02:00
|
|
|
return c.NoContent(http.StatusOK)
|
2018-03-16 16:05:22 +02:00
|
|
|
})
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/old", nil)
|
2018-03-16 16:05:22 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/new", req.URL.EscapedPath())
|
2020-08-23 05:18:31 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2018-03-16 16:05:22 +02:00
|
|
|
}
|
2018-08-15 16:35:27 +02:00
|
|
|
|
|
|
|
// Issue #1143
|
|
|
|
func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
r := e.Router()
|
|
|
|
|
|
|
|
e.Pre(RewriteWithConfig(RewriteConfig{
|
|
|
|
Rules: map[string]string{
|
|
|
|
"/api/*/mgmt/proj/*/agt": "/api/$1/hosts/$2",
|
|
|
|
"/api/*/mgmt/proj": "/api/$1/eng",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/api/:version/hosts/:name", func(c echo.Context) error {
|
2020-08-23 05:18:31 +02:00
|
|
|
return c.String(http.StatusOK, "hosts")
|
2018-08-15 16:35:27 +02:00
|
|
|
})
|
2018-10-14 17:16:58 +02:00
|
|
|
r.Add(http.MethodGet, "/api/:version/eng", func(c echo.Context) error {
|
2020-08-23 05:18:31 +02:00
|
|
|
return c.String(http.StatusOK, "eng")
|
2018-08-15 16:35:27 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/mgmt/proj/test/agt", nil)
|
2018-08-15 16:35:27 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
2020-08-28 21:47:02 +02:00
|
|
|
assert.Equal(t, "/api/v1/hosts/test", req.URL.EscapedPath())
|
2020-08-23 05:18:31 +02:00
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
2018-08-15 16:35:27 +02:00
|
|
|
|
|
|
|
defer rec.Result().Body.Close()
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(rec.Result().Body)
|
|
|
|
assert.Equal(t, "hosts", string(bodyBytes))
|
|
|
|
}
|
|
|
|
}
|
2020-07-24 16:01:19 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|