pages-server/server/upstream/redirects_test.go

38 lines
1.5 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},
// TODO: There shouldn't be double-slashes in these URLs:
// https://codeberg.org/Codeberg/pages-server/issues/269
{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},
// TODO: This is a workaround for the above bug. Should it be preserved?
{Redirect{"/src/*", "/dst:splat", 200}, "/src/foo", "/dst/foo", true},
// TODO: This behavior is incorrect; no redirect should be performed.
{Redirect{"/src/*", "/dst", 200}, "/srcfoo", "/dst", 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)
}
}
}