Skip to content

Commit

Permalink
Merge pull request #91 from k2tzumi/fix-open-api-validator
Browse files Browse the repository at this point in the history
Improvement of unstable operation during OAS Spec verification
  • Loading branch information
k1LoW committed Aug 1, 2022
2 parents 0605ba4 + 9be2e71 commit 72b6081
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
11 changes: 11 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ func (rnr *httpRunner) Run(ctx context.Context, r *httpRequest) error {
if err := rnr.validator.ValidateRequest(ctx, req); err != nil {
return err
}
// reset Request.Body
reqBody, err := r.encodeBody()
if err != nil {
return err
}
rc, ok := reqBody.(io.ReadCloser)
if !ok && reqBody != nil {
rc = io.NopCloser(reqBody)
}
req.Body = rc

res, err = rnr.client.Do(req)
if err != nil {
return err
Expand Down
19 changes: 17 additions & 2 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
}
endpoint := "https://api.github.com"
tests := []struct {
req *httpRequest
want int
req *httpRequest
useOpenApi3Validator bool
want int
}{
{
&httpRequest{
Expand All @@ -31,6 +32,7 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
"Authorization": fmt.Sprintf("token %s", os.Getenv("GITHUB_TOKEN")),
},
},
true,
http.StatusOK,
},
{
Expand All @@ -40,6 +42,7 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
mediaType: MediaTypeApplicationJSON,
headers: map[string]string{},
},
false,
http.StatusNotFound,
},
}
Expand All @@ -54,6 +57,18 @@ func TestHTTPRunnerRunUsingGitHubAPI(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if tt.useOpenApi3Validator {
c := &httpRunnerConfig{
OpenApi3DocLocation: "testdata/openapi3.yml",
SkipValidateRequest: false,
SkipValidateResponse: false,
}
v, err := newHttpValidator(c)
if err != nil {
t.Fatal(err)
}
r.validator = v
}
if err := r.Run(ctx, tt.req); err != nil {
t.Error(err)
continue
Expand Down
16 changes: 7 additions & 9 deletions testdata/openapi3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ paths:
content:
application/json:
schema:
type: object
properties:
data:
type: object
properties:
username:
type: string
required:
- username
- email
login:
type: string
id:
type: integer
required:
- data
- login
- id

0 comments on commit 72b6081

Please sign in to comment.