Skip to content

Commit

Permalink
Merge pull request #23 from StephanHCB/issue-21-fix-nonjson-body
Browse files Browse the repository at this point in the history
fix(#21): support non-json body types
  • Loading branch information
StephanHCB committed Jul 21, 2023
2 parents 0506148 + c5d929e commit cc43337
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions implementation/verifier/verifier.go
Expand Up @@ -113,9 +113,27 @@ func (c *VerifierImpl) Perform(ctx context.Context, method string, requestUrl st
response.Status = mockResponse.Status
response.Time = mockResponse.Time
if response.Body != nil && mockResponse.Body != nil {
// copy over through json round trip
marshalled, _ := json.Marshal(mockResponse.Body)
_ = json.Unmarshal(marshalled, response.Body)
switch response.Body.(type) {
case **[]byte:
if asString, ok := mockResponse.Body.(string); ok {
// allow strings containing anything
asBytes := []byte(asString)
*(response.Body.(**[]byte)) = &asBytes
} else {
// if given a structure, marshal to json and return that
marshalled, _ := json.Marshal(mockResponse.Body)
*(response.Body.(**[]byte)) = &marshalled
}
default:
if asString, ok := mockResponse.Body.(string); ok {
// allow strings containing a json doc
_ = json.Unmarshal([]byte(asString), response.Body)
} else {
// copy over through json round trip
marshalled, _ := json.Marshal(mockResponse.Body)
_ = json.Unmarshal(marshalled, response.Body)
}
}
}

return nil
Expand Down

0 comments on commit cc43337

Please sign in to comment.