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

Improve error message when an example URI is incompatible with request template #184

Open
wants to merge 1 commit 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
16 changes: 11 additions & 5 deletions lib/pacto/actors/from_examples.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- encoding : utf-8 -*-
require 'pacto/errors'

module Pacto
module Actors
class FirstExampleSelector
Expand Down Expand Up @@ -32,7 +34,7 @@ def build_request(contract, values = {})
if contract.examples?
example = @selector.select(contract.examples, values)
data = contract.request.to_hash
request_values.merge! example_uri_values(contract)
request_values.merge! example_uri_values(contract, example)
data['uri'] = contract.request.uri(request_values)
data['body'] = example.request.body
data['method'] = contract.request.http_method
Expand All @@ -53,11 +55,15 @@ def build_response(contract, values = {})
end
end

def example_uri_values(contract)
def example_uri_values(contract,example)
uri_template = contract.request.pattern.uri_template
if contract.examples && contract.examples.values.first[:request][:uri]
example_uri = contract.examples.values.first[:request][:uri]
uri_template.extract example_uri
if example && example[:request][:uri]
example_uri = example[:request][:uri]
extracted_values = uri_template.extract(example_uri)
raise InvalidContract.new([
"Example URI #{example_uri} is not compatible with the request URI template #{uri_template.pattern}"]) unless extracted_values
return extracted_values

else
{}
end
Expand Down
2 changes: 1 addition & 1 deletion spec/fabricators/contract_fabricator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
end

Fabricator(:request_clause, from: REQUEST_CLAUSE_CLASS) do
initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
initialize_with { @_klass.new(to_hash.merge(skip_freeze: true)) } # Hash based initialization
host { 'example.com' }
http_method { 'GET' }
path { '/abcd' }
Expand Down
24 changes: 23 additions & 1 deletion spec/unit/actors/from_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,29 @@ module Actors
generator.build_response contract
end
end

context 'a contract with uri template' do
subject(:generator) { described_class.new fallback, Pacto::Actors::NamedExampleSelector }
let(:contract) do
Fabricate(:contract,
request:
Fabricate(:request_clause,
host: "somewhere.com",
path: "/this_is_something/{b}"),
examples: { "bad" =>
Fabricate(:an_example,
request: {uri: "http://somwer/this_is_not_something/BAD"}),
"good" =>
Fabricate(:an_example,
request: {uri: "http://somewhere.com/this_is_something/GOOD"})
})
end
it 'should raise an error if the example uri is incompatible' do
expect{generator.build_request(contract, example_name: "bad")}.to raise_error(Pacto::InvalidContract)
end
it 'should correctly perform substitutions for compatible example uris' do
expect(generator.build_request(contract, example_name: "good").uri.to_s).to eq("http://somewhere.com/this_is_something/GOOD")
end
end
context 'a contract with examples' do
let(:contract) { Fabricate(:contract, example_count: 3) }
let(:request) { generator.build_request contract }
Expand Down