From c68d7b324af110f4a76f6bd1885ac4e64b822d4d Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Fri, 11 Aug 2023 18:42:46 +0400 Subject: [PATCH] A test for Sub-path functionality. --- httpr_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/httpr_test.go b/httpr_test.go index f05ae1f..4eafbd0 100644 --- a/httpr_test.go +++ b/httpr_test.go @@ -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") + } +}