Merge pull request #57 from horiaradu/master

Response body value validation
This commit is contained in:
domaindrivendev 2017-04-29 12:46:31 -07:00 committed by GitHub
commit 25d8adaf8b
7 changed files with 44 additions and 10 deletions

View File

@ -8,6 +8,7 @@ PATH
remote: ./rswag-specs
specs:
rswag-specs (1.2.0)
json (~> 1.8)
json-schema (~> 2.2)
rails (>= 3.1, < 5.1)
rspec-rails (>= 2.14, < 4)

View File

@ -103,6 +103,17 @@ If you've used [Swagger](http://swagger.io/specification) before, then the synta
Take special note of the __run_test!__ method that's called within each response block. This tells rswag to create and execute a corresponding example. It builds and submits a request based on parameter descriptions and corresponding values that have been provided using the rspec "let" syntax. For example, the "post" description in the example above specifies a "body" parameter called "blog". It also lists 2 different responses. For the success case (i.e. the 201 response), notice how "let" is used to set the blog parameter to a value that matches the provided schema. For the failure case (i.e. the 422 response), notice how it's set to a value that does not match the provided schema. When the test is executed, rswag also validates the actual response code and, where applicable, the response body against the provided [JSON Schema](http://json-schema.org/documentation.html).
If you want to do additional validation on the response, pass a block to the __run_test!__ method:
```ruby
response '201', 'blog created' do
run_test! do |response|
data = JSON.parse(response.body)
expect(data['title']).to eq('foo')
end
end
```
If you'd like your specs to be a little more explicit about what's going on here, you can replace the call to __run_test!__ with equivalent "before" and "it" blocks:
```ruby

View File

@ -69,7 +69,7 @@ module Rswag
metadata[:response][:examples] = example
end
def run_test!
def run_test!(&block)
# NOTE: rspec 2.x support
if RSPEC_VERSION < 3
before do
@ -77,7 +77,7 @@ module Rswag
end
it "returns a #{metadata[:response][:code]} response" do
assert_response_matches_metadata(example.metadata)
assert_response_matches_metadata(example.metadata, &block)
end
else
before do |example|
@ -85,7 +85,7 @@ module Rswag
end
it "returns a #{metadata[:response][:code]} response" do |example|
assert_response_matches_metadata(example.metadata)
assert_response_matches_metadata(example.metadata, &block)
end
end
end

View File

@ -28,10 +28,10 @@ module Rswag
end
end
def assert_response_matches_metadata(api_metadata)
def assert_response_matches_metadata(api_metadata, &block)
global_metadata = rswag_config.get_swagger_doc(api_metadata[:swagger_doc])
validator = ResponseValidator.new(api_metadata, global_metadata)
validator.validate!(response)
validator.validate!(response, &block)
end
private

View File

@ -1,5 +1,6 @@
require 'active_support/core_ext/hash/slice'
require 'json-schema'
require 'json'
require 'rswag/specs/extended_schema'
module Rswag
@ -11,10 +12,11 @@ module Rswag
@global_metadata = global_metadata
end
def validate!(response)
def validate!(response, &block)
validate_code!(response.code)
validate_headers!(response.headers)
validate_body!(response.body)
validate_body!(response.body, &block)
block.call(response) if block_given?
end
private

View File

@ -17,6 +17,7 @@ Gem::Specification.new do |s|
s.files = Dir["{lib}/**/*"] + ["MIT-LICENSE", "Rakefile" ]
s.add_dependency "rails", ">= 3.1", "< 5.1"
s.add_dependency 'json', '~> 1.8'
s.add_dependency 'json-schema', '~> 2.2'
s.add_dependency 'rspec-rails', '>= 2.14', '< 4'
end

View File

@ -42,6 +42,25 @@ module Rswag
let(:response) { OpenStruct.new(code: 200, body: "{\"foo\":\"Some comment\"}") }
it { expect { call }.to raise_error UnexpectedResponse }
end
context "'block' provided" do
let(:call) do
subject.validate!(response) do |response|
data = JSON.parse(response.body)
expect(data['text']).to eq('Some comment')
end
end
context 'the block validation passes' do
let(:response) { OpenStruct.new(code: 200, body: "{\"text\":\"Some comment\"}") }
it { expect { call }.to_not raise_error }
end
context 'the block validation fails' do
let(:response) { OpenStruct.new(code: 200, body: "{\"text\":\"Some other comment\"}") }
it { expect { call }.to raise_error(RSpec::Expectations::ExpectationNotMetError) }
end
end
end
context "referenced 'schema' provided" do