Skip to content

Commit

Permalink
feat: add message pact verification support - alpha release only
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Mar 24, 2018
1 parent 677620f commit 69cd7e9
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ gemspec

if ENV['X_PACT_DEVELOPMENT']
gem "pact", path: '../pact'
gem "pact-message", path: '../pact-message'
gem "pact-support", path: '../pact-support'
end
1 change: 1 addition & 0 deletions lib/pact/provider_verifier/app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'pact/provider_verifier/add_header_middlware'
require 'pact/provider/rspec'
require 'pact/message'
require 'pact/cli/run_pact_verification'
require 'rack/reverse_proxy'
require 'faraday_middleware'
Expand Down
31 changes: 31 additions & 0 deletions spec/integration_verify_message_pact_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'pact/provider_verifier/app'

RSpec.describe "verifying a message pact" do

before(:all) do
@pipe = IO.popen("rackup -p 9393 spec/support/message_producer_verifier.ru")
sleep 2
end

let(:pact_path) { './spec/support/pacts/message-pact-v3-pass.json' }

subject { `bundle exec bin/pact-provider-verifier #{pact_path} -a 1.0.100 --provider-base-url http://localhost:9393 2>&1` }

context "when verification passes" do
it do
expect(subject).to include("1 interaction, 0 failures")
end
end

context "when verification fails" do
let(:pact_path) { './spec/support/pacts/message-pact-v3-fail.json' }

it do
expect(subject).to include("1 interaction, 1 failure")
end
end

after(:all) do
Process.kill 'KILL', @pipe.pid
end
end
39 changes: 39 additions & 0 deletions spec/support/message_producer_verifier.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# TODO provider states

require 'json'

class Provider
def a_test_message
{
text: "Hello world!!"
}
end
end

class MessageCreator
def initialize provider
@provider = provider
end

def create message_descriptor
message_creation_method = message_descriptor.description.downcase.gsub(' ', '_').to_sym
message_content = @provider.send(message_creation_method)
{ content: message_content }
end
end

class HttpRequestHandler
def initialize message_creator
@message_creator = message_creator
end

def call env
request_body = JSON.parse(env['rack.input'].read)
message_descriptor = OpenStruct.new(request_body)
response_body = @message_creator.create(message_descriptor)
[200, {'Content-Type' => 'application/json'}, [response_body.to_json]]
end

end

run HttpRequestHandler.new(MessageCreator.new(Provider.new))
11 changes: 11 additions & 0 deletions spec/support/pacts/message-pact-v3-fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"consumer": {"name": "Foo"},
"provider": {"name": "Bar"},
"messages": [{
"description": "a test message",
"content": {
"text": "Hello world!!!"
}
}
]
}
11 changes: 11 additions & 0 deletions spec/support/pacts/message-pact-v3-pass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"consumer": {"name": "Foo"},
"provider": {"name": "Bar"},
"messages": [{
"description": "a test message",
"content": {
"text": "Hello world!!"
}
}
]
}

0 comments on commit 69cd7e9

Please sign in to comment.