MCPcopy
hub / github.com/gofiber/fiber / Test_RegexHandler_Custom

Function Test_RegexHandler_Custom

path_test.go:567–596  ·  view source on GitHub ↗

Test_RegexHandler_Custom verifies that a custom regex handler can be used

(t *testing.T)

Source from the content-addressed store, hash-verified

565
566// Test_RegexHandler_Custom verifies that a custom regex handler can be used
567func Test_RegexHandler_Custom(t *testing.T) {
568 t.Parallel()
569
570 var lastPattern string
571 var compileCalled bool
572
573 // Create app with custom regex handler
574 app := New(Config{
575 RegexHandler: mockRegexHandler(&lastPattern, &compileCalled),
576 })
577
578 // Register a route with regex constraint
579 app.Get("/api/:id<regex(\\d+)>", func(c Ctx) error {
580 return c.SendString("matched")
581 })
582
583 // Verify the mock handler was used during route registration
584 require.True(t, compileCalled, "RegexHandler should have been called")
585 require.Equal(t, `\d+`, lastPattern, "Pattern should match")
586
587 // Test the route
588 resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/api/123", http.NoBody))
589 require.NoError(t, err)
590 require.Equal(t, StatusOK, resp.StatusCode)
591
592 // Test with non-matching pattern
593 resp, err = app.Test(httptest.NewRequest(http.MethodGet, "/api/abc", http.NoBody))
594 require.NoError(t, err)
595 require.Equal(t, StatusNotFound, resp.StatusCode)
596}
597
598// Test_RegexHandler_MatchOnlyCompiler verifies Fiber accepts compilers that only implement MatchString.
599func Test_RegexHandler_MatchOnlyCompiler(t *testing.T) {

Callers

nothing calls this directly

Calls 5

mockRegexHandlerFunction · 0.85
TestMethod · 0.80
NewFunction · 0.70
GetMethod · 0.65
SendStringMethod · 0.65

Tested by

no test coverage detected