You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 1. Load the OpenAPI 3+ spec into a byte arraypetstore, err:=os.ReadFile("test_specs/petstorev3.json")
iferr!=nil {
panic(err)
}
// 2. Create a new OpenAPI document using libopenapidocument, docErrs:=libopenapi.NewDocument(petstore)
ifdocErrs!=nil {
panic(docErrs)
}
// 3. Create a new validatordocValidator, validatorErrs:=NewValidator(document)
ifvalidatorErrs!=nil {
panic(validatorErrs)
}
// 6. Create a new *http.Request (normally, this would be where the host application will pass in the request)request, _:=http.NewRequest(http.MethodGet, "/pet/findByStatus?status=sold", nil)
// 7. Simulate a request/response, in this case the contract returns a 200 with an array of pets.// Normally, this would be where the host application would pass in the response.recorder:=httptest.NewRecorder()
handler:=func(w http.ResponseWriter, r*http.Request) {
// set return content type.w.Header().Set(helpers.ContentTypeHeader, helpers.JSONContentType)
w.WriteHeader(http.StatusOK)
// create a Petbody:=map[string]interface{}{
"id": 123,
"name": "cotton",
"category": map[string]interface{}{
"id": "NotAValidPetId", // this will fail, it should be an integer."name": "dogs",
},
"photoUrls": []string{"https://pb33f.io"},
}
// marshal the request body into bytes.responseBodyBytes, _:=json.Marshal([]interface{}{body}) // operation returns an array of pets// return the response._, _=w.Write(responseBodyBytes)
}
// simulate request/responsehandler(recorder, request)
// 8. Validate!valid, validationErrs:=docValidator.ValidateHttpRequestResponse(request, recorder.Result())
if!valid {
for_, e:=rangevalidationErrs {
// 5. Handle the errorfmt.Printf("Type: %s, Failure: %s\n", e.ValidationType, e.Message)
fmt.Printf("Schema Error: %s, Line: %d, Col: %d\n",
e.SchemaValidationErrors[0].Reason,
e.SchemaValidationErrors[0].Line,
e.SchemaValidationErrors[0].Column)
}
}
Will print:
Type: response, Failure: 200 response body for '/pet/findByStatus' failed to validate schema
Schema Error: expected integer, but got string, Line: 19, Col: 27
0 commit comments