summaryrefslogtreecommitdiff
path: root/group_test.go
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-12-03 14:36:10 +0800
committerEltrac <hi@guhub.cn>2025-12-03 14:36:10 +0800
commitf7643a06846043cf932cf3d2eb7f49be5322ca56 (patch)
tree5366abbeb5e8428e12dccd4b322d79e5452bad9d /group_test.go
parent59ebb0972ee5107a208ef5e02759a7299afa9288 (diff)
feat: routes grouping
Diffstat (limited to 'group_test.go')
-rw-r--r--group_test.go123
1 files changed, 123 insertions, 0 deletions
diff --git a/group_test.go b/group_test.go
new file mode 100644
index 0000000..e682f09
--- /dev/null
+++ b/group_test.go
@@ -0,0 +1,123 @@
+package growel
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "testing"
+)
+
+/*
+ ======================================================
+ Test Group()
+ ======================================================
+*/
+
+func TestGroupCallback(t *testing.T) {
+ e := New()
+
+ called := false
+
+ e.Group("/user", func(g *Group) {
+ if g.prefix != "/user" {
+ t.Fatalf("expected prefix '/user', got '%s'", g.prefix)
+ }
+ if g.engine != e {
+ t.Fatal("group.engine does not point to Engine")
+ }
+ called = true
+ })
+
+ if !called {
+ t.Fatal("Group callback not invoked")
+ }
+}
+
+/*
+ ======================================================
+ Test route registration
+ ======================================================
+*/
+
+func TestGroupRouteRegistration(t *testing.T) {
+ e := New()
+
+ e.Group("/api", func(api *Group) {
+ api.GET("/ping", func(c *Context) {})
+ api.POST("/user", func(c *Context) {})
+ api.PUT("/v1/item/", func(c *Context) {})
+ api.DELETE("/v1/item/42", func(c *Context) {})
+ })
+
+ routes := e.router.routes
+
+ if len(routes) != 4 {
+ t.Fatalf("expected 4 routes, got %d", len(routes))
+ }
+
+ cases := []struct {
+ method string
+ path string
+ }{
+ {"GET", "/api/ping"},
+ {"POST", "/api/user"},
+ {"PUT", "/api/v1/item"},
+ {"DELETE", "/api/v1/item/42"},
+ }
+
+ for _, want := range cases {
+ found := false
+
+ for _, r := range routes {
+ joined := "/" + joinParts(r.parts)
+ if r.method == want.method && joined == want.path {
+ found = true
+ break
+ }
+ }
+
+ if !found {
+ t.Errorf("missing expected route: %s %s", want.method, want.path)
+ }
+ }
+}
+
+// helper: convert []string{"api","ping"} → "api/ping"
+func joinParts(parts []string) string {
+ out := ""
+ for i, p := range parts {
+ if i > 0 {
+ out += "/"
+ }
+ out += p
+ }
+ return out
+}
+
+/*
+ ======================================================
+ End-to-End ServeHTTP test
+ ======================================================
+*/
+
+func TestGroupServeHTTP(t *testing.T) {
+ e := New()
+
+ e.Group("/hello", func(h *Group) {
+ h.GET("/world", func(c *Context) {
+ c.Plain(200, "ok")
+ })
+ })
+
+ w := httptest.NewRecorder()
+ req, _ := http.NewRequest("GET", "/hello/world", nil)
+
+ e.ServeHTTP(w, req)
+
+ if w.Code != 200 {
+ t.Fatalf("expected 200, got %d", w.Code)
+ }
+
+ if body := w.Body.String(); body != "ok\n" && body != "ok" {
+ t.Fatalf("expected body 'ok', got '%s'", body)
+ }
+}