| 812 | } |
| 813 | |
| 814 | func Test_Integration_Domain_WithSubAppMount(t *testing.T) { |
| 815 | t.Parallel() |
| 816 | |
| 817 | app := fiber.New() |
| 818 | app.Use(helmet.New()) |
| 819 | |
| 820 | // Create sub-app with routes |
| 821 | subApp := fiber.New() |
| 822 | subApp.Use(requestid.New()) |
| 823 | subApp.Get("/users", func(c fiber.Ctx) error { |
| 824 | return c.JSON(fiber.Map{"users": []string{"alice", "bob"}}) |
| 825 | }) |
| 826 | subApp.Get("/posts", func(c fiber.Ctx) error { |
| 827 | return c.JSON(fiber.Map{"posts": []string{"post1", "post2"}}) |
| 828 | }) |
| 829 | |
| 830 | // Mount sub-app on domain |
| 831 | app.Domain("api.example.com").Use("/api", subApp) |
| 832 | |
| 833 | // Fallback route |
| 834 | app.Get("/api/users", func(c fiber.Ctx) error { |
| 835 | return c.SendString("default users") |
| 836 | }) |
| 837 | |
| 838 | // Test domain-mounted sub-app |
| 839 | req := httptest.NewRequest(http.MethodGet, "/api/users", http.NoBody) |
| 840 | req.Host = "api.example.com" |
| 841 | resp, err := app.Test(req) |
| 842 | require.NoError(t, err) |
| 843 | require.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 844 | body, err := io.ReadAll(resp.Body) |
| 845 | require.NoError(t, err) |
| 846 | require.Contains(t, string(body), "alice") |
| 847 | require.NotEmpty(t, resp.Header.Get("X-Request-ID")) |
| 848 | require.Equal(t, "nosniff", resp.Header.Get(fiber.HeaderXContentTypeOptions)) |
| 849 | |
| 850 | // Test non-matching domain (fallback) |
| 851 | req = httptest.NewRequest(http.MethodGet, "/api/users", http.NoBody) |
| 852 | req.Host = "www.example.com" |
| 853 | resp, err = app.Test(req) |
| 854 | require.NoError(t, err) |
| 855 | require.Equal(t, fiber.StatusOK, resp.StatusCode) |
| 856 | body, err = io.ReadAll(resp.Body) |
| 857 | require.NoError(t, err) |
| 858 | require.Equal(t, "default users", string(body)) |
| 859 | } |
| 860 | |
| 861 | func Test_Integration_Domain_WithParams(t *testing.T) { |
| 862 | t.Parallel() |