Integration tests for domain routing
(t *testing.T)
| 702 | // Integration tests for domain routing |
| 703 | |
| 704 | func Test_Integration_Domain_WithMiddleware(t *testing.T) { |
| 705 | t.Parallel() |
| 706 | |
| 707 | app := fiber.New() |
| 708 | app.Use(helmet.New()) |
| 709 | app.Use(requestid.New()) |
| 710 | |
| 711 | // Domain-specific route with middleware |
| 712 | app.Domain("api.example.com").Get("/users", func(c fiber.Ctx) error { |
| 713 | return c.SendString("api users") |
| 714 | }) |
| 715 | |
| 716 | // Fallback route |
| 717 | app.Get("/users", func(c fiber.Ctx) error { |
| 718 | return c.SendString("default users") |
| 719 | }) |
| 720 | |
| 721 | // Test matching domain |
| 722 | req := httptest.NewRequest(http.MethodGet, "/users", http.NoBody) |
| 723 | req.Host = "api.example.com" |
| 724 | resp, err := app.Test(req) |
| 725 | require.NoError(t, err) |
| 726 | require.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 727 | body, err := io.ReadAll(resp.Body) |
| 728 | require.NoError(t, err) |
| 729 | require.Equal(t, "api users", string(body)) |
| 730 | require.NotEmpty(t, resp.Header.Get("X-Request-ID")) |
| 731 | require.Equal(t, "nosniff", resp.Header.Get(fiber.HeaderXContentTypeOptions)) |
| 732 | |
| 733 | // Test non-matching domain (fallback) |
| 734 | req = httptest.NewRequest(http.MethodGet, "/users", http.NoBody) |
| 735 | req.Host = "www.example.com" |
| 736 | resp, err = app.Test(req) |
| 737 | require.NoError(t, err) |
| 738 | require.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 739 | body, err = io.ReadAll(resp.Body) |
| 740 | require.NoError(t, err) |
| 741 | require.Equal(t, "default users", string(body)) |
| 742 | } |
| 743 | |
| 744 | func Test_Integration_Domain_WithCORS(t *testing.T) { |
| 745 | t.Parallel() |