refactor: Enhance rules tests.

- refactor: change incorrect package.
- refactor: test readability.
This commit is contained in:
Fernandez Ludovic 2017-05-28 05:50:03 +02:00 committed by Ludovic Fernandez
parent cbccdd51c5
commit b6c5c14447
7 changed files with 171 additions and 150 deletions

View file

@ -1,55 +1,90 @@
package middlewares
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStripPrefixRegex(t *testing.T) {
handlerPath := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, r.URL.Path)
})
handler := NewStripPrefixRegex(handlerPath, []string{"/a/api/", "/b/{regex}/", "/c/{category}/{id:[0-9]+}/"})
server := httptest.NewServer(handler)
defer server.Close()
testPrefixRegex := []string{"/a/api/", "/b/{regex}/", "/c/{category}/{id:[0-9]+}/"}
tests := []struct {
expectedCode int
expectedResponse string
url string
path string
expectedStatusCode int
expectedPath string
expectedHeader string
}{
{url: "/a/test", expectedCode: 404, expectedResponse: "404 page not found\n"},
{url: "/a/api/test", expectedCode: 200, expectedResponse: "test"},
{url: "/b/api/", expectedCode: 200, expectedResponse: ""},
{url: "/b/api/test1", expectedCode: 200, expectedResponse: "test1"},
{url: "/b/api2/test2", expectedCode: 200, expectedResponse: "test2"},
{url: "/c/api/123/", expectedCode: 200, expectedResponse: ""},
{url: "/c/api/123/test3", expectedCode: 200, expectedResponse: "test3"},
{url: "/c/api/abc/test4", expectedCode: 404, expectedResponse: "404 page not found\n"},
{
path: "/a/test",
expectedStatusCode: 404,
},
{
path: "/a/api/test",
expectedStatusCode: 200,
expectedPath: "test",
expectedHeader: "/a/api/",
},
{
path: "/b/api/",
expectedStatusCode: 200,
expectedHeader: "/b/api/",
},
{
path: "/b/api/test1",
expectedStatusCode: 200,
expectedPath: "test1",
expectedHeader: "/b/api/",
},
{
path: "/b/api2/test2",
expectedStatusCode: 200,
expectedPath: "test2",
expectedHeader: "/b/api2/",
},
{
path: "/c/api/123/",
expectedStatusCode: 200,
expectedHeader: "/c/api/123/",
},
{
path: "/c/api/123/test3",
expectedStatusCode: 200,
expectedPath: "test3",
expectedHeader: "/c/api/123/",
},
{
path: "/c/api/abc/test4",
expectedStatusCode: 404,
},
}
for _, test := range tests {
resp, err := http.Get(server.URL + test.url)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != test.expectedCode {
t.Fatalf("Received non-%d response: %d\n", test.expectedCode, resp.StatusCode)
}
response, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if test.expectedResponse != string(response) {
t.Errorf("Expected '%s' : '%s'\n", test.expectedResponse, response)
}
test := test
t.Run(test.path, func(t *testing.T) {
t.Parallel()
var actualPath, actualHeader string
handlerPath := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
actualPath = r.URL.Path
actualHeader = r.Header.Get(ForwardedPrefixHeader)
})
handler := NewStripPrefixRegex(handlerPath, testPrefixRegex)
server := httptest.NewServer(handler)
defer server.Close()
resp, err := http.Get(server.URL + test.path)
require.NoError(t, err, "%s: unexpected error.", test.path)
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.path)
assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.path)
assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.path, ForwardedPrefixHeader)
})
}
}