From f195c8275988e5061e01b21d2f38c5eae8455554 Mon Sep 17 00:00:00 2001 From: Horia Radu Date: Sat, 29 Apr 2017 21:17:53 +0300 Subject: [PATCH] yield entire response instead of only the body --- README.md | 2 +- rswag-specs/lib/rswag/specs/response_validator.rb | 5 ++--- rswag-specs/spec/rswag/specs/response_validator_spec.rb | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8d4355c..be69bbb 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ If you want to do additional validation on the response, pass a block to the __r ```ruby response '201', 'blog created' do run_test! do |response| - data = JSON.parse(response) + data = JSON.parse(response.body) expect(data['title']).to eq('foo') end end diff --git a/rswag-specs/lib/rswag/specs/response_validator.rb b/rswag-specs/lib/rswag/specs/response_validator.rb index 2e75097..7194953 100644 --- a/rswag-specs/lib/rswag/specs/response_validator.rb +++ b/rswag-specs/lib/rswag/specs/response_validator.rb @@ -16,6 +16,7 @@ module Rswag validate_code!(response.code) validate_headers!(response.headers) validate_body!(response.body, &block) + block.call(response) if block_given? end private @@ -35,7 +36,7 @@ module Rswag end end - def validate_body!(body, &block) + def validate_body!(body) response_schema = @api_metadata[:response][:schema] return if response_schema.nil? @@ -47,8 +48,6 @@ module Rswag rescue JSON::Schema::ValidationError => ex raise UnexpectedResponse, "Expected response body to match schema: #{ex.message}" end - - block.call(body) if block_given? end end diff --git a/rswag-specs/spec/rswag/specs/response_validator_spec.rb b/rswag-specs/spec/rswag/specs/response_validator_spec.rb index bf1bba6..cb95996 100644 --- a/rswag-specs/spec/rswag/specs/response_validator_spec.rb +++ b/rswag-specs/spec/rswag/specs/response_validator_spec.rb @@ -45,8 +45,8 @@ module Rswag context "'block' provided" do let(:call) do - subject.validate!(response) do |body| - data = JSON.parse(body) + subject.validate!(response) do |response| + data = JSON.parse(response.body) expect(data['text']).to eq('Some comment') end end