Skip to content

Commit d1d420b

Browse files
refactor: add tests
1 parent c7dac96 commit d1d420b

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

path_value_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ func TestPathValue(t *testing.T) {
4040
requestPath: "/users/Gojo/friends/all-of-them/and/more",
4141
expectedBody: "Gojo all-of-them/and/more",
4242
},
43+
{
44+
name: "Named slash path",
45+
pattern: "/foo/bar/{other-stuff:*}/fizz/buzz",
46+
method: "GET",
47+
pathKeys: []string{"other-stuff"},
48+
requestPath: "/foo/bar/one/two/three/fizz/buzz",
49+
expectedBody: "one/two/three",
50+
},
4351
}
4452

4553
for _, tc := range testCases {
@@ -72,3 +80,19 @@ func TestPathValue(t *testing.T) {
7280
})
7381
}
7482
}
83+
84+
func TestPathValueNamedSlashPathEmpty(t *testing.T) {
85+
r := NewRouter()
86+
87+
r.Get("/foo/{path:*}/bar", func(w http.ResponseWriter, r *http.Request) {
88+
w.Write([]byte(r.PathValue("path") + ":" + URLParam(r, "path")))
89+
})
90+
91+
ts := httptest.NewServer(r)
92+
defer ts.Close()
93+
94+
_, body := testRequest(t, ts, "GET", "/foo/bar", nil)
95+
if body != ":" {
96+
t.Fatalf("expecting empty path values, got %q", body)
97+
}
98+
}

tree_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,65 @@ func TestTreeRegexp(t *testing.T) {
333333
}
334334
}
335335

336+
func TestTreeSlashParam(t *testing.T) {
337+
hNested := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
338+
hGreedy := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
339+
hEmpty := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
340+
hEmbeddedEmpty := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
341+
hTerminal := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
342+
hRegexp := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
343+
344+
tr := &node{}
345+
tr.InsertRoute(mGET, "/foo/bar/{other-stuff:*}/fizz/buzz", hNested)
346+
tr.InsertRoute(mGET, "/greedy/{path:*}/bar", hGreedy)
347+
tr.InsertRoute(mGET, "/empty/{path:*}/bar", hEmpty)
348+
tr.InsertRoute(mGET, "/embedded/prefix-{path:*}/bar", hEmbeddedEmpty)
349+
tr.InsertRoute(mGET, "/files/{path:*}", hTerminal)
350+
tr.InsertRoute(mGET, "/regexp/{path:.*}/tail", hRegexp)
351+
352+
tests := []struct {
353+
r string
354+
h http.Handler
355+
k []string
356+
v []string
357+
}{
358+
{r: "/foo/bar/one/two/three/fizz/buzz", h: hNested, k: []string{"other-stuff"}, v: []string{"one/two/three"}},
359+
{r: "/greedy/one/bar/two/bar", h: hGreedy, k: []string{"path"}, v: []string{"one/bar/two"}},
360+
{r: "/empty/bar", h: hEmpty, k: []string{"path"}, v: []string{""}},
361+
{r: "/empty//bar", h: hEmpty, k: []string{"path"}, v: []string{""}},
362+
{r: "/embedded/prefix-bar", h: hEmbeddedEmpty, k: []string{"path"}, v: []string{""}},
363+
{r: "/files", h: hTerminal, k: []string{"path"}, v: []string{""}},
364+
{r: "/files/", h: hTerminal, k: []string{"path"}, v: []string{""}},
365+
{r: "/files/a/b", h: hTerminal, k: []string{"path"}, v: []string{"a/b"}},
366+
{r: "/regexp/a/b/tail", h: nil, k: []string{}, v: []string{}},
367+
{r: "/regexp/abc/tail", h: hRegexp, k: []string{"path"}, v: []string{"abc"}},
368+
}
369+
370+
for i, tt := range tests {
371+
rctx := NewRouteContext()
372+
373+
_, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
374+
375+
var handler http.Handler
376+
if methodHandler, ok := handlers[mGET]; ok {
377+
handler = methodHandler.handler
378+
}
379+
380+
paramKeys := rctx.routeParams.Keys
381+
paramValues := rctx.routeParams.Values
382+
383+
if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
384+
t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
385+
}
386+
if !stringSliceEqual(tt.k, paramKeys) {
387+
t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
388+
}
389+
if !stringSliceEqual(tt.v, paramValues) {
390+
t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
391+
}
392+
}
393+
}
394+
336395
func TestTreeRegexpRecursive(t *testing.T) {
337396
hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
338397
hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

0 commit comments

Comments
 (0)