Merge branch 'master' into fixdepnotice

This commit is contained in:
domaindrivendev
2017-06-26 17:26:28 -07:00
committed by GitHub
28 changed files with 201 additions and 188 deletions

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

@@ -3,6 +3,7 @@ require 'json-schema'
module Rswag
module Specs
class ExtendedSchema < JSON::Schema::Draft4
def initialize
super
@attributes['type'] = ExtendedTypeAttribute
@@ -12,6 +13,7 @@ module Rswag
end
class ExtendedTypeAttribute < JSON::Schema::TypeV4Attribute
def self.validate(current_schema, data, fragments, processor, validator, options={})
return if data.nil? && current_schema.schema['x-nullable'] == true
super

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

@@ -1,5 +1,5 @@
module Rswag
module Specs
VERSION = '1.2.0'
VERSION = '1.2.1'
end
end

View File

@@ -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.2"
s.add_dependency 'json', '~> 1.8'
s.add_dependency 'json-schema', '~> 2.2'
s.add_dependency 'rspec-rails', '>= 2.14', '< 4'
end

View File

@@ -29,7 +29,7 @@ module Rswag
api_metadata[:response][:schema] = {
type: 'object',
properties: { text: { type: 'string' } },
required: [ 'text' ]
required: ['text']
}
end
@@ -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
@@ -51,7 +70,7 @@ module Rswag
author: {
type: 'object',
properties: { name: { type: 'string' } },
required: [ 'name' ]
required: ['name']
}
}
end