Skip to content

Commit 8501f70

Browse files
committed
feat(redirect): add tests for WithPath
Signed-off-by: Vaughn Dice <[email protected]>
1 parent f8dfbb4 commit 8501f70

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

redirect/redirect_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,70 @@
11
package redirect
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"testing"
7+
)
8+
9+
type testConfigReader struct {
10+
includePath string
11+
trimPrefix string
12+
}
13+
14+
func (c *testConfigReader) Get(key string) string {
15+
switch key {
16+
case includePathKey:
17+
return c.includePath
18+
case trimPrefixKey:
19+
return c.trimPrefix
20+
default:
21+
return ""
22+
}
23+
}
24+
25+
func TestWithPath(t *testing.T) {
26+
type test struct {
27+
name string
28+
cfg testConfigReader
29+
reqPath string
30+
wantURL string
31+
}
32+
33+
var tests = []test{
34+
{
35+
"include_path false",
36+
testConfigReader{},
37+
"/foo/bar",
38+
"http://localhost",
39+
},
40+
{
41+
"include_path true, trim_prefix empty",
42+
testConfigReader{includePath: "true"},
43+
"/foo/bar",
44+
"http://localhost/foo/bar",
45+
},
46+
{
47+
"include_path true, trim_prefix is foo",
48+
testConfigReader{includePath: "true", trimPrefix: "/foo"},
49+
"/foo/bar",
50+
"http://localhost/bar",
51+
},
52+
}
53+
54+
for _, test := range tests {
55+
r := &SpinRedirect{
56+
cfg: &test.cfg,
57+
}
58+
59+
dest := "http://localhost"
60+
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost%s", test.reqPath), nil)
61+
if err != nil {
62+
t.Fatalf("failed to create new http request: %s", err.Error())
63+
}
64+
65+
gotURL := r.WithPath(dest, req)
66+
if gotURL != test.wantURL {
67+
t.Fatalf("test '%s' failed: got '%s', want '%s'", test.name, gotURL, test.wantURL)
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)