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

@ -4,15 +4,15 @@ import (
"net/http" "net/http"
) )
// ReplacedPathHeader is the default header to set the old path to
const ReplacedPathHeader = "X-Replaced-Path"
// ReplacePath is a middleware used to replace the path of a URL request // ReplacePath is a middleware used to replace the path of a URL request
type ReplacePath struct { type ReplacePath struct {
Handler http.Handler Handler http.Handler
Path string Path string
} }
// ReplacedPathHeader is the default header to set the old path to
const ReplacedPathHeader = "X-Replaced-Path"
func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *ReplacePath) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Header.Add(ReplacedPathHeader, r.URL.Path) r.Header.Add(ReplacedPathHeader, r.URL.Path)
r.URL.Path = s.Path r.URL.Path = s.Path

View file

@ -1,10 +1,10 @@
package middlewares_test package middlewares
import ( import (
"net/http" "net/http"
"testing" "testing"
"github.com/containous/traefik/middlewares" "github.com/stretchr/testify/assert"
) )
func TestReplacePath(t *testing.T) { func TestReplacePath(t *testing.T) {
@ -17,28 +17,22 @@ func TestReplacePath(t *testing.T) {
for _, path := range paths { for _, path := range paths {
t.Run(path, func(t *testing.T) { t.Run(path, func(t *testing.T) {
var newPath, oldPath string
handler := &middlewares.ReplacePath{ var expectedPath, actualHeader string
handler := &ReplacePath{
Path: replacementPath, Path: replacementPath,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newPath = r.URL.Path expectedPath = r.URL.Path
oldPath = r.Header.Get("X-Replaced-Path") actualHeader = r.Header.Get(ReplacedPathHeader)
}), }),
} }
req, err := http.NewRequest("GET", "http://localhost"+path, nil) req, err := http.NewRequest("GET", "http://localhost"+path, nil)
if err != nil { assert.NoError(t, err, "%s: unexpected error.", path)
t.Error(err)
}
handler.ServeHTTP(nil, req) handler.ServeHTTP(nil, req)
if newPath != replacementPath { assert.Equal(t, expectedPath, replacementPath, "%s: unexpected path.", path)
t.Fatalf("new path should be '%s'", replacementPath) assert.Equal(t, path, actualHeader, "%s: unexpected '%s' header.", path, ReplacedPathHeader)
}
if oldPath != path {
t.Fatalf("old path should be '%s'", path)
}
}) })
} }
} }

View file

@ -5,9 +5,8 @@ import (
"strings" "strings"
) )
const ( // ForwardedPrefixHeader is the default header to set prefix
forwardedPrefixHeader = "X-Forwarded-Prefix" const ForwardedPrefixHeader = "X-Forwarded-Prefix"
)
// StripPrefix is a middleware used to strip prefix from an URL request // StripPrefix is a middleware used to strip prefix from an URL request
type StripPrefix struct { type StripPrefix struct {
@ -35,7 +34,7 @@ func (s *StripPrefix) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (s *StripPrefix) serveRequest(w http.ResponseWriter, r *http.Request, prefix string) { func (s *StripPrefix) serveRequest(w http.ResponseWriter, r *http.Request, prefix string) {
r.Header[forwardedPrefixHeader] = []string{prefix} r.Header.Add(ForwardedPrefixHeader, prefix)
r.RequestURI = r.URL.RequestURI() r.RequestURI = r.URL.RequestURI()
s.Handler.ServeHTTP(w, r) s.Handler.ServeHTTP(w, r)
} }

View file

@ -40,7 +40,7 @@ func (s *StripPrefixRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
r.URL.Path = r.URL.Path[len(prefix.Path):] r.URL.Path = r.URL.Path[len(prefix.Path):]
r.Header[forwardedPrefixHeader] = []string{prefix.Path} r.Header.Add(ForwardedPrefixHeader, prefix.Path)
r.RequestURI = r.URL.RequestURI() r.RequestURI = r.URL.RequestURI()
s.Handler.ServeHTTP(w, r) s.Handler.ServeHTTP(w, r)
return return

View file

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

View file

@ -16,6 +16,7 @@ func TestStripPrefix(t *testing.T) {
path string path string
expectedStatusCode int expectedStatusCode int
expectedPath string expectedPath string
expectedHeader string
}{ }{
{ {
desc: "no prefixes configured", desc: "no prefixes configured",
@ -29,6 +30,7 @@ func TestStripPrefix(t *testing.T) {
path: "/", path: "/",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/", expectedPath: "/",
expectedHeader: "/",
}, },
{ {
desc: "prefix and path matching", desc: "prefix and path matching",
@ -36,6 +38,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat", path: "/stat",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/", expectedPath: "/",
expectedHeader: "/stat",
}, },
{ {
desc: "path prefix on exactly matching path", desc: "path prefix on exactly matching path",
@ -43,6 +46,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/", path: "/stat/",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/", expectedPath: "/",
expectedHeader: "/stat/",
}, },
{ {
desc: "path prefix on matching longer path", desc: "path prefix on matching longer path",
@ -50,6 +54,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/us", path: "/stat/us",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/us", expectedPath: "/us",
expectedHeader: "/stat/",
}, },
{ {
desc: "path prefix on mismatching path", desc: "path prefix on mismatching path",
@ -63,6 +68,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/", path: "/stat/",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/", expectedPath: "/",
expectedHeader: "/stat",
}, },
{ {
desc: "earlier prefix matching", desc: "earlier prefix matching",
@ -70,6 +76,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/us", path: "/stat/us",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/us", expectedPath: "/us",
expectedHeader: "/stat",
}, },
{ {
desc: "later prefix matching", desc: "later prefix matching",
@ -77,6 +84,7 @@ func TestStripPrefix(t *testing.T) {
path: "/stat", path: "/stat",
expectedStatusCode: http.StatusOK, expectedStatusCode: http.StatusOK,
expectedPath: "/", expectedPath: "/",
expectedHeader: "/stat",
}, },
} }
@ -84,20 +92,23 @@ func TestStripPrefix(t *testing.T) {
test := test test := test
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
t.Parallel() t.Parallel()
var gotPath string
var actualPath, actualHeader string
server := httptest.NewServer(&StripPrefix{ server := httptest.NewServer(&StripPrefix{
Prefixes: test.prefixes, Prefixes: test.prefixes,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path actualPath = r.URL.Path
actualHeader = r.Header.Get(ForwardedPrefixHeader)
}), }),
}) })
defer server.Close() defer server.Close()
resp, err := http.Get(server.URL + test.path) resp, err := http.Get(server.URL + test.path)
require.NoError(t, err, "Failed to send GET request") require.NoError(t, err, "%s: failed to send GET request.", test.desc)
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "Unexpected status code")
assert.Equal(t, test.expectedPath, gotPath, "Unexpected path") assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "%s: unexpected status code.", test.desc)
assert.Equal(t, test.expectedPath, actualPath, "%s: unexpected path.", test.desc)
assert.Equal(t, test.expectedHeader, actualHeader, "%s: unexpected '%s' header.", test.desc, ForwardedPrefixHeader)
}) })
} }
} }

View file

@ -3,10 +3,11 @@ package server
import ( import (
"net/http" "net/http"
"net/url" "net/url"
"reflect"
"testing" "testing"
"github.com/containous/mux" "github.com/containous/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestParseOneRule(t *testing.T) { func TestParseOneRule(t *testing.T) {
@ -17,17 +18,12 @@ func TestParseOneRule(t *testing.T) {
expression := "Host:foo.bar" expression := "Host:foo.bar"
routeResult, err := rules.Parse(expression) routeResult, err := rules.Parse(expression)
require.NoError(t, err, "Error while building route for %s", expression)
if err != nil {
t.Fatalf("Error while building route for Host:foo.bar: %s", err)
}
request, err := http.NewRequest("GET", "http://foo.bar", nil) request, err := http.NewRequest("GET", "http://foo.bar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult}) routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if !routeMatch { assert.True(t, routeMatch, "Rule %s don't match.", expression)
t.Fatalf("Rule Host:foo.bar don't match: %s", err)
}
} }
func TestParseTwoRules(t *testing.T) { func TestParseTwoRules(t *testing.T) {
@ -39,47 +35,54 @@ func TestParseTwoRules(t *testing.T) {
expression := "Host: Foo.Bar ; Path:/FOObar" expression := "Host: Foo.Bar ; Path:/FOObar"
routeResult, err := rules.Parse(expression) routeResult, err := rules.Parse(expression)
if err != nil { require.NoError(t, err, "Error while building route for %s.", expression)
t.Fatalf("Error while building route for Host:foo.bar;Path:/FOObar: %s", err)
}
request, err := http.NewRequest("GET", "http://foo.bar/foobar", nil) request, _ := http.NewRequest("GET", "http://foo.bar/foobar", nil)
routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult}) routeMatch := routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if routeMatch { assert.False(t, routeMatch, "Rule %s don't match.", expression)
t.Fatalf("Rule Host:foo.bar;Path:/FOObar don't match: %s", err)
}
request, err = http.NewRequest("GET", "http://foo.bar/FOObar", nil) request, _ = http.NewRequest("GET", "http://foo.bar/FOObar", nil)
routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult}) routeMatch = routeResult.Match(request, &mux.RouteMatch{Route: routeResult})
if !routeMatch { assert.True(t, routeMatch, "Rule %s don't match.", expression)
t.Fatalf("Rule Host:foo.bar;Path:/FOObar don't match: %s", err)
}
} }
func TestParseDomains(t *testing.T) { func TestParseDomains(t *testing.T) {
rules := &Rules{} rules := &Rules{}
expressionsSlice := []string{
"Host:foo.bar,test.bar", tests := []struct {
"Path:/test", expression string
"Host:foo.bar;Path:/test", domain []string
"Host: Foo.Bar ;Path:/test", }{
} {
domainsSlice := [][]string{ expression: "Host:foo.bar,test.bar",
{"foo.bar", "test.bar"}, domain: []string{"foo.bar", "test.bar"},
{}, },
{"foo.bar"}, {
{"foo.bar"}, expression: "Path:/test",
} domain: []string{},
for i, expression := range expressionsSlice { },
domains, err := rules.ParseDomains(expression) {
if err != nil { expression: "Host:foo.bar;Path:/test",
t.Fatalf("Error while parsing domains: %v", err) domain: []string{"foo.bar"},
} },
if !reflect.DeepEqual(domains, domainsSlice[i]) { {
t.Fatalf("Error parsing domains: expected %+v, got %+v", domainsSlice[i], domains) expression: "Host: Foo.Bar ;Path:/test",
domain: []string{"foo.bar"},
},
} }
for _, test := range tests {
test := test
t.Run(test.expression, func(t *testing.T) {
t.Parallel()
domains, err := rules.ParseDomains(test.expression)
require.NoError(t, err, "%s: Error while parsing domain.", test.expression)
assert.EqualValues(t, test.domain, domains, "%s: Error parsing domains from expression.", test.expression)
})
} }
} }
@ -87,70 +90,49 @@ func TestPriorites(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
router.StrictSlash(true) router.StrictSlash(true)
rules := &Rules{route: &serverRoute{route: router.NewRoute()}} rules := &Rules{route: &serverRoute{route: router.NewRoute()}}
routeFoo, err := rules.Parse("PathPrefix:/foo") expression01 := "PathPrefix:/foo"
if err != nil {
t.Fatalf("Error while building route for PathPrefix:/foo: %s", err) routeFoo, err := rules.Parse(expression01)
} require.NoError(t, err, "Error while building route for %s", expression01)
fooHandler := &fakeHandler{name: "fooHandler"} fooHandler := &fakeHandler{name: "fooHandler"}
routeFoo.Handler(fooHandler) routeFoo.Handler(fooHandler)
if !router.Match(&http.Request{URL: &url.URL{ routeMatch := router.Match(&http.Request{URL: &url.URL{Path: "/foo"}}, &mux.RouteMatch{})
Path: "/foo", assert.True(t, routeMatch, "Error matching route")
}}, &mux.RouteMatch{}) {
t.Fatal("Error matching route")
}
if router.Match(&http.Request{URL: &url.URL{ routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/fo"}}, &mux.RouteMatch{})
Path: "/fo", assert.False(t, routeMatch, "Error matching route")
}}, &mux.RouteMatch{}) {
t.Fatal("Error matching route")
}
multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}} multipleRules := &Rules{route: &serverRoute{route: router.NewRoute()}}
routeFoobar, err := multipleRules.Parse("PathPrefix:/foobar") expression02 := "PathPrefix:/foobar"
if err != nil {
t.Fatalf("Error while building route for PathPrefix:/foobar: %s", err) routeFoobar, err := multipleRules.Parse(expression02)
} require.NoError(t, err, "Error while building route for %s", expression02)
foobarHandler := &fakeHandler{name: "foobarHandler"} foobarHandler := &fakeHandler{name: "foobarHandler"}
routeFoobar.Handler(foobarHandler) routeFoobar.Handler(foobarHandler)
if !router.Match(&http.Request{URL: &url.URL{ routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foo"}}, &mux.RouteMatch{})
Path: "/foo",
}}, &mux.RouteMatch{}) { assert.True(t, routeMatch, "Error matching route")
t.Fatal("Error matching route")
}
fooMatcher := &mux.RouteMatch{} fooMatcher := &mux.RouteMatch{}
if !router.Match(&http.Request{URL: &url.URL{ routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foobar"}}, fooMatcher)
Path: "/foobar",
}}, fooMatcher) {
t.Fatal("Error matching route")
}
if fooMatcher.Handler == foobarHandler { assert.True(t, routeMatch, "Error matching route")
t.Fatal("Error matching priority") assert.NotEqual(t, fooMatcher.Handler, foobarHandler, "Error matching priority")
} assert.Equal(t, fooMatcher.Handler, fooHandler, "Error matching priority")
if fooMatcher.Handler != fooHandler {
t.Fatal("Error matching priority")
}
routeFoo.Priority(1) routeFoo.Priority(1)
routeFoobar.Priority(10) routeFoobar.Priority(10)
router.SortRoutes() router.SortRoutes()
foobarMatcher := &mux.RouteMatch{} foobarMatcher := &mux.RouteMatch{}
if !router.Match(&http.Request{URL: &url.URL{ routeMatch = router.Match(&http.Request{URL: &url.URL{Path: "/foobar"}}, foobarMatcher)
Path: "/foobar",
}}, foobarMatcher) {
t.Fatal("Error matching route")
}
if foobarMatcher.Handler != foobarHandler { assert.True(t, routeMatch, "Error matching route")
t.Fatal("Error matching priority") assert.Equal(t, foobarMatcher.Handler, foobarHandler, "Error matching priority")
} assert.NotEqual(t, foobarMatcher.Handler, fooHandler, "Error matching priority")
if foobarMatcher.Handler == fooHandler {
t.Fatal("Error matching priority")
}
} }
type fakeHandler struct { type fakeHandler struct {