Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Fix swagger example stubbing #175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/pacto/core/pacto_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def to_hash
{
status: status,
headers: headers,
body: body
body: format_body(body)
}
end

Expand All @@ -27,5 +27,15 @@ def to_s
string << " with body (#{raw_body.bytesize} bytes)" if raw_body
string
end

private

def format_body(body)
if body.is_a?(Hash) || body.is_a?(Array)
body.to_json
else
body
end
end
end
end
2 changes: 1 addition & 1 deletion lib/pacto/formats/swagger/contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def build_examples(response)
default: {
request: {}, # Swagger doesn't have a clear way to capture request examples
response: {
body: response_body
body: response_body.parse
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions lib/pacto/formats/swagger/response_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ def schema
return nil unless swagger_response.schema
swagger_response.schema.parse
end

def to_hash
[:status, :headers, :schema].each_with_object({}) do | key, hash |
hash[key.to_s] = send key
end
end
end
end
end
Expand Down
15 changes: 1 addition & 14 deletions lib/pacto/stubs/webmock_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ def stub_request!(contract)

stub.to_return do |request|
pacto_request = Pacto::Adapters::WebMock::PactoRequest.new request
response = contract.response_for pacto_request
{
status: response.status,
headers: response.headers,
body: format_body(response.body)
}
contract.response_for(pacto_request).to_hash
end
end

Expand All @@ -84,14 +79,6 @@ def process_hooks(webmock_request_signature, webmock_response)

private

def format_body(body)
if body.is_a?(Hash) || body.is_a?(Array)
body.to_json
else
body
end
end

def strict_details(request)
{}.tap do |details|
details[webmock_params_key(request)] = request.params unless request.params.empty?
Expand Down
18 changes: 16 additions & 2 deletions spec/unit/pacto/formats/swagger/contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Formats
module Swagger
describe Contract do
let(:swagger_yaml) do
''"
%(
swagger: '2.0'
info:
title: Sample API
Expand All @@ -23,7 +23,15 @@ module Swagger
200:
description: |-
Success.
"''
examples:
application/json: |-
{
"item": {
"id": 45,
"name": "basketball"
}
}
)
end
let(:swagger_definition) do
::Swagger.build(swagger_yaml, format: :yaml)
Expand All @@ -40,6 +48,12 @@ module Swagger
described_class.new(api_operation, file: file)
end

describe 'contract examples' do
let(:example) { contract.examples }
it { expect(example).to be_truthy }
it { expect(example[:default][:response][:body]).to be_a(Hash) }
end

it_behaves_like 'a contract'
end
end
Expand Down