Test_RegexHandler_Custom verifies that a custom regex handler can be used
(t *testing.T)
| 565 | |
| 566 | // Test_RegexHandler_Custom verifies that a custom regex handler can be used |
| 567 | func 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. |
| 599 | func Test_RegexHandler_MatchOnlyCompiler(t *testing.T) { |
nothing calls this directly
no test coverage detected