2015-05-10 05:10:08 +08:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware/headers"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHeaders(t *testing.T) {
|
2015-06-20 21:59:33 +08:00
|
|
|
c := NewTestController(`header / Foo Bar`)
|
2015-05-10 05:10:08 +08:00
|
|
|
|
|
|
|
mid, err := Headers(c)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected no errors, but got: %v", err)
|
|
|
|
}
|
|
|
|
if mid == nil {
|
|
|
|
t.Fatal("Expected middleware, was nil instead")
|
|
|
|
}
|
|
|
|
|
2015-06-20 21:59:33 +08:00
|
|
|
handler := mid(EmptyNext)
|
2015-05-10 05:10:08 +08:00
|
|
|
myHandler, ok := handler.(headers.Headers)
|
|
|
|
if !ok {
|
2015-05-10 13:44:43 +08:00
|
|
|
t.Fatalf("Expected handler to be type Headers, got: %#v", handler)
|
2015-05-10 05:10:08 +08:00
|
|
|
}
|
|
|
|
|
2015-06-20 21:59:33 +08:00
|
|
|
if !SameNext(myHandler.Next, EmptyNext) {
|
2015-05-10 05:10:08 +08:00
|
|
|
t.Error("'Next' field of handler was not set properly")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHeadersParse(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
expected []headers.Rule
|
|
|
|
}{
|
|
|
|
{`header /foo Foo "Bar Baz"`,
|
|
|
|
false, []headers.Rule{
|
|
|
|
{Path: "/foo", Headers: []headers.Header{
|
|
|
|
{"Foo", "Bar Baz"},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
{`header /bar { Foo "Bar Baz" Baz Qux }`,
|
|
|
|
false, []headers.Rule{
|
|
|
|
{Path: "/bar", Headers: []headers.Header{
|
|
|
|
{"Foo", "Bar Baz"},
|
|
|
|
{"Baz", "Qux"},
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
2015-06-20 21:59:33 +08:00
|
|
|
c := NewTestController(test.input)
|
2015-05-10 05:10:08 +08:00
|
|
|
actual, err := headersParse(c)
|
|
|
|
|
|
|
|
if err == nil && test.shouldErr {
|
|
|
|
t.Errorf("Test %d didn't error, but it should have", i)
|
|
|
|
} else if err != nil && !test.shouldErr {
|
|
|
|
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(actual) != len(test.expected) {
|
|
|
|
t.Fatalf("Test %d expected %d rules, but got %d",
|
|
|
|
i, len(test.expected), len(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, expectedRule := range test.expected {
|
|
|
|
actualRule := actual[j]
|
|
|
|
|
|
|
|
if actualRule.Path != expectedRule.Path {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected path %s, but got %s",
|
|
|
|
i, j, expectedRule.Path, actualRule.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedHeaders := fmt.Sprintf("%v", expectedRule.Headers)
|
|
|
|
actualHeaders := fmt.Sprintf("%v", actualRule.Headers)
|
|
|
|
|
|
|
|
if actualHeaders != expectedHeaders {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected headers %s, but got %s",
|
|
|
|
i, j, expectedHeaders, actualHeaders)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|