|
| 1 | +// SPDX-License-Identifier: EUPL-1.2 |
| 2 | + |
| 3 | +package api |
| 4 | + |
| 5 | +import ( |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | +) |
| 12 | + |
| 13 | +// TestWithNoRoute_Good_FiresOnUnknownPath verifies the canonical SPA-host |
| 14 | +// path: a handler set via WithNoRoute catches a request whose path doesn't |
| 15 | +// match any registered route, and the response body is whatever the |
| 16 | +// handler writes (not gin's default 404 body). |
| 17 | +func TestWithNoRoute_Good_FiresOnUnknownPath(t *testing.T) { |
| 18 | + const payload = "SPA-INDEX" |
| 19 | + |
| 20 | + e, err := New(WithNoRoute(func(c *gin.Context) { |
| 21 | + c.String(http.StatusOK, payload) |
| 22 | + })) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("api.New: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + w := httptest.NewRecorder() |
| 28 | + req := httptest.NewRequest(http.MethodGet, "/nonexistent/spa/route", nil) |
| 29 | + e.Handler().ServeHTTP(w, req) |
| 30 | + |
| 31 | + if w.Code != http.StatusOK { |
| 32 | + t.Fatalf("status = %d, want 200", w.Code) |
| 33 | + } |
| 34 | + if got := w.Body.String(); got != payload { |
| 35 | + t.Fatalf("body = %q, want %q", got, payload) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// TestWithNoRoute_Bad_DoesNotShadowExplicitRoutes verifies the registration |
| 40 | +// order — an explicit route handler must win over the NoRoute fallback even |
| 41 | +// when both could plausibly serve the request. |
| 42 | +func TestWithNoRoute_Bad_DoesNotShadowExplicitRoutes(t *testing.T) { |
| 43 | + e, err := New(WithNoRoute(func(c *gin.Context) { |
| 44 | + c.String(http.StatusOK, "fallback") |
| 45 | + })) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("api.New: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + // /health is always registered by build(). |
| 51 | + w := httptest.NewRecorder() |
| 52 | + req := httptest.NewRequest(http.MethodGet, "/health", nil) |
| 53 | + e.Handler().ServeHTTP(w, req) |
| 54 | + |
| 55 | + if w.Code != http.StatusOK { |
| 56 | + t.Fatalf("status = %d, want 200", w.Code) |
| 57 | + } |
| 58 | + if got := w.Body.String(); got == "fallback" { |
| 59 | + t.Fatalf("NoRoute shadowed /health — body = %q", got) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestWithNoRoute_Ugly_NilStaysAsDefault404 verifies the degenerate path — |
| 64 | +// no NoRoute set means gin's default 404 surfaces unchanged. This protects |
| 65 | +// the contract that WithNoRoute is opt-in and unset Engines stay |
| 66 | +// compatible with the pre-WithNoRoute API. |
| 67 | +func TestWithNoRoute_Ugly_NilStaysAsDefault404(t *testing.T) { |
| 68 | + e, err := New() // no WithNoRoute |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("api.New: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + w := httptest.NewRecorder() |
| 74 | + req := httptest.NewRequest(http.MethodGet, "/nope", nil) |
| 75 | + e.Handler().ServeHTTP(w, req) |
| 76 | + |
| 77 | + if w.Code != http.StatusNotFound { |
| 78 | + t.Fatalf("status = %d, want 404", w.Code) |
| 79 | + } |
| 80 | +} |
0 commit comments