diff --git a/mock/mock_engine.go b/mock/mock_engine.go index e504608..1e55019 100644 --- a/mock/mock_engine.go +++ b/mock/mock_engine.go @@ -348,6 +348,11 @@ func (rme *ResponseMockEngine) runWorkflow(request *http.Request) ([]byte, int, ), 200, err } + // check for wiretap-status-code in header and override the code, regardless of what was found in the spec. + if statusCode := request.Header.Get("wiretap-status-code"); statusCode != "" { + c, _ = strconv.Atoi(statusCode) + } + return mock, c, nil } diff --git a/mock/mock_engine_test.go b/mock/mock_engine_test.go index c30fdb5..ea43917 100644 --- a/mock/mock_engine_test.go +++ b/mock/mock_engine_test.go @@ -1232,3 +1232,48 @@ paths: assert.Equal(t, "1", items[0]) } + +// https://github.com/pb33f/wiretap/issues/89 +func TestNewMockEngine_OverrideStatusCode_Issue89(t *testing.T) { + + spec := `openapi: 3.1.0 +paths: + /test: + get: + responses: + '200': + content: + application/json: + schema: + $ref: '#/components/schemas/Thing' +components: + schemas: + Thing: + type: object + properties: + name: + type: string + description: + type: string +` + + d, _ := libopenapi.NewDocument([]byte(spec)) + doc, _ := d.BuildV3Model() + + me := NewMockEngine(&doc.Model, false) + + request, _ := http.NewRequest(http.MethodGet, "https://api.pb33f.io/test", nil) + request.Header.Set("wiretap-status-code", "418") + + b, status, err := me.GenerateResponse(request) + + assert.NoError(t, err) + assert.Equal(t, 418, status) + + var decoded map[string]any + _ = json.Unmarshal(b, &decoded) + + assert.NotEmpty(t, decoded["name"]) + assert.NotEmpty(t, decoded["description"]) + +}