mirror of
https://github.com/caddyserver/caddy.git
synced 2025-06-23 15:31:40 -04:00
Check if the Next handler was called unexpectedly
Removed body check and replaced urlPrinter with a inlined Next handler
This commit is contained in:
parent
95b4e61a07
commit
0f9df18dfb
@ -1,7 +1,6 @@
|
|||||||
package redirect
|
package redirect
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
@ -10,14 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestRedirect(t *testing.T) {
|
func TestRedirect(t *testing.T) {
|
||||||
re := Redirect{
|
for i, test := range []struct {
|
||||||
Next: middleware.HandlerFunc(urlPrinter),
|
|
||||||
Rules: []Rule{
|
|
||||||
{From: "/from", To: "/to"},
|
|
||||||
{From: "/a", To: "/b"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
from string
|
from string
|
||||||
expectedLocation string
|
expectedLocation string
|
||||||
}{
|
}{
|
||||||
@ -29,9 +21,20 @@ func TestRedirect(t *testing.T) {
|
|||||||
{"/asdf?foo=bar", ""},
|
{"/asdf?foo=bar", ""},
|
||||||
{"/foo#bar", ""},
|
{"/foo#bar", ""},
|
||||||
{"/a#foo", "/b"},
|
{"/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"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range tests {
|
|
||||||
req, err := http.NewRequest("GET", test.from, nil)
|
req, err := http.NewRequest("GET", test.from, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
||||||
@ -45,22 +48,8 @@ func TestRedirect(t *testing.T) {
|
|||||||
i, test.expectedLocation, rec.Header().Get("Location"))
|
i, test.expectedLocation, rec.Header().Get("Location"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var expectedBody string
|
if nextCalled && test.expectedLocation != "" {
|
||||||
|
t.Errorf("Test %d: Next handler was unexpectedly called", i)
|
||||||
if test.expectedLocation != "" {
|
|
||||||
expectedBody = "<a href=\"" + test.expectedLocation + "\"></a>.\n\n"
|
|
||||||
} else {
|
|
||||||
expectedBody = test.from
|
|
||||||
}
|
|
||||||
|
|
||||||
if rec.Body.String() != expectedBody {
|
|
||||||
t.Errorf("Test %d: Expected body to be %q but was %q",
|
|
||||||
i, expectedBody, rec.Body.String())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
||||||
fmt.Fprintf(w, r.URL.String())
|
|
||||||
return 0, nil
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user