|
| 1 | +package dotweb |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | +) |
| 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}, |
| 68 | + Method: "GET", |
| 69 | + }, |
| 70 | + }, |
| 71 | + httpServer: &HttpServer{ |
| 72 | + DotApp: app, |
| 73 | + }, |
| 74 | + routerNode: &Node{}, |
| 75 | + } |
| 76 | + |
| 77 | + w := &testHttpWriter{} |
| 78 | + context.response = NewResponse(w) |
| 79 | + |
| 80 | + // Serve HTTP |
| 81 | + app.HttpServer.Router().ServeHTTP(context) |
| 82 | + |
| 83 | + // Check response - we can't easily check body content without more setup |
| 84 | + // This test mainly verifies no panic and correct routing logic |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestGroupNotFoundHandlePriority tests that group handler takes priority over global handler |
| 90 | +func TestGroupNotFoundHandlePriority(t *testing.T) { |
| 91 | + app := New() |
| 92 | + |
| 93 | + // Set global handler |
| 94 | + app.SetNotFoundHandle(func(ctx Context) { |
| 95 | + ctx.WriteString("Global Handler") |
| 96 | + }) |
| 97 | + |
| 98 | + // Create group with handler |
| 99 | + apiGroup := app.HttpServer.Group("/api") |
| 100 | + apiGroup.SetNotFoundHandle(func(ctx Context) { |
| 101 | + ctx.WriteString("Group Handler") |
| 102 | + }) |
| 103 | + |
| 104 | + // Add valid route |
| 105 | + apiGroup.GET("/users", func(ctx Context) error { |
| 106 | + return ctx.WriteString("Users") |
| 107 | + }) |
| 108 | + |
| 109 | + // Verify group has notFoundHandler set |
| 110 | + xg := apiGroup.(*xGroup) |
| 111 | + if xg.notFoundHandler == nil { |
| 112 | + t.Error("Group should have notFoundHandler set") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// TestMultipleGroupsWithNotFoundHandle tests multiple groups with different handlers |
| 117 | +func TestMultipleGroupsWithNotFoundHandle(t *testing.T) { |
| 118 | + app := New() |
| 119 | + |
| 120 | + // Set global handler |
| 121 | + app.SetNotFoundHandle(func(ctx Context) { |
| 122 | + ctx.WriteString("Global 404") |
| 123 | + }) |
| 124 | + |
| 125 | + // Create API group |
| 126 | + apiGroup := app.HttpServer.Group("/api") |
| 127 | + apiGroup.SetNotFoundHandle(func(ctx Context) { |
| 128 | + ctx.WriteString(`{"code": 404, "message": "API not found"}`) |
| 129 | + }) |
| 130 | + |
| 131 | + // Create Web group |
| 132 | + webGroup := app.HttpServer.Group("/web") |
| 133 | + webGroup.SetNotFoundHandle(func(ctx Context) { |
| 134 | + ctx.WriteString("<h1>404 - Page Not Found</h1>") |
| 135 | + }) |
| 136 | + |
| 137 | + // Verify both groups have handlers |
| 138 | + apiXg := apiGroup.(*xGroup) |
| 139 | + webXg := webGroup.(*xGroup) |
| 140 | + |
| 141 | + if apiXg.notFoundHandler == nil { |
| 142 | + t.Error("API group should have notFoundHandler set") |
| 143 | + } |
| 144 | + if webXg.notFoundHandler == nil { |
| 145 | + t.Error("Web group should have notFoundHandler set") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// TestGroupSetNotFoundHandleReturnsGroup tests that SetNotFoundHandle returns the Group for chaining |
| 150 | +func TestGroupSetNotFoundHandleReturnsGroup(t *testing.T) { |
| 151 | + app := New() |
| 152 | + |
| 153 | + group := app.HttpServer.Group("/api") |
| 154 | + result := group.SetNotFoundHandle(func(ctx Context) { |
| 155 | + ctx.WriteString("404") |
| 156 | + }) |
| 157 | + |
| 158 | + if result == nil { |
| 159 | + t.Error("SetNotFoundHandle should return Group for chaining") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// test helper |
| 164 | +type testHttpWriter http.Header |
| 165 | + |
| 166 | +func (ho testHttpWriter) Header() http.Header { |
| 167 | + return http.Header(ho) |
| 168 | +} |
| 169 | + |
| 170 | +func (ho testHttpWriter) Write(byte []byte) (int, error) { |
| 171 | + return len(byte), nil |
| 172 | +} |
| 173 | + |
| 174 | +func (ho testHttpWriter) WriteHeader(code int) { |
| 175 | +} |
0 commit comments