|
| 1 | +package oasmiddleware_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/getkin/kin-openapi/routers" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + api "github.com/openmeterio/openmeter/api/v3" |
| 13 | + "github.com/openmeterio/openmeter/api/v3/oasmiddleware" |
| 14 | +) |
| 15 | + |
| 16 | +// TestValidateResponse_Violation proves that the ValidateResponse middleware fires its |
| 17 | +// error hook when a handler returns a response body that violates the OpenAPI spec. |
| 18 | +// |
| 19 | +// GET /openmeter/addons requires a 200 body with both "data" and "meta" fields. |
| 20 | +// Returning {} omits both required fields and must trigger a violation. |
| 21 | +func TestValidateResponse_Violation(t *testing.T) { |
| 22 | + swagger, err := api.GetSwagger() |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + swagger.Servers = nil |
| 26 | + |
| 27 | + router, err := oasmiddleware.NewValidationRouter(t.Context(), swagger, &oasmiddleware.ValidationRouterOpts{ |
| 28 | + DeleteServers: true, |
| 29 | + }) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + var gotErr error |
| 33 | + mw := oasmiddleware.ValidateResponse(router, oasmiddleware.ValidateResponseOption{ |
| 34 | + ResponseValidationErrorHook: func(err error, r *http.Request) { |
| 35 | + gotErr = err |
| 36 | + }, |
| 37 | + }) |
| 38 | + |
| 39 | + badHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 40 | + w.Header().Set("Content-Type", "application/json") |
| 41 | + w.WriteHeader(http.StatusOK) |
| 42 | + _, _ = w.Write([]byte(`{}`)) |
| 43 | + }) |
| 44 | + |
| 45 | + req := httptest.NewRequest(http.MethodGet, "/openmeter/addons", nil) |
| 46 | + rec := httptest.NewRecorder() |
| 47 | + |
| 48 | + mw(badHandler).ServeHTTP(rec, req) |
| 49 | + |
| 50 | + assert.Error(t, gotErr, "expected a validation error for missing required fields") |
| 51 | + t.Logf("validation error (expected): %v", gotErr) |
| 52 | +} |
| 53 | + |
| 54 | +// TestValidateResponse_Clean proves that a well-formed response does not trigger the error hook. |
| 55 | +func TestValidateResponse_Clean(t *testing.T) { |
| 56 | + swagger, err := api.GetSwagger() |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + swagger.Servers = nil |
| 60 | + |
| 61 | + router, err := oasmiddleware.NewValidationRouter(t.Context(), swagger, &oasmiddleware.ValidationRouterOpts{ |
| 62 | + DeleteServers: true, |
| 63 | + }) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + var gotErr error |
| 67 | + mw := oasmiddleware.ValidateResponse(router, oasmiddleware.ValidateResponseOption{ |
| 68 | + ResponseValidationErrorHook: func(err error, r *http.Request) { |
| 69 | + gotErr = err |
| 70 | + }, |
| 71 | + }) |
| 72 | + |
| 73 | + goodHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 74 | + w.Header().Set("Content-Type", "application/json") |
| 75 | + w.WriteHeader(http.StatusOK) |
| 76 | + _, _ = w.Write([]byte(`{"data":[],"meta":{"page":{"number":0,"size":100,"total":0}}}`)) |
| 77 | + }) |
| 78 | + |
| 79 | + req := httptest.NewRequest(http.MethodGet, "/openmeter/addons", nil) |
| 80 | + rec := httptest.NewRecorder() |
| 81 | + |
| 82 | + mw(goodHandler).ServeHTTP(rec, req) |
| 83 | + |
| 84 | + assert.NoError(t, gotErr, "expected no validation error for a well-formed response") |
| 85 | +} |
| 86 | + |
| 87 | +// TestValidateResponse_RouteFilterSkipsValidation proves that when RouteFilterHook returns false, |
| 88 | +// the response body is neither buffered nor validated — even if it would otherwise violate the spec. |
| 89 | +// The filter is the per-route gate that lets callers (e.g. unstable-only mode) avoid the |
| 90 | +// buffering overhead on routes they don't care about. |
| 91 | +func TestValidateResponse_RouteFilterSkipsValidation(t *testing.T) { |
| 92 | + swagger, err := api.GetSwagger() |
| 93 | + require.NoError(t, err) |
| 94 | + |
| 95 | + swagger.Servers = nil |
| 96 | + |
| 97 | + router, err := oasmiddleware.NewValidationRouter(t.Context(), swagger, &oasmiddleware.ValidationRouterOpts{ |
| 98 | + DeleteServers: true, |
| 99 | + }) |
| 100 | + require.NoError(t, err) |
| 101 | + |
| 102 | + var ( |
| 103 | + gotErr error |
| 104 | + filteredRoute *routers.Route |
| 105 | + ) |
| 106 | + mw := oasmiddleware.ValidateResponse(router, oasmiddleware.ValidateResponseOption{ |
| 107 | + RouteFilterHook: func(route *routers.Route) bool { |
| 108 | + filteredRoute = route |
| 109 | + return false |
| 110 | + }, |
| 111 | + ResponseValidationErrorHook: func(err error, r *http.Request) { |
| 112 | + gotErr = err |
| 113 | + }, |
| 114 | + }) |
| 115 | + |
| 116 | + // Body that would fail validation if the filter let it through. |
| 117 | + badHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 118 | + w.Header().Set("Content-Type", "application/json") |
| 119 | + w.WriteHeader(http.StatusOK) |
| 120 | + _, _ = w.Write([]byte(`{}`)) |
| 121 | + }) |
| 122 | + |
| 123 | + req := httptest.NewRequest(http.MethodGet, "/openmeter/addons", nil) |
| 124 | + rec := httptest.NewRecorder() |
| 125 | + |
| 126 | + mw(badHandler).ServeHTTP(rec, req) |
| 127 | + |
| 128 | + require.NotNil(t, filteredRoute, "RouteFilterHook should have been invoked with the matched route") |
| 129 | + assert.Equal(t, "/openmeter/addons", filteredRoute.Path) |
| 130 | + assert.NoError(t, gotErr, "validation error hook must not fire when the filter returns false") |
| 131 | + assert.Equal(t, http.StatusOK, rec.Code, "client response should still be served") |
| 132 | + assert.Equal(t, `{}`, rec.Body.String(), "client response body should still be served") |
| 133 | +} |
0 commit comments