mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-19 03:26:57 +00:00
Refactor redirect code and add tests (#304)
Move repetitive code from Options.matchRedirects into a new Redirect.rewriteURL method and add a new test file. No functional changes are intended; this is in preparation for a later change to address #269. Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/304 Reviewed-by: crapStone <codeberg@crapstone.dev> Co-authored-by: Daniel Erat <dan@erat.org> Co-committed-by: Daniel Erat <dan@erat.org>
This commit is contained in:
parent
03881382a4
commit
9ffdc9d4f9
2 changed files with 68 additions and 44 deletions
37
server/upstream/redirects_test.go
Normal file
37
server/upstream/redirects_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue