2015-05-07 15:55:03 -05:00
|
|
|
package rewrite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2015-05-16 07:03:48 -05:00
|
|
|
"strings"
|
2015-05-24 20:00:54 -05:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
2015-05-07 15:55:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRewrite(t *testing.T) {
|
|
|
|
rw := Rewrite{
|
|
|
|
Next: middleware.HandlerFunc(urlPrinter),
|
|
|
|
Rules: []Rule{
|
2015-05-16 07:03:48 -05:00
|
|
|
NewSimpleRule("/from", "/to"),
|
|
|
|
NewSimpleRule("/a", "/b"),
|
2015-11-24 06:20:39 -05:00
|
|
|
NewSimpleRule("/b", "/b{uri}"),
|
2015-05-07 15:55:03 -05:00
|
|
|
},
|
|
|
|
}
|
2015-05-16 07:03:48 -05:00
|
|
|
|
|
|
|
regexpRules := [][]string{
|
2015-10-09 17:35:34 -05:00
|
|
|
{"/reg/", ".*", "/to", ""},
|
|
|
|
{"/r/", "[a-z]+", "/toaz", "!.html|"},
|
|
|
|
{"/url/", "a([a-z0-9]*)s([A-Z]{2})", "/to/{path}", ""},
|
|
|
|
{"/ab/", "ab", "/ab?{query}", ".txt|"},
|
|
|
|
{"/ab/", "ab", "/ab?type=html&{query}", ".html|"},
|
|
|
|
{"/abc/", "ab", "/abc/{file}", ".html|"},
|
|
|
|
{"/abcd/", "ab", "/a/{dir}/{file}", ".html|"},
|
|
|
|
{"/abcde/", "ab", "/a#{fragment}", ".html|"},
|
|
|
|
{"/ab/", `.*\.jpg`, "/ajpg", ""},
|
2015-05-16 07:03:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, regexpRule := range regexpRules {
|
|
|
|
var ext []string
|
|
|
|
if s := strings.Split(regexpRule[3], "|"); len(s) > 1 {
|
|
|
|
ext = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
rule, err := NewRegexpRule(regexpRule[0], regexpRule[1], regexpRule[2], ext)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
rw.Rules = append(rw.Rules, rule)
|
|
|
|
}
|
|
|
|
|
2015-05-07 15:55:03 -05:00
|
|
|
tests := []struct {
|
|
|
|
from string
|
|
|
|
expectedTo string
|
|
|
|
}{
|
|
|
|
{"/from", "/to"},
|
|
|
|
{"/a", "/b"},
|
2015-11-24 06:20:39 -05:00
|
|
|
{"/b", "/b/b"},
|
2015-05-07 15:55:03 -05:00
|
|
|
{"/aa", "/aa"},
|
|
|
|
{"/", "/"},
|
|
|
|
{"/a?foo=bar", "/b?foo=bar"},
|
|
|
|
{"/asdf?foo=bar", "/asdf?foo=bar"},
|
|
|
|
{"/foo#bar", "/foo#bar"},
|
|
|
|
{"/a#foo", "/b#foo"},
|
2015-05-16 07:03:48 -05:00
|
|
|
{"/reg/foo", "/to"},
|
|
|
|
{"/re", "/re"},
|
|
|
|
{"/r/", "/r/"},
|
|
|
|
{"/r/123", "/r/123"},
|
|
|
|
{"/r/a123", "/toaz"},
|
|
|
|
{"/r/abcz", "/toaz"},
|
|
|
|
{"/r/z", "/toaz"},
|
|
|
|
{"/r/z.html", "/r/z.html"},
|
|
|
|
{"/r/z.js", "/toaz"},
|
|
|
|
{"/url/asAB", "/to/url/asAB"},
|
|
|
|
{"/url/aBsAB", "/url/aBsAB"},
|
|
|
|
{"/url/a00sAB", "/to/url/a00sAB"},
|
|
|
|
{"/url/a0z0sAB", "/to/url/a0z0sAB"},
|
|
|
|
{"/ab/aa", "/ab/aa"},
|
|
|
|
{"/ab/ab", "/ab/ab"},
|
|
|
|
{"/ab/ab.txt", "/ab"},
|
|
|
|
{"/ab/ab.txt?name=name", "/ab?name=name"},
|
|
|
|
{"/ab/ab.html?name=name", "/ab?type=html&name=name"},
|
2015-05-16 10:27:41 -05:00
|
|
|
{"/abc/ab.html", "/abc/ab.html"},
|
|
|
|
{"/abcd/abcd.html", "/a/abcd/abcd.html"},
|
|
|
|
{"/abcde/abcde.html", "/a"},
|
|
|
|
{"/abcde/abcde.html#1234", "/a#1234"},
|
2015-05-21 19:38:07 -05:00
|
|
|
{"/ab/ab.jpg", "/ajpg"},
|
2015-05-07 15:55:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
req, err := http.NewRequest("GET", test.from, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Test %d: Could not create HTTP request: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
rw.ServeHTTP(rec, req)
|
|
|
|
|
|
|
|
if rec.Body.String() != test.expectedTo {
|
|
|
|
t.Errorf("Test %d: Expected URL to be '%s' but was '%s'",
|
|
|
|
i, test.expectedTo, rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func urlPrinter(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
fmt.Fprintf(w, r.URL.String())
|
|
|
|
return 0, nil
|
|
|
|
}
|