Skip to content

Commit 8f90f97

Browse files
authored
Merge pull request #3683 from gofiber/codex/2025-08-13-15-47-17
🧹 chore: delay routing error creation
2 parents 7c10ce1 + 19711d0 commit 8f90f97

File tree

8 files changed

+44
-45
lines changed

8 files changed

+44
-45
lines changed

client/core_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/gofiber/fiber/v3"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
1312
"github.com/valyala/fasthttp/fasthttputil"
13+
14+
"github.com/gofiber/fiber/v3"
1415
)
1516

1617
func Test_AddMissing_Port(t *testing.T) {
@@ -168,7 +169,7 @@ func Test_Execute(t *testing.T) {
168169

169170
resp, err := core.execute(context.Background(), client, req)
170171
require.NoError(t, err)
171-
require.Equal(t, "Cannot GET /", string(resp.RawResponse.Body()))
172+
require.Equal(t, "Not Found", string(resp.RawResponse.Body()))
172173
})
173174

174175
t.Run("add user response hooks", func(t *testing.T) {
@@ -185,7 +186,7 @@ func Test_Execute(t *testing.T) {
185186

186187
resp, err := core.execute(context.Background(), client, req)
187188
require.NoError(t, err)
188-
require.Equal(t, "Cannot GET /", string(resp.RawResponse.Body()))
189+
require.Equal(t, "Not Found", string(resp.RawResponse.Body()))
189190
})
190191

191192
t.Run("no timeout", func(t *testing.T) {

docs/guide/routing.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ In addition, several parameters in a row and several unnamed parameter character
128128
app.Get("/:sign:param", handler)
129129

130130
// GET /api-v1
131-
// Params: "name" -> "v1"
131+
// Params: "name" -> "v1"
132132
app.Get("/api-:name", handler)
133133

134134
// GET /customer/v1/cart/proxy
@@ -180,7 +180,7 @@ app.Get("/:test<min(5)>", func(c fiber.Ctx) error {
180180
// 12
181181

182182
// curl -X GET http://localhost:3000/1
183-
// Cannot GET /1
183+
// Not Found
184184
```
185185

186186
</TabItem>
@@ -194,10 +194,10 @@ app.Get("/:test<min(100);maxLen(5)>", func(c fiber.Ctx) error {
194194
})
195195

196196
// curl -X GET http://localhost:3000/120000
197-
// Cannot GET /120000
197+
// Not Found
198198

199199
// curl -X GET http://localhost:3000/1
200-
// Cannot GET /1
200+
// Not Found
201201

202202
// curl -X GET http://localhost:3000/250
203203
// 250
@@ -214,10 +214,10 @@ app.Get(`/:date<regex(\d{4}-\d{2}-\d{2})>`, func(c fiber.Ctx) error {
214214
})
215215

216216
// curl -X GET http://localhost:3000/125
217-
// Cannot GET /125
217+
// Not Found
218218

219219
// curl -X GET http://localhost:3000/test
220-
// Cannot GET /test
220+
// Not Found
221221

222222
// curl -X GET http://localhost:3000/2022-08-27
223223
// 2022-08-27
@@ -243,7 +243,7 @@ app.Get("/:test<int>?", func(c fiber.Ctx) error {
243243
// curl -X GET http://localhost:3000/
244244
//
245245
// curl -X GET http://localhost:3000/7.0
246-
// Cannot GET /7.0
246+
// Not Found
247247
```
248248

249249
#### Custom Constraint

hooks_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"testing"
77
"time"
88

9-
"github.com/gofiber/fiber/v3/log"
109
"github.com/stretchr/testify/assert"
1110
"github.com/stretchr/testify/require"
1211
"github.com/valyala/bytebufferpool"
12+
13+
"github.com/gofiber/fiber/v3/log"
1314
)
1415

1516
const testMountPath = "/api"
@@ -418,7 +419,7 @@ func Test_executeOnListenHooks_Error(t *testing.T) {
418419
return errors.New("listen error")
419420
})
420421

421-
err := app.hooks.executeOnListenHooks(ListenData{Host: "127.0.0.1", Port: "80"})
422+
err := app.hooks.executeOnListenHooks(ListenData{Host: "127.0.0.1", Port: "0"})
422423
require.EqualError(t, err, "listen error")
423424
}
424425

middleware/keyauth/keyauth_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010
"strings"
1111
"testing"
1212

13-
"github.com/gofiber/fiber/v3"
1413
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/require"
15+
16+
"github.com/gofiber/fiber/v3"
1617
)
1718

1819
const CorrectKey = "specials: !$%,.#\"!?~`<>@$^*(){}[]|/\\123"
@@ -166,6 +167,7 @@ func Test_AuthSources(t *testing.T) {
166167
require.NoError(t, err, test.description)
167168

168169
body, err := io.ReadAll(res.Body)
170+
169171
require.NoError(t, err)
170172
errClose := res.Body.Close()
171173
require.NoError(t, errClose)
@@ -191,7 +193,7 @@ func Test_AuthSources(t *testing.T) {
191193

192194
if authSource == paramExtractorName && testKey == "" {
193195
expectedCode = 404
194-
expectedBody = "Cannot GET /"
196+
expectedBody = "Not Found"
195197
}
196198
require.Equal(t, expectedCode, res.StatusCode, test.description)
197199
require.Equal(t, expectedBody, string(body), test.description)

middleware/logger/logger_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import (
1818
"testing"
1919
"time"
2020

21-
"github.com/gofiber/fiber/v3"
22-
fiberlog "github.com/gofiber/fiber/v3/log"
23-
"github.com/gofiber/fiber/v3/middleware/requestid"
2421
"github.com/stretchr/testify/require"
2522
"github.com/valyala/bytebufferpool"
2623
"github.com/valyala/fasthttp"
24+
25+
"github.com/gofiber/fiber/v3"
26+
fiberlog "github.com/gofiber/fiber/v3/log"
27+
"github.com/gofiber/fiber/v3/middleware/requestid"
2728
)
2829

2930
const (
@@ -468,7 +469,7 @@ func Test_Logger_All(t *testing.T) {
468469
require.NoError(t, err)
469470
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
470471

471-
expected := fmt.Sprintf("%dHost=example.comhttpHTTP/1.10.0.0.0example.com/?foo=bar/%s%s%s%s%s%s%s%s%sCannot GET /", os.Getpid(), colors.Black, colors.Red, colors.Green, colors.Yellow, colors.Blue, colors.Magenta, colors.Cyan, colors.White, colors.Reset)
472+
expected := fmt.Sprintf("%dHost=example.comhttpHTTP/1.10.0.0.0example.com/?foo=bar/%s%s%s%s%s%s%s%s%sNot Found", os.Getpid(), colors.Black, colors.Red, colors.Green, colors.Yellow, colors.Blue, colors.Magenta, colors.Cyan, colors.White, colors.Reset)
472473
require.Equal(t, expected, buf.String())
473474
}
474475

@@ -1039,7 +1040,7 @@ func Test_Logger_ForceColors(t *testing.T) {
10391040
require.NoError(t, err)
10401041
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
10411042

1042-
expected := fmt.Sprintf("0.0.0.0%s404%s%sGET%s/%sCannot GET /%s\n", colors.Yellow, colors.Reset, colors.Cyan, colors.Reset, colors.Red, colors.Reset)
1043+
expected := fmt.Sprintf("0.0.0.0%s404%s%sGET%s/%sNot Found%s\n", colors.Yellow, colors.Reset, colors.Cyan, colors.Reset, colors.Red, colors.Reset)
10431044
require.Equal(t, expected, buf.String())
10441045
}
10451046

middleware/static/static_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func Test_Static_Index_Default(t *testing.T) {
5959

6060
body, err = io.ReadAll(resp.Body)
6161
require.NoError(t, err)
62-
require.Equal(t, "Cannot GET /not-found", string(body))
62+
require.Equal(t, "Not Found", string(body))
6363
}
6464

6565
// go test -run Test_Static_Index
@@ -178,7 +178,7 @@ func Test_Static_Disable_Cache(t *testing.T) {
178178

179179
body, err = io.ReadAll(resp.Body)
180180
require.NoError(t, err)
181-
require.Equal(t, "Cannot GET /test.txt", string(body))
181+
require.Equal(t, "Not Found", string(body))
182182
}
183183

184184
func Test_Static_NotFoundHandler(t *testing.T) {
@@ -962,13 +962,13 @@ func Test_Static_PathTraversal(t *testing.T) {
962962
body, err := io.ReadAll(resp.Body)
963963
require.NoError(t, err)
964964

965-
// If we got a 404, we expect the "Cannot GET" message because that's how fiber handles NotFound by default.
965+
// If we got a 404, we expect the "Not Found" message because that's how fiber handles NotFound by default.
966966
if status == 404 {
967-
require.Contains(t, string(body), "Cannot GET",
968-
"Blocked traversal should have a Cannot GET message for %s", path)
967+
require.Contains(t, string(body), "Not Found",
968+
"Blocked traversal should have a \"Not Found\" message for %s", path)
969969
} else {
970970
require.Contains(t, string(body), "Are you a hacker?",
971-
"Blocked traversal should have a Cannot GET message for %s", path)
971+
"Blocked traversal should have a \"Not Found\" message for %s", path)
972972
}
973973
}
974974

@@ -1066,15 +1066,15 @@ func Test_Static_PathTraversal_WindowsOnly(t *testing.T) {
10661066
require.Containsf(t, []int{400, 404}, status,
10671067
"Status code for path traversal %s should be 400 or 404, got %d", path, status)
10681068

1069-
// If it's a 404, we expect a "Cannot GET" message
1069+
// If it's a 404, we expect a "Not Found" message
10701070
if status == 404 {
10711071
respBody, err := io.ReadAll(resp.Body)
10721072
require.NoError(t, err)
1073-
require.Contains(t, string(respBody), "Cannot GET",
1074-
"Blocked traversal should have a 'Cannot GET' message for %s", path)
1073+
require.Contains(t, string(respBody), "Not Found",
1074+
"Blocked traversal should have a \"Not Found\" message for %s", path)
10751075
} else {
10761076
require.Contains(t, string(body), "Are you a hacker?",
1077-
"Blocked traversal should have a Cannot GET message for %s", path)
1077+
"Blocked traversal should have a \"Not Found\" message for %s", path)
10781078
}
10791079
}
10801080

router.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package fiber
77
import (
88
"bytes"
99
"fmt"
10-
"html"
1110
"slices"
1211
"sync/atomic"
1312

@@ -117,7 +116,6 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
117116
lenr := len(tree) - 1
118117

119118
indexRoute := c.indexRoute
120-
var err error
121119

122120
// Loop over the route stack starting from previous index
123121
for indexRoute < lenr {
@@ -153,12 +151,10 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
153151
}
154152

155153
// If c.Next() does not match, return 404
156-
err = NewError(StatusNotFound, "Cannot "+c.Method()+" "+html.EscapeString(c.getPathOriginal()))
157-
158154
// If no match, scan stack again if other methods match the request
159155
// Moved from app.handler because middleware may break the route chain
160156
if c.matched {
161-
return false, err
157+
return false, ErrNotFound
162158
}
163159

164160
exists := false
@@ -201,9 +197,9 @@ func (app *App) next(c *DefaultCtx) (bool, error) {
201197
c.indexRoute = indexRoute
202198
}
203199
if exists {
204-
err = ErrMethodNotAllowed
200+
return false, ErrMethodNotAllowed
205201
}
206-
return false, err
202+
return false, ErrNotFound
207203
}
208204

209205
func (app *App) nextCustom(c CustomCtx) (bool, error) {
@@ -217,7 +213,6 @@ func (app *App) nextCustom(c CustomCtx) (bool, error) {
217213
lenr := len(tree) - 1
218214

219215
indexRoute := c.getIndexRoute()
220-
var err error
221216

222217
// Loop over the route stack starting from previous index
223218
for indexRoute < lenr {
@@ -251,12 +246,10 @@ func (app *App) nextCustom(c CustomCtx) (bool, error) {
251246
}
252247

253248
// If c.Next() does not match, return 404
254-
err = NewError(StatusNotFound, "Cannot "+c.Method()+" "+html.EscapeString(c.getPathOriginal()))
255-
256249
// If no match, scan stack again if other methods match the request
257250
// Moved from app.handler because middleware may break the route chain
258251
if c.getMatched() {
259-
return false, err
252+
return false, ErrNotFound
260253
}
261254

262255
exists := false
@@ -299,9 +292,9 @@ func (app *App) nextCustom(c CustomCtx) (bool, error) {
299292
c.setIndexRoute(indexRoute)
300293
}
301294
if exists {
302-
err = ErrMethodNotAllowed
295+
return false, ErrMethodNotAllowed
303296
}
304-
return false, err
297+
return false, ErrNotFound
305298
}
306299

307300
func (app *App) requestHandler(rctx *fasthttp.RequestCtx) {

router_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func Test_Router_NotFound(t *testing.T) {
395395
appHandler(c)
396396

397397
require.Equal(t, 404, c.Response.StatusCode())
398-
require.Equal(t, "Cannot DELETE /this/route/does/not/exist", string(c.Response.Body()))
398+
require.Equal(t, "Not Found", string(c.Response.Body()))
399399
}
400400

401401
func Test_Router_NotFound_HTML_Inject(t *testing.T) {
@@ -413,7 +413,7 @@ func Test_Router_NotFound_HTML_Inject(t *testing.T) {
413413
appHandler(c)
414414

415415
require.Equal(t, 404, c.Response.StatusCode())
416-
require.Equal(t, "Cannot DELETE /does/not/exist&lt;script&gt;alert(&#39;foo&#39;);&lt;/script&gt;", string(c.Response.Body()))
416+
require.Equal(t, "Not Found", string(c.Response.Body()))
417417
}
418418

419419
func registerTreeManipulationRoutes(app *App, middleware ...func(Ctx) error) {
@@ -911,6 +911,7 @@ func Benchmark_App_MethodNotAllowed(b *testing.B) {
911911

912912
// go test -v ./... -run=^$ -bench=Benchmark_Router_NotFound -benchmem -count=4
913913
func Benchmark_Router_NotFound(b *testing.B) {
914+
b.ReportAllocs()
914915
app := New()
915916
app.Use(func(c Ctx) error {
916917
return c.Next()
@@ -926,7 +927,7 @@ func Benchmark_Router_NotFound(b *testing.B) {
926927
appHandler(c)
927928
}
928929
require.Equal(b, 404, c.Response.StatusCode())
929-
require.Equal(b, "Cannot DELETE /this/route/does/not/exist", string(c.Response.Body()))
930+
require.Equal(b, "Not Found", string(c.Response.Body()))
930931
}
931932

932933
// go test -v ./... -run=^$ -bench=Benchmark_Router_Handler -benchmem -count=4

0 commit comments

Comments
 (0)