diff options
| author | Eltrac <hi@guhub.cn> | 2025-12-03 14:49:52 +0800 |
|---|---|---|
| committer | Eltrac <hi@guhub.cn> | 2025-12-03 14:49:52 +0800 |
| commit | a206d80e51372dffcb86b6fcd2be3c8e24517c5c (patch) | |
| tree | aa1c407db99b6db9307cac2f3b628a5530d7965a | |
| parent | c1f6161d4360578cba00f2a6933b7cf9247622a3 (diff) | |
test: netsted group
| -rw-r--r-- | group_test.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/group_test.go b/group_test.go index e1ceca4..56774cb 100644 --- a/group_test.go +++ b/group_test.go @@ -92,6 +92,48 @@ func TestGroupServeHTTP(t *testing.T) { } } +func TestNestedGroup(t *testing.T) { + e := New() + e.Group("/api", func(api *Group) { + api.Group("v1", func(v1 *Group) { + v1.GET("/ping", func(c *Context) { + c.Plain(200, "pong") + }) + }) + api.Group("v2", func(v2 *Group) { + v2.GET("/ping", func(c *Context) { + c.Plain(200, "pong") + }) + }) + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("GET", "/api/v1/ping", nil) + + e.ServeHTTP(w, req) + + if w.Code != 200 { + t.Fatalf("expected 200, got %d", w.Code) + } + + if body := w.Body.String(); body != "pong\n" && body != "pong" { + t.Fatalf("expected body 'pong', got '%s'", body) + } + + w = httptest.NewRecorder() + req, _ = http.NewRequest("GET", "/api/v2/ping", nil) + + e.ServeHTTP(w, req) + + if w.Code != 200 { + t.Fatalf("expected 200, got %d", w.Code) + } + + if body := w.Body.String(); body != "pong\n" && body != "pong" { + t.Fatalf("expected body 'pong', got '%s'", body) + } +} + // helper: convert []string{"api","ping"} → "api/ping" func joinParts(parts []string) string { out := "" |
