2015-05-07 14:10:48 -05:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2015-05-29 21:21:50 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware/rewrite"
|
2015-05-07 14:10:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRewrite(t *testing.T) {
|
2015-06-20 08:59:33 -05:00
|
|
|
c := NewTestController(`rewrite /from /to`)
|
2015-05-07 14:10:48 -05:00
|
|
|
|
|
|
|
mid, err := Rewrite(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 08:59:33 -05:00
|
|
|
handler := mid(EmptyNext)
|
2015-05-07 14:10:48 -05:00
|
|
|
myHandler, ok := handler.(rewrite.Rewrite)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Expected handler to be type Rewrite, got: %#v", handler)
|
|
|
|
}
|
|
|
|
|
2015-06-20 08:59:33 -05:00
|
|
|
if !SameNext(myHandler.Next, EmptyNext) {
|
2015-05-07 14:10:48 -05:00
|
|
|
t.Error("'Next' field of handler was not set properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(myHandler.Rules) != 1 {
|
|
|
|
t.Errorf("Expected handler to have %d rule, has %d instead", 1, len(myHandler.Rules))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRewriteParse(t *testing.T) {
|
2015-05-16 07:03:48 -05:00
|
|
|
simpleTests := []struct {
|
2015-05-07 14:10:48 -05:00
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
expected []rewrite.Rule
|
|
|
|
}{
|
|
|
|
{`rewrite /from /to`, false, []rewrite.Rule{
|
2015-05-16 07:03:48 -05:00
|
|
|
rewrite.SimpleRule{"/from", "/to"},
|
2015-05-07 14:10:48 -05:00
|
|
|
}},
|
|
|
|
{`rewrite /from /to
|
|
|
|
rewrite a b`, false, []rewrite.Rule{
|
2015-05-16 07:03:48 -05:00
|
|
|
rewrite.SimpleRule{"/from", "/to"},
|
|
|
|
rewrite.SimpleRule{"a", "b"},
|
2015-05-07 14:10:48 -05:00
|
|
|
}},
|
|
|
|
{`rewrite a`, true, []rewrite.Rule{}},
|
|
|
|
{`rewrite`, true, []rewrite.Rule{}},
|
|
|
|
{`rewrite a b c`, true, []rewrite.Rule{
|
2015-05-16 07:03:48 -05:00
|
|
|
rewrite.SimpleRule{"a", "b"},
|
2015-05-07 14:10:48 -05:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
for i, test := range simpleTests {
|
2015-06-20 08:59:33 -05:00
|
|
|
c := NewTestController(test.input)
|
2015-05-07 14:10:48 -05:00
|
|
|
actual, err := rewriteParse(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)
|
2015-05-16 07:03:48 -05:00
|
|
|
} else if err != nil && test.shouldErr {
|
|
|
|
continue
|
2015-05-07 14:10:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(actual) != len(test.expected) {
|
|
|
|
t.Fatalf("Test %d expected %d rules, but got %d",
|
|
|
|
i, len(test.expected), len(actual))
|
|
|
|
}
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
for j, e := range test.expected {
|
|
|
|
actualRule := actual[j].(rewrite.SimpleRule)
|
|
|
|
expectedRule := e.(rewrite.SimpleRule)
|
2015-05-07 14:10:48 -05:00
|
|
|
|
|
|
|
if actualRule.From != expectedRule.From {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected From=%s, got %s",
|
|
|
|
i, j, expectedRule.From, actualRule.From)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualRule.To != expectedRule.To {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected To=%s, got %s",
|
|
|
|
i, j, expectedRule.To, actualRule.To)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-16 07:03:48 -05:00
|
|
|
|
|
|
|
regexpTests := []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
expected []rewrite.Rule
|
|
|
|
}{
|
|
|
|
{`rewrite {
|
|
|
|
r .*
|
|
|
|
to /to
|
|
|
|
}`, false, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{"/", "/to", nil, regexp.MustCompile(".*")},
|
|
|
|
}},
|
|
|
|
{`rewrite {
|
|
|
|
regexp .*
|
|
|
|
to /to
|
|
|
|
ext / html txt
|
|
|
|
}`, false, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{"/", "/to", []string{"/", "html", "txt"}, regexp.MustCompile(".*")},
|
|
|
|
}},
|
|
|
|
{`rewrite /path {
|
|
|
|
r rr
|
|
|
|
to /dest
|
|
|
|
}
|
|
|
|
rewrite / {
|
|
|
|
regexp [a-z]+
|
|
|
|
to /to
|
|
|
|
}
|
|
|
|
`, false, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{"/path", "/dest", nil, regexp.MustCompile("rr")},
|
|
|
|
&rewrite.RegexpRule{"/", "/to", nil, regexp.MustCompile("[a-z]+")},
|
|
|
|
}},
|
|
|
|
{`rewrite {
|
|
|
|
to /to
|
|
|
|
}`, true, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{},
|
|
|
|
}},
|
|
|
|
{`rewrite {
|
|
|
|
r .*
|
|
|
|
}`, true, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{},
|
|
|
|
}},
|
|
|
|
{`rewrite {
|
|
|
|
|
|
|
|
}`, true, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{},
|
|
|
|
}},
|
|
|
|
{`rewrite /`, true, []rewrite.Rule{
|
|
|
|
&rewrite.RegexpRule{},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range regexpTests {
|
2015-06-20 08:59:33 -05:00
|
|
|
c := NewTestController(test.input)
|
2015-05-16 07:03:48 -05:00
|
|
|
actual, err := rewriteParse(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)
|
|
|
|
} else if err != nil && test.shouldErr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(actual) != len(test.expected) {
|
|
|
|
t.Fatalf("Test %d expected %d rules, but got %d",
|
|
|
|
i, len(test.expected), len(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
for j, e := range test.expected {
|
|
|
|
actualRule := actual[j].(*rewrite.RegexpRule)
|
|
|
|
expectedRule := e.(*rewrite.RegexpRule)
|
|
|
|
|
|
|
|
if actualRule.Base != expectedRule.Base {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected Base=%s, got %s",
|
|
|
|
i, j, expectedRule.Base, actualRule.Base)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualRule.To != expectedRule.To {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected To=%s, got %s",
|
|
|
|
i, j, expectedRule.To, actualRule.To)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fmt.Sprint(actualRule.Exts) != fmt.Sprint(expectedRule.Exts) {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected Ext=%v, got %v",
|
|
|
|
i, j, expectedRule.To, actualRule.To)
|
|
|
|
}
|
|
|
|
|
|
|
|
if actualRule.String() != expectedRule.String() {
|
|
|
|
t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
|
|
|
|
i, j, expectedRule.String(), actualRule.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 14:10:48 -05:00
|
|
|
}
|