Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance rules tests. #1679

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ BIND_DIR := "dist"
TRAEFIK_MOUNT := -v "$(CURDIR)/$(BIND_DIR):/go/src/github.com/containous/traefik/$(BIND_DIR)"

GIT_BRANCH := $(subst heads/,,$(shell git rev-parse --abbrev-ref HEAD 2>/dev/null))
TRAEFIK_DEV_IMAGE := traefik-dev$(if $(GIT_BRANCH),:$(GIT_BRANCH))
TRAEFIK_DEV_IMAGE := traefik-dev$(if $(GIT_BRANCH),:$(subst /,-,$(GIT_BRANCH)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this one has bothered me before!

REPONAME := $(shell echo $(REPO) | tr '[:upper:]' '[:lower:]')
TRAEFIK_IMAGE := $(if $(REPONAME),$(REPONAME),"containous/traefik")
INTEGRATION_OPTS := $(if $(MAKE_DOCKER_HOST),-e "DOCKER_HOST=$(MAKE_DOCKER_HOST)", -v "/var/run/docker.sock:/var/run/docker.sock")
Expand Down
6 changes: 3 additions & 3 deletions middlewares/replace_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"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
type ReplacePath struct {
Handler http.Handler
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) {
r.Header.Add(ReplacedPathHeader, r.URL.Path)
r.URL.Path = s.Path
Expand Down
26 changes: 10 additions & 16 deletions middlewares/replace_path_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package middlewares_test
package middlewares

import (
"net/http"
"testing"

"github.com/containous/traefik/middlewares"
"github.com/stretchr/testify/assert"
)

func TestReplacePath(t *testing.T) {
Expand All @@ -17,28 +17,22 @@ func TestReplacePath(t *testing.T) {

for _, path := range paths {
t.Run(path, func(t *testing.T) {
var newPath, oldPath string
handler := &middlewares.ReplacePath{

var expectedPath, actualHeader string
handler := &ReplacePath{
Path: replacementPath,
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
newPath = r.URL.Path
oldPath = r.Header.Get("X-Replaced-Path")
expectedPath = r.URL.Path
actualHeader = r.Header.Get(ReplacedPathHeader)
}),
}

req, err := http.NewRequest("GET", "http://localhost"+path, nil)
if err != nil {
t.Error(err)
}
assert.NoError(t, err, "%s: unexpected error.", path)

handler.ServeHTTP(nil, req)
if newPath != replacementPath {
t.Fatalf("new path should be '%s'", replacementPath)
}

if oldPath != path {
t.Fatalf("old path should be '%s'", path)
}
assert.Equal(t, expectedPath, replacementPath, "%s: unexpected path.", path)
assert.Equal(t, path, actualHeader, "%s: unexpected '%s' header.", path, ReplacedPathHeader)
})
}
}
7 changes: 3 additions & 4 deletions middlewares/stripPrefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import (
"strings"
)

const (
forwardedPrefixHeader = "X-Forwarded-Prefix"
)
// ForwardedPrefixHeader is the default header to set prefix
const ForwardedPrefixHeader = "X-Forwarded-Prefix"

// StripPrefix is a middleware used to strip prefix from an URL request
type StripPrefix struct {
Expand Down Expand Up @@ -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) {
r.Header[forwardedPrefixHeader] = []string{prefix}
r.Header.Add(ForwardedPrefixHeader, prefix)
r.RequestURI = r.URL.RequestURI()
s.Handler.ServeHTTP(w, r)
}
Expand Down
2 changes: 1 addition & 1 deletion middlewares/stripPrefixRegex.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *StripPrefixRegex) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

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()
s.Handler.ServeHTTP(w, r)
return
Expand Down
107 changes: 71 additions & 36 deletions middlewares/stripPrefixRegex_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}

}
21 changes: 16 additions & 5 deletions middlewares/stripPrefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestStripPrefix(t *testing.T) {
path string
expectedStatusCode int
expectedPath string
expectedHeader string
}{
{
desc: "no prefixes configured",
Expand All @@ -29,27 +30,31 @@ func TestStripPrefix(t *testing.T) {
path: "/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/",
},
{
desc: "prefix and path matching",
prefixes: []string{"/stat"},
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
desc: "path prefix on exactly matching path",
prefixes: []string{"/stat/"},
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat/",
},
{
desc: "path prefix on matching longer path",
prefixes: []string{"/stat/"},
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat/",
},
{
desc: "path prefix on mismatching path",
Expand All @@ -63,41 +68,47 @@ func TestStripPrefix(t *testing.T) {
path: "/stat/",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
{
desc: "earlier prefix matching",
prefixes: []string{"/stat", "/stat/us"},
path: "/stat/us",
expectedStatusCode: http.StatusOK,
expectedPath: "/us",
expectedHeader: "/stat",
},
{
desc: "later prefix matching",
prefixes: []string{"/mismatch", "/stat"},
path: "/stat",
expectedStatusCode: http.StatusOK,
expectedPath: "/",
expectedHeader: "/stat",
},
}

for _, test := range tests {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
var gotPath string

var actualPath, actualHeader string
server := httptest.NewServer(&StripPrefix{
Prefixes: test.prefixes,
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()

resp, err := http.Get(server.URL + test.path)
require.NoError(t, err, "Failed to send GET request")
assert.Equal(t, test.expectedStatusCode, resp.StatusCode, "Unexpected status code")
require.NoError(t, err, "%s: failed to send GET request.", test.desc)

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)
})
}
}