summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/rewrite/rewrite_test.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2021-03-01 18:27:59 -0700
committerMatthew Holt <mholt@users.noreply.github.com>2021-03-01 18:27:59 -0700
commitad8d01cb66316cf04ea49ec277316d6f83b9abb6 (patch)
treec561189d8be27c5ab9145a86d8c9a6f26a339f1e /modules/caddyhttp/rewrite/rewrite_test.go
parent5bf0a55df499e189424103d87d93960395d3172f (diff)
rewrite: Implement regex path replacements
https://caddy.community/t/collapsing-multiple-forward-slashes-in-path-only/11626
Diffstat (limited to 'modules/caddyhttp/rewrite/rewrite_test.go')
-rw-r--r--modules/caddyhttp/rewrite/rewrite_test.go23
1 files changed, 20 insertions, 3 deletions
diff --git a/modules/caddyhttp/rewrite/rewrite_test.go b/modules/caddyhttp/rewrite/rewrite_test.go
index 9329a04..4d595e2 100644
--- a/modules/caddyhttp/rewrite/rewrite_test.go
+++ b/modules/caddyhttp/rewrite/rewrite_test.go
@@ -16,6 +16,7 @@ package rewrite
import (
"net/http"
+ "regexp"
"testing"
"github.com/caddyserver/caddy/v2"
@@ -232,20 +233,26 @@ func TestRewrite(t *testing.T) {
},
{
- rule: Rewrite{URISubstring: []replacer{{Find: "findme", Replace: "replaced"}}},
+ rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
input: newRequest(t, "GET", "/foo/bar"),
expect: newRequest(t, "GET", "/foo/bar"),
},
{
- rule: Rewrite{URISubstring: []replacer{{Find: "findme", Replace: "replaced"}}},
+ rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
input: newRequest(t, "GET", "/foo/findme/bar"),
expect: newRequest(t, "GET", "/foo/replaced/bar"),
},
{
- rule: Rewrite{URISubstring: []replacer{{Find: "findme", Replace: "replaced"}}},
+ rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
input: newRequest(t, "GET", "/foo/findme%2Fbar"),
expect: newRequest(t, "GET", "/foo/replaced%2Fbar"),
},
+
+ {
+ rule: Rewrite{PathRegexp: []*regexReplacer{{Find: "/{2,}", Replace: "/"}}},
+ input: newRequest(t, "GET", "/foo//bar///baz?a=b//c"),
+ expect: newRequest(t, "GET", "/foo/bar/baz?a=b//c"),
+ },
} {
// copy the original input just enough so that we can
// compare it after the rewrite to see if it changed
@@ -260,6 +267,16 @@ func TestRewrite(t *testing.T) {
repl.Set("http.request.uri.path", tc.input.URL.Path)
repl.Set("http.request.uri.query", tc.input.URL.RawQuery)
+ // we can't directly call Provision() without a valid caddy.Context
+ // (TODO: fix that) so here we ad-hoc compile the regex
+ for _, rep := range tc.rule.PathRegexp {
+ re, err := regexp.Compile(rep.Find)
+ if err != nil {
+ t.Fatal(err)
+ }
+ rep.re = re
+ }
+
changed := tc.rule.rewrite(tc.input, repl, nil)
if expected, actual := !reqEqual(originalInput, tc.input), changed; expected != actual {