Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.1.11 #118

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mock/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
45 changes: 45 additions & 0 deletions mock/mock_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

}
Loading