mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 01:26:47 +01:00
Refactor. Tests and tests data.
This commit is contained in:
parent
1ed786f836
commit
9110dc4745
8 changed files with 174 additions and 13 deletions
|
@ -1,10 +1,9 @@
|
||||||
package setup
|
package setup
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/mholt/caddy/middleware/rewrite"
|
"github.com/mholt/caddy/middleware/rewrite"
|
||||||
)
|
)
|
||||||
|
@ -96,9 +95,9 @@ func TestRewriteParse(t *testing.T) {
|
||||||
}{
|
}{
|
||||||
{`rewrite {
|
{`rewrite {
|
||||||
r .*
|
r .*
|
||||||
to /to
|
to /to /index.php?
|
||||||
}`, false, []rewrite.Rule{
|
}`, false, []rewrite.Rule{
|
||||||
&rewrite.ComplexRule{Base: "/", To: "/to", Regexp: regexp.MustCompile(".*")},
|
&rewrite.ComplexRule{Base: "/", To: "/to /index.php?", Regexp: regexp.MustCompile(".*")},
|
||||||
}},
|
}},
|
||||||
{`rewrite {
|
{`rewrite {
|
||||||
regexp .*
|
regexp .*
|
||||||
|
@ -113,11 +112,11 @@ func TestRewriteParse(t *testing.T) {
|
||||||
}
|
}
|
||||||
rewrite / {
|
rewrite / {
|
||||||
regexp [a-z]+
|
regexp [a-z]+
|
||||||
to /to
|
to /to /to2
|
||||||
}
|
}
|
||||||
`, false, []rewrite.Rule{
|
`, false, []rewrite.Rule{
|
||||||
&rewrite.ComplexRule{Base: "/path", To: "/dest", Regexp: regexp.MustCompile("rr")},
|
&rewrite.ComplexRule{Base: "/path", To: "/dest", Regexp: regexp.MustCompile("rr")},
|
||||||
&rewrite.ComplexRule{Base: "/", To: "/to", Regexp: regexp.MustCompile("[a-z]+")},
|
&rewrite.ComplexRule{Base: "/", To: "/to /to2", Regexp: regexp.MustCompile("[a-z]+")},
|
||||||
}},
|
}},
|
||||||
{`rewrite {
|
{`rewrite {
|
||||||
r .*
|
r .*
|
||||||
|
@ -132,6 +131,12 @@ func TestRewriteParse(t *testing.T) {
|
||||||
{`rewrite /`, true, []rewrite.Rule{
|
{`rewrite /`, true, []rewrite.Rule{
|
||||||
&rewrite.ComplexRule{},
|
&rewrite.ComplexRule{},
|
||||||
}},
|
}},
|
||||||
|
{`rewrite {
|
||||||
|
to /to
|
||||||
|
if {path} is a
|
||||||
|
}`, false, []rewrite.Rule{
|
||||||
|
&rewrite.ComplexRule{Base: "/", To: "/to", Ifs: []rewrite.If{rewrite.If{"{path}", "is", "a"}}},
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, test := range regexpTests {
|
for i, test := range regexpTests {
|
||||||
|
@ -170,11 +175,19 @@ func TestRewriteParse(t *testing.T) {
|
||||||
i, j, expectedRule.To, actualRule.To)
|
i, j, expectedRule.To, actualRule.To)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if actualRule.Regexp != nil {
|
||||||
if actualRule.String() != expectedRule.String() {
|
if actualRule.String() != expectedRule.String() {
|
||||||
t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
|
t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
|
||||||
i, j, expectedRule.String(), actualRule.String())
|
i, j, expectedRule.String(), actualRule.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if fmt.Sprint(actualRule.Ifs) != fmt.Sprint(expectedRule.Ifs) {
|
||||||
|
t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
|
||||||
|
i, j, fmt.Sprint(expectedRule.Ifs), fmt.Sprint(actualRule.Ifs))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ func endsWithFunc(a, b string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// matchFunc is condition for Match operator.
|
// matchFunc is condition for Match operator.
|
||||||
// It does regexp matching of
|
// It does regexp matching of a against pattern in b
|
||||||
func matchFunc(a, b string) bool {
|
func matchFunc(a, b string) bool {
|
||||||
matched, _ := regexp.MatchString(b, a)
|
matched, _ := regexp.MatchString(b, a)
|
||||||
return matched
|
return matched
|
||||||
|
|
90
middleware/rewrite/condition_test.go
Normal file
90
middleware/rewrite/condition_test.go
Normal file
|
@ -0,0 +1,90 @@
|
||||||
|
package rewrite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConditions(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
condition string
|
||||||
|
isTrue bool
|
||||||
|
}{
|
||||||
|
{"a is b", false},
|
||||||
|
{"a is a", true},
|
||||||
|
{"a not b", true},
|
||||||
|
{"a not a", false},
|
||||||
|
{"a has a", true},
|
||||||
|
{"a has b", false},
|
||||||
|
{"ba has b", true},
|
||||||
|
{"bab has b", true},
|
||||||
|
{"bab has bb", false},
|
||||||
|
{"bab starts_with bb", false},
|
||||||
|
{"bab starts_with ba", true},
|
||||||
|
{"bab starts_with bab", true},
|
||||||
|
{"bab ends_with bb", false},
|
||||||
|
{"bab ends_with bab", true},
|
||||||
|
{"bab ends_with ab", true},
|
||||||
|
{"a match *", false},
|
||||||
|
{"a match a", true},
|
||||||
|
{"a match .*", true},
|
||||||
|
{"a match a.*", true},
|
||||||
|
{"a match b.*", false},
|
||||||
|
{"ba match b.*", true},
|
||||||
|
{"ba match b[a-z]", true},
|
||||||
|
{"b0 match b[a-z]", false},
|
||||||
|
{"b0a match b[a-z]", false},
|
||||||
|
{"b0a match b[a-z]+", false},
|
||||||
|
{"b0a match b[a-z0-9]+", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
str := strings.Fields(test.condition)
|
||||||
|
ifCond, err := NewIf(str[0], str[1], str[2])
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
isTrue := ifCond.True(nil)
|
||||||
|
if isTrue != test.isTrue {
|
||||||
|
t.Errorf("Test %v: expected %v found %v", i, test.isTrue, isTrue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidOperators := []string{"ss", "and", "if"}
|
||||||
|
for _, op := range invalidOperators {
|
||||||
|
_, err := NewIf("a", op, "b")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("Invalid operator %v used, expected error.", op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
replaceTests := []struct {
|
||||||
|
url string
|
||||||
|
condition string
|
||||||
|
isTrue bool
|
||||||
|
}{
|
||||||
|
{"/home", "{uri} match /home", true},
|
||||||
|
{"/hom", "{uri} match /home", false},
|
||||||
|
{"/hom", "{uri} starts_with /home", false},
|
||||||
|
{"/hom", "{uri} starts_with /h", true},
|
||||||
|
{"/home/.hiddenfile", `{uri} match \/\.(.*)`, true},
|
||||||
|
{"/home/.hiddendir/afile", `{uri} match \/\.(.*)`, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range replaceTests {
|
||||||
|
r, err := http.NewRequest("GET", test.url, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
str := strings.Fields(test.condition)
|
||||||
|
ifCond, err := NewIf(str[0], str[1], str[2])
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
isTrue := ifCond.True(r)
|
||||||
|
if isTrue != test.isTrue {
|
||||||
|
t.Errorf("Test %v: expected %v found %v", i, test.isTrue, isTrue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,7 +21,7 @@ func TestRewrite(t *testing.T) {
|
||||||
FileSys: http.Dir("."),
|
FileSys: http.Dir("."),
|
||||||
}
|
}
|
||||||
|
|
||||||
regexpRules := [][]string{
|
regexps := [][]string{
|
||||||
{"/reg/", ".*", "/to", ""},
|
{"/reg/", ".*", "/to", ""},
|
||||||
{"/r/", "[a-z]+", "/toaz", "!.html|"},
|
{"/r/", "[a-z]+", "/toaz", "!.html|"},
|
||||||
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
||||||
|
@ -33,7 +33,7 @@ func TestRewrite(t *testing.T) {
|
||||||
{"/ab/", `.*\.jpg`, "/ajpg", ""},
|
{"/ab/", `.*\.jpg`, "/ajpg", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, regexpRule := range regexpRules {
|
for _, regexpRule := range regexps {
|
||||||
var ext []string
|
var ext []string
|
||||||
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
|
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
|
||||||
ext = s[:len(s)-1]
|
ext = s[:len(s)-1]
|
||||||
|
|
0
middleware/rewrite/testdata/testdir/empty
vendored
Normal file
0
middleware/rewrite/testdata/testdir/empty
vendored
Normal file
1
middleware/rewrite/testdata/testfile
vendored
Normal file
1
middleware/rewrite/testdata/testfile
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
empty
|
|
@ -19,6 +19,13 @@ func To(fs http.FileSystem, r *http.Request, to string) bool {
|
||||||
t := ""
|
t := ""
|
||||||
for _, v := range tos {
|
for _, v := range tos {
|
||||||
t = path.Clean(replacer.Replace(v))
|
t = path.Clean(replacer.Replace(v))
|
||||||
|
|
||||||
|
// add trailing slash for directories, if present
|
||||||
|
if strings.HasSuffix(v, "/") && !strings.HasSuffix(t, "/") {
|
||||||
|
t += "/"
|
||||||
|
}
|
||||||
|
|
||||||
|
// validate file
|
||||||
if isValidFile(fs, t) {
|
if isValidFile(fs, t) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -69,5 +76,11 @@ func isValidFile(fs http.FileSystem, file string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.HasSuffix(file, "/") && stat.IsDir()
|
// directory
|
||||||
|
if strings.HasSuffix(file, "/") {
|
||||||
|
return stat.IsDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
// file
|
||||||
|
return !stat.IsDir()
|
||||||
}
|
}
|
||||||
|
|
44
middleware/rewrite/to_test.go
Normal file
44
middleware/rewrite/to_test.go
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package rewrite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTo(t *testing.T) {
|
||||||
|
fs := http.Dir("testdata")
|
||||||
|
tests := []struct {
|
||||||
|
url string
|
||||||
|
to string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"/", "/somefiles", "/somefiles"},
|
||||||
|
{"/somefiles", "/somefiles /index.php{uri}", "/index.php/somefiles"},
|
||||||
|
{"/somefiles", "/testfile /index.php{uri}", "/testfile"},
|
||||||
|
{"/somefiles", "/testfile/ /index.php{uri}", "/index.php/somefiles"},
|
||||||
|
{"/somefiles", "/somefiles /index.php{uri}", "/index.php/somefiles"},
|
||||||
|
{"/?a=b", "/somefiles /index.php?{query}", "/index.php?a=b"},
|
||||||
|
{"/?a=b", "/testfile /index.php?{query}", "/testfile?a=b"},
|
||||||
|
{"/?a=b", "/testdir /index.php?{query}", "/index.php?a=b"},
|
||||||
|
{"/?a=b", "/testdir/ /index.php?{query}", "/testdir/?a=b"},
|
||||||
|
}
|
||||||
|
|
||||||
|
uri := func(r *url.URL) string {
|
||||||
|
uri := r.Path
|
||||||
|
if r.RawQuery != "" {
|
||||||
|
uri += "?" + r.RawQuery
|
||||||
|
}
|
||||||
|
return uri
|
||||||
|
}
|
||||||
|
for i, test := range tests {
|
||||||
|
r, err := http.NewRequest("GET", test.url, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
To(fs, r, test.to)
|
||||||
|
if uri(r.URL) != test.expected {
|
||||||
|
t.Errorf("Test %v: expected %v found %v", i, test.expected, uri(r.URL))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue