mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-23 06:16:42 +00:00
Response body value validation
Add the possibility to pass a block to the run_test! method in order to add expectations on your response
This commit is contained in:
parent
cf9170101b
commit
51c9f4e5e6
@ -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)
|
||||
|
||||
10
README.md
10
README.md
@ -103,6 +103,16 @@ 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 JSON response, pass a block to the __run_test!__ method:
|
||||
|
||||
```ruby
|
||||
response '201', 'blog created' do
|
||||
run_test! do |body|
|
||||
expect(body['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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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,10 @@ 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)
|
||||
end
|
||||
|
||||
private
|
||||
@ -34,7 +35,7 @@ module Rswag
|
||||
end
|
||||
end
|
||||
|
||||
def validate_body!(body)
|
||||
def validate_body!(body, &block)
|
||||
response_schema = @api_metadata[:response][:schema]
|
||||
return if response_schema.nil?
|
||||
|
||||
@ -46,6 +47,8 @@ module Rswag
|
||||
rescue JSON::Schema::ValidationError => ex
|
||||
raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}"
|
||||
end
|
||||
|
||||
block.call(JSON.parse(body)) if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -16,7 +16,8 @@ Gem::Specification.new do |s|
|
||||
|
||||
s.files = Dir["{lib}/**/*"] + ["MIT-LICENSE", "Rakefile" ]
|
||||
|
||||
s.add_dependency "rails", ">= 3.1", "< 5.1"
|
||||
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
|
||||
|
||||
@ -29,7 +29,7 @@ module Rswag
|
||||
api_metadata[:response][:schema] = {
|
||||
type: 'object',
|
||||
properties: { text: { type: 'string' } },
|
||||
required: [ 'text' ]
|
||||
required: ['text']
|
||||
}
|
||||
end
|
||||
|
||||
@ -42,6 +42,24 @@ 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 |body|
|
||||
expect(body['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
|
||||
@ -51,7 +69,7 @@ module Rswag
|
||||
author: {
|
||||
type: 'object',
|
||||
properties: { name: { type: 'string' } },
|
||||
required: [ 'name' ]
|
||||
required: ['name']
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user