mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-20 22:52:58 -05:00
Merge pull request #67 from peterhellberg/redirect_test
redirect: initial test for Redirect
This commit is contained in:
commit
a96c4d707b
1 changed files with 55 additions and 0 deletions
55
middleware/redirect/redirect_test.go
Normal file
55
middleware/redirect/redirect_test.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package redirect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/middleware"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRedirect(t *testing.T) {
|
||||||
|
for i, test := range []struct {
|
||||||
|
from string
|
||||||
|
expectedLocation string
|
||||||
|
}{
|
||||||
|
{"/from", "/to"},
|
||||||
|
{"/a", "/b"},
|
||||||
|
{"/aa", ""},
|
||||||
|
{"/", ""},
|
||||||
|
{"/a?foo=bar", "/b"},
|
||||||
|
{"/asdf?foo=bar", ""},
|
||||||
|
{"/foo#bar", ""},
|
||||||
|
{"/a#foo", "/b"},
|
||||||
|
} {
|
||||||
|
var nextCalled bool
|
||||||
|
|
||||||
|
re := Redirect{
|
||||||
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
|
nextCalled = true
|
||||||
|
return 0, nil
|
||||||
|
}),
|
||||||
|
Rules: []Rule{
|
||||||
|
{From: "/from", To: "/to"},
|
||||||
|
{From: "/a", To: "/b"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", test.from, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
re.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
if rec.Header().Get("Location") != test.expectedLocation {
|
||||||
|
t.Errorf("Test %d: Expected Location header to be %q but was %q",
|
||||||
|
i, test.expectedLocation, rec.Header().Get("Location"))
|
||||||
|
}
|
||||||
|
|
||||||
|
if nextCalled && test.expectedLocation != "" {
|
||||||
|
t.Errorf("Test %d: Next handler was unexpectedly called", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue