Skip to content

Commit

Permalink
Merge pull request #74 from hornc/adamcohen-master
Browse files Browse the repository at this point in the history
Print response body when test fails (v2)
  • Loading branch information
hornc committed Jan 13, 2016
2 parents ef88cd6 + 97659f7 commit 2775011
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions apivore.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ $:.push File.expand_path("../lib", __FILE__)

Gem::Specification.new do |s|
s.name = 'apivore'
s.version = '1.4.3'
s.date = '2015-12-08'
s.version = '1.4.5'
s.date = '2016-01-14'
s.summary = "Tests your API against its Swagger 2.0 spec"
s.description = "Tests your rails API using its Swagger description of end-points, models, and query parameters."
s.authors = ["Charles Horn"]
Expand Down
4 changes: 4 additions & 0 deletions lib/apivore/swagger_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def has_response_code_for_path?(path, method, code)
mappings[path][method].has_key?(code.to_s)
end

def response_codes_for_path(path, method)
mappings[path][method].keys.join(", ")
end

def has_matching_document_for(path, method, code, body)
JSON::Validator.fully_validate(
swagger, body, fragment: fragment(path, method, code)
Expand Down
10 changes: 7 additions & 3 deletions lib/apivore/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def matches?(swagger_checker)
)
swagger_checker.response = response
post_checks(swagger_checker)

if has_errors? && response.body.length > 0
errors << "\nResponse body:\n #{JSON.pretty_generate(JSON.parse(response.body))}"
end

swagger_checker.remove_tested_end_point_response(
path, method, expected_response_code
)
Expand All @@ -51,7 +56,6 @@ def apivore_build_path(path, data)
path + (data['_query_string'] ? "?#{data['_query_string']}" : '')
end


def pre_checks(swagger_checker)
check_request_path(swagger_checker)
end
Expand All @@ -71,7 +75,8 @@ def check_request_path(swagger_checker)
elsif !swagger_checker.has_response_code_for_path?(path, method, expected_response_code)
errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\
" a documented response code of #{expected_response_code} at path"\
" #{method} #{path}"
" #{method} #{path}. "\
"\n Available response codes: #{swagger_checker.response_codes_for_path(path, method)}"
elsif method == "get" && swagger_checker.fragment(path, method, expected_response_code).nil?
errors << "Swagger doc: #{swagger_checker.swagger_path} missing"\
" response model for get request with #{path} for code"\
Expand All @@ -83,7 +88,6 @@ def check_status_code
if response.status != expected_response_code
errors << "Path #{path} did not respond with expected status code."\
" Expected #{expected_response_code} got #{response.status}"\
"\nResponse body: #{response.body}"
end
end

Expand Down

0 comments on commit 2775011

Please sign in to comment.