middleware/middleware_test.go

37 lines
842 B
Go
Raw Normal View History

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWrap(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/something/1/2", r.URL.Path)
})
mw1 := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path += "/1"
h.ServeHTTP(w, r)
})
}
mw2 := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path += "/2"
h.ServeHTTP(w, r)
})
}
server := httptest.NewServer(Wrap(handler, mw1, mw2))
defer server.Close()
res, err := http.Get(server.URL + "/something")
require.NoError(t, err)
assert.Equal(t, 200, res.StatusCode)
}