A test for Sub-path functionality.

This commit is contained in:
Alexander Andreev 2023-08-11 18:42:46 +04:00
parent 3cb32c5ec9
commit c68d7b324a
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -84,3 +84,45 @@ func TestPaths(t *testing.T) {
t.Error("Path", p, "should return 404") 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")
}
}