TestGroupSetNotFoundHandle tests the SetNotFoundHandle functionality
(t *testing.T)
| 8 | |
| 9 | // TestGroupSetNotFoundHandle tests the SetNotFoundHandle functionality |
| 10 | func TestGroupSetNotFoundHandle(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | groupPrefix string |
| 14 | requestPath string |
| 15 | expectedBody string |
| 16 | shouldUseGroup bool |
| 17 | }{ |
| 18 | { |
| 19 | name: "Group 404 - API endpoint not found", |
| 20 | groupPrefix: "/api", |
| 21 | requestPath: "/api/users", |
| 22 | expectedBody: "API 404", |
| 23 | shouldUseGroup: true, |
| 24 | }, |
| 25 | { |
| 26 | name: "Group 404 - Similar prefix should not match", |
| 27 | groupPrefix: "/api", |
| 28 | requestPath: "/api_v2/users", |
| 29 | expectedBody: "Global 404", |
| 30 | shouldUseGroup: false, |
| 31 | }, |
| 32 | { |
| 33 | name: "Global 404 - No matching group", |
| 34 | groupPrefix: "/api", |
| 35 | requestPath: "/web/index", |
| 36 | expectedBody: "Global 404", |
| 37 | shouldUseGroup: false, |
| 38 | }, |
| 39 | } |
| 40 | |
| 41 | for _, tt := range tests { |
| 42 | t.Run(tt.name, func(t *testing.T) { |
| 43 | // Create app |
| 44 | app := New() |
| 45 | |
| 46 | // Set global 404 handler |
| 47 | app.SetNotFoundHandle(func(ctx Context) { |
| 48 | ctx.WriteString("Global 404") |
| 49 | }) |
| 50 | |
| 51 | // Create group with custom 404 handler |
| 52 | group := app.HttpServer.Group(tt.groupPrefix) |
| 53 | group.SetNotFoundHandle(func(ctx Context) { |
| 54 | ctx.WriteString(tt.expectedBody) |
| 55 | }) |
| 56 | |
| 57 | // Add a valid route to group |
| 58 | group.GET("/exists", func(ctx Context) error { |
| 59 | return ctx.WriteString("OK") |
| 60 | }) |
| 61 | |
| 62 | // Create context |
| 63 | context := &HttpContext{ |
| 64 | response: &Response{}, |
| 65 | request: &Request{ |
| 66 | Request: &http.Request{ |
| 67 | URL: &url.URL{Path: tt.requestPath}, |
nothing calls this directly
no test coverage detected