mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-23 22:27:38 -05:00
headers: Test for Headers and headersParse funcs
This commit is contained in:
parent
df6efe5d88
commit
0d5a8a7383
1 changed files with 85 additions and 0 deletions
85
config/setup/headers_test.go
Normal file
85
config/setup/headers_test.go
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
package setup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mholt/caddy/middleware/headers"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHeaders(t *testing.T) {
|
||||||
|
c := newTestController(`header / Foo Bar`)
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
handler := mid(emptyNext)
|
||||||
|
myHandler, ok := handler.(headers.Headers)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("Expected handler to be type BasicAuth, got: %#v", handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !sameNext(myHandler.Next, emptyNext) {
|
||||||
|
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 {
|
||||||
|
c := newTestController(test.input)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue