mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-26 05:55:27 +00:00
69fb22a9e7
Remove leading slashes from captured portions of paths when redirecting using splats. This makes a directive like "/articles/* /posts/:splat 302" behave as described in FEATURES.md, i.e. "/articles/foo" now redirects to "/posts/foo" rather than to "/posts//foo". Fixes #269. This also changes the behavior of a redirect like "/articles/* /posts:splat 302". "/articles/foo" will now redirect to "/postsfoo" rather than to "/posts/foo". This change also fixes an issue where paths like "/articles123" would be incorrectly matched by the above patterns. Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/308 Reviewed-by: crapStone <codeberg@crapstone.dev> Co-authored-by: Daniel Erat <dan@erat.org> Co-committed-by: Daniel Erat <dan@erat.org>
37 lines
1.4 KiB
Go
37 lines
1.4 KiB
Go
package upstream
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestRedirect_rewriteURL(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
redirect Redirect
|
|
reqURL string
|
|
wantDstURL string
|
|
wantOk bool
|
|
}{
|
|
{Redirect{"/", "/dst", 200}, "/", "/dst", true},
|
|
{Redirect{"/", "/dst", 200}, "/foo", "", false},
|
|
{Redirect{"/src", "/dst", 200}, "/src", "/dst", true},
|
|
{Redirect{"/src", "/dst", 200}, "/foo", "", false},
|
|
{Redirect{"/src", "/dst", 200}, "/src/foo", "", false},
|
|
{Redirect{"/*", "/dst", 200}, "/", "/dst", true},
|
|
{Redirect{"/*", "/dst", 200}, "/src", "/dst", true},
|
|
{Redirect{"/src/*", "/dst/:splat", 200}, "/src", "/dst/", true},
|
|
{Redirect{"/src/*", "/dst/:splat", 200}, "/src/", "/dst/", true},
|
|
{Redirect{"/src/*", "/dst/:splat", 200}, "/src/foo", "/dst/foo", true},
|
|
{Redirect{"/src/*", "/dst/:splat", 200}, "/src/foo/bar", "/dst/foo/bar", true},
|
|
{Redirect{"/src/*", "/dst/:splatsuffix", 200}, "/src/foo", "/dst/foosuffix", true},
|
|
{Redirect{"/src/*", "/dst:splat", 200}, "/src/foo", "/dstfoo", true},
|
|
{Redirect{"/src/*", "/dst", 200}, "/srcfoo", "", false},
|
|
// This is the example from FEATURES.md:
|
|
{Redirect{"/articles/*", "/posts/:splat", 302}, "/articles/2022/10/12/post-1/", "/posts/2022/10/12/post-1/", true},
|
|
} {
|
|
if dstURL, ok := tc.redirect.rewriteURL(tc.reqURL); dstURL != tc.wantDstURL || ok != tc.wantOk {
|
|
t.Errorf("%#v.rewriteURL(%q) = %q, %v; want %q, %v",
|
|
tc.redirect, tc.reqURL, dstURL, ok, tc.wantDstURL, tc.wantOk)
|
|
}
|
|
}
|
|
}
|