2017-05-28 03:50:03 +00:00
|
|
|
package middlewares
|
2017-04-25 18:13:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-28 03:50:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-06-01 20:09:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-25 18:13:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestReplacePath(t *testing.T) {
|
|
|
|
const replacementPath = "/replacement-path"
|
|
|
|
|
|
|
|
paths := []string{
|
|
|
|
"/example",
|
|
|
|
"/some/really/long/path",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
t.Run(path, func(t *testing.T) {
|
2017-05-28 03:50:03 +00:00
|
|
|
|
|
|
|
var expectedPath, actualHeader string
|
|
|
|
handler := &ReplacePath{
|
2017-04-25 18:13:39 +00:00
|
|
|
Path: replacementPath,
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-05-28 03:50:03 +00:00
|
|
|
expectedPath = r.URL.Path
|
|
|
|
actualHeader = r.Header.Get(ReplacedPathHeader)
|
2017-04-25 18:13:39 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
2017-06-01 20:09:36 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost"+path, nil)
|
|
|
|
require.NoError(t, err, "%s: unexpected error.", path)
|
2017-04-25 18:13:39 +00:00
|
|
|
|
|
|
|
handler.ServeHTTP(nil, req)
|
2017-06-01 20:09:36 +00:00
|
|
|
assert.Equal(t, expectedPath, replacementPath, "Unexpected path.")
|
|
|
|
assert.Equal(t, path, actualHeader, "Unexpected '%s' header.", ReplacedPathHeader)
|
2017-04-25 18:13:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|