Commit 9110dc47 authored by Abiola Ibrahim's avatar Abiola Ibrahim

Refactor. Tests and tests data.

parent 1ed786f8
package setup
import (
"testing"
"fmt"
"regexp"
"testing"
"github.com/mholt/caddy/middleware/rewrite"
)
......@@ -96,9 +95,9 @@ func TestRewriteParse(t *testing.T) {
}{
{`rewrite {
r .*
to /to
to /to /index.php?
}`, false, []rewrite.Rule{
&rewrite.ComplexRule{Base: "/", To: "/to", Regexp: regexp.MustCompile(".*")},
&rewrite.ComplexRule{Base: "/", To: "/to /index.php?", Regexp: regexp.MustCompile(".*")},
}},
{`rewrite {
regexp .*
......@@ -113,11 +112,11 @@ func TestRewriteParse(t *testing.T) {
}
rewrite / {
regexp [a-z]+
to /to
to /to /to2
}
`, false, []rewrite.Rule{
&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 {
r .*
......@@ -132,6 +131,12 @@ func TestRewriteParse(t *testing.T) {
{`rewrite /`, true, []rewrite.Rule{
&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 {
......@@ -170,10 +175,18 @@ func TestRewriteParse(t *testing.T) {
i, j, expectedRule.To, actualRule.To)
}
if actualRule.String() != expectedRule.String() {
if actualRule.Regexp != nil {
if actualRule.String() != expectedRule.String() {
t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
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, expectedRule.String(), actualRule.String())
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.
// It does regexp matching of
// It does regexp matching of a against pattern in b
func matchFunc(a, b string) bool {
matched, _ := regexp.MatchString(b, a)
return matched
......
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("."),
}
regexpRules := [][]string{
regexps := [][]string{
{"/reg/", ".*", "/to", ""},
{"/r/", "[a-z]+", "/toaz", "!.html|"},
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
......@@ -33,7 +33,7 @@ func TestRewrite(t *testing.T) {
{"/ab/", `.*\.jpg`, "/ajpg", ""},
}
for _, regexpRule := range regexpRules {
for _, regexpRule := range regexps {
var ext []string
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
ext = s[:len(s)-1]
......
empty
\ No newline at end of file
......@@ -19,6 +19,13 @@ func To(fs http.FileSystem, r *http.Request, to string) bool {
t := ""
for _, v := range tos {
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) {
break
}
......@@ -69,5 +76,11 @@ func isValidFile(fs http.FileSystem, file string) bool {
return false
}
return strings.HasSuffix(file, "/") && stat.IsDir()
// directory
if strings.HasSuffix(file, "/") {
return stat.IsDir()
}
// file
return !stat.IsDir()
}
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))
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment