diff --git a/lib/pacto/actors/from_examples.rb b/lib/pacto/actors/from_examples.rb index d32945d..68a7417 100644 --- a/lib/pacto/actors/from_examples.rb +++ b/lib/pacto/actors/from_examples.rb @@ -1,4 +1,6 @@ # -*- encoding : utf-8 -*- +require 'pacto/errors' + module Pacto module Actors class FirstExampleSelector @@ -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 @@ -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 diff --git a/spec/fabricators/contract_fabricator.rb b/spec/fabricators/contract_fabricator.rb index d833c36..19fcbd2 100644 --- a/spec/fabricators/contract_fabricator.rb +++ b/spec/fabricators/contract_fabricator.rb @@ -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' } diff --git a/spec/unit/actors/from_examples_spec.rb b/spec/unit/actors/from_examples_spec.rb index d7f0033..346601f 100644 --- a/spec/unit/actors/from_examples_spec.rb +++ b/spec/unit/actors/from_examples_spec.rb @@ -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 }