A test for Sub-path functionality.
This commit is contained in:
parent
3cb32c5ec9
commit
c68d7b324a
@ -84,3 +84,45 @@ func TestPaths(t *testing.T) {
|
||||
t.Error("Path", p, "should return 404")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubPaths(t *testing.T) {
|
||||
found := true
|
||||
|
||||
r := httpr.New()
|
||||
|
||||
s := r.Sub("/api/v1")
|
||||
|
||||
err := s.Handler(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) { found = true })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = s.Handler(http.MethodGet, "/test", func(w http.ResponseWriter, r *http.Request) { found = true })
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r.NotFoundHandler = func(w http.ResponseWriter, r *http.Request) { found = false }
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
p := "/api/v1/"
|
||||
req := httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
||||
r.ServeHTTP(w, req)
|
||||
if !found {
|
||||
t.Error("Path", p, "should return 200")
|
||||
}
|
||||
|
||||
p = "/api/v1/test"
|
||||
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
||||
r.ServeHTTP(w, req)
|
||||
if !found {
|
||||
t.Error("Path", p, "should return 200")
|
||||
}
|
||||
|
||||
p = "/api/v1/nonexistent"
|
||||
req = httptest.NewRequest(http.MethodGet, p, strings.NewReader(""))
|
||||
r.ServeHTTP(w, req)
|
||||
if found {
|
||||
t.Error(found, "Path", p, "should return 404")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user