Skip to content

Commit

Permalink
Add missing tests on Analyzer.Analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
Peltoche committed Oct 13, 2018
1 parent 84181d2 commit 2af348c
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (t *Analyzer) validateResponse(res *http.Response, resSpec *spec.Responses)
var input interface{}
err = json.Unmarshal(body, &input)
if err != nil {
return fmt.Errorf("failed to parse json body: %s", err)
return fmt.Errorf("validation failure list:\nfailed to parse response body: %s", err)
}

err = validate.AgainstSchema(response.Schema, input, strfmt.Default)
Expand Down
132 changes: 132 additions & 0 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,135 @@ func Test_Analyzer_Analyze_with_missing_header(t *testing.T) {
assert.EqualError(t, err, "validation failure list:\n"+
"userID in header is required")
}

func Test_Analyzer_Analyze_with_unrequired_body(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("POST", "/pet", strings.NewReader(`{
"name": "foobar",
"photoUrls": ["tutu"]
}`))
require.NoError(t, err)

body := `{}`

res := &http.Response{
Status: http.StatusText(http.StatusCreated),
StatusCode: http.StatusCreated,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Request: req,
Header: make(http.Header, 0),
}

err = analyzer.Analyze(req, res)

assert.EqualError(t, err, "validation failure list:\n"+
`no response body defined inside the specs but have "{}"`)
}

func Test_Analyzer_Analyze_with_unknown_response_status(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/findByStatus", nil)
require.NoError(t, err)

q := req.URL.Query()
q.Set("status", "available")
req.URL.RawQuery = q.Encode()

body := `[]`

res := &http.Response{
Status: http.StatusText(http.StatusTeapot),
StatusCode: http.StatusTeapot,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Request: req,
Header: make(http.Header, 0),
}

err = analyzer.Analyze(req, res)

assert.EqualError(t, err, "validation failure list:\n"+
"response status I'm a teapot not defined inside the specs")
}

func Test_Analyzer_Analyze_with_unmarshalable_response_body(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/findByStatus", nil)
require.NoError(t, err)

q := req.URL.Query()
q.Set("status", "available")
req.URL.RawQuery = q.Encode()

body := `invalid-json`

res := &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Request: req,
Header: make(http.Header, 0),
}

err = analyzer.Analyze(req, res)

assert.EqualError(t, err, "validation failure list:\n"+
"failed to parse response body: invalid character 'i' looking for beginning of value")
}

func Test_Analyzer_Analyze_with_invalid_response_body(t *testing.T) {
specs, err := NewSpecsFromFile("./dataset/petstore.json")
require.NoError(t, err)

analyzer := NewAnalyzer(specs)

req, err := http.NewRequest("GET", "/pet/findByStatus", nil)
require.NoError(t, err)

q := req.URL.Query()
q.Set("status", "available")
req.URL.RawQuery = q.Encode()

// Should be an array
body := `{}`

res := &http.Response{
Status: http.StatusText(http.StatusOK),
StatusCode: http.StatusOK,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Body: ioutil.NopCloser(bytes.NewBufferString(body)),
ContentLength: int64(len(body)),
Request: req,
Header: make(http.Header, 0),
}

err = analyzer.Analyze(req, res)

assert.EqualError(t, err, "validation failure list:\n"+
` in body must be of type array: "object"`)
}

0 comments on commit 2af348c

Please sign in to comment.