mirror of
https://github.com/labstack/echo.git
synced 2024-12-18 16:20:53 +02:00
da083ffd0a
Signed-off-by: Vishal Rana <vr@labstack.com>
36 lines
906 B
Go
36 lines
906 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
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",
|
|
},
|
|
}))
|
|
req := httptest.NewRequest(echo.GET, "/", nil)
|
|
rec := httptest.NewRecorder()
|
|
req.URL.Path = "/api/users"
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/users", req.URL.Path)
|
|
req.URL.Path = "/js/main.js"
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/public/javascripts/main.js", req.URL.Path)
|
|
req.URL.Path = "/old"
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/new", req.URL.Path)
|
|
req.URL.Path = "/users/jack/orders/1"
|
|
e.ServeHTTP(rec, req)
|
|
assert.Equal(t, "/user/jack/order/1", req.URL.Path)
|
|
}
|