From 4c0d698adcff9f67377e809222054dfcf9e3b3bf Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Mon, 18 Dec 2017 11:47:25 +1100 Subject: [PATCH] fix(pact-stub-service): ensure all interactions loaded when loading multiple pacts Closes: https://github.com/pact-foundation/pact-mock_service/issues/83 --- lib/pact/mock_service/app.rb | 8 ++--- .../stub_cli_with_multiple_pacts_spec.rb | 29 +++++++++++++++++++ spec/support/pact-for-stub-1.json | 26 +++++++++++++++++ spec/support/pact-for-stub-2.json | 26 +++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 spec/integration/stub_cli_with_multiple_pacts_spec.rb create mode 100644 spec/support/pact-for-stub-1.json create mode 100644 spec/support/pact-for-stub-2.json diff --git a/lib/pact/mock_service/app.rb b/lib/pact/mock_service/app.rb index 705763b..f590238 100644 --- a/lib/pact/mock_service/app.rb +++ b/lib/pact/mock_service/app.rb @@ -38,12 +38,12 @@ def shutdown end def setup_stub stub_pactfile_paths - stub_pactfile_paths.each do | pactfile_path | + interactions = stub_pactfile_paths.collect do | pactfile_path | $stdout.puts "INFO: Loading interactions from #{pactfile_path}" hash_interactions = JSON.parse(File.read(pactfile_path))['interactions'] - interactions = hash_interactions.collect { | hash | Interaction.from_hash(hash) } - @session.set_expected_interactions interactions - end + hash_interactions.collect { | hash | Interaction.from_hash(hash) } + end.flatten + @session.set_expected_interactions interactions end def write_pact_if_configured diff --git a/spec/integration/stub_cli_with_multiple_pacts_spec.rb b/spec/integration/stub_cli_with_multiple_pacts_spec.rb new file mode 100644 index 0000000..6588631 --- /dev/null +++ b/spec/integration/stub_cli_with_multiple_pacts_spec.rb @@ -0,0 +1,29 @@ +require 'support/integration_spec_support' + +describe "The pact-stub-service command line interface with multiple pacts", mri_only: true do + + include Pact::IntegrationTestSupport + + PORT = 5556 + + before :all do + clear_dirs + @pid = start_stub_server PORT, "spec/support/pact-for-stub-1.json spec/support/pact-for-stub-2.json" + end + + it "includes the interactions from the first pact file" do + response = Faraday.get "http://localhost:#{PORT}/path1" + puts response.body if response.status != 200 + expect(response.status).to eq 200 + end + + it "includes the interactions from the second pact file" do + response = Faraday.get "http://localhost:#{PORT}/path2" + puts response.body if response.status != 200 + expect(response.status).to eq 200 + end + + after :all do + kill_server @pid + end +end diff --git a/spec/support/pact-for-stub-1.json b/spec/support/pact-for-stub-1.json new file mode 100644 index 0000000..578e2a0 --- /dev/null +++ b/spec/support/pact-for-stub-1.json @@ -0,0 +1,26 @@ +{ + "provider": { + "name": "a provider" + }, + "consumer": { + "name": "a consumer" + }, + "interactions": [ + { + "description": "request one", + "request": { + "method": "get", + "path": "/path1" + }, + "response": { + "status": 200 + }, + "providerState": "state one" + } + ], + "metadata": { + "pactSpecification": { + "version": "2.0" + } + } +} diff --git a/spec/support/pact-for-stub-2.json b/spec/support/pact-for-stub-2.json new file mode 100644 index 0000000..c74a4af --- /dev/null +++ b/spec/support/pact-for-stub-2.json @@ -0,0 +1,26 @@ +{ + "provider": { + "name": "a provider" + }, + "consumer": { + "name": "a consumer" + }, + "interactions": [ + { + "description": "request two", + "request": { + "method": "get", + "path": "/path2" + }, + "response": { + "status": 200 + }, + "providerState": "state two" + } + ], + "metadata": { + "pactSpecification": { + "version": "2.0" + } + } +}