Skip to content

Commit

Permalink
feat(message pact): add DSL for configuring Message Pact verifications
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Jan 16, 2020
1 parent 89ed9ef commit a5181b6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/pact/consumer/configuration/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'pact/consumer/configuration/service_consumer'

module Pact

module Consumer
module DSL
def service_consumer name, &block
Expand Down
7 changes: 6 additions & 1 deletion lib/pact/provider/configuration/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'pact/provider/configuration/service_provider_dsl'
require 'pact/provider/configuration/message_provider_dsl'

module Pact

Expand All @@ -8,6 +9,10 @@ module DSL
def service_provider name, &block
Configuration::ServiceProviderDSL.build(name, &block)
end

def message_provider name, &block
Configuration::MessageProviderDSL.build(name, &block)
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/pact/provider/configuration/message_provider_dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'pact/provider/configuration/service_provider_dsl'

module Pact
module Provider
module Configuration
class MessageProviderDSL < ServiceProviderDSL
class RackToMessageAdapter
def initialize(message_builder)
@message_builder = message_builder
end

def call(env)
request_body_json = JSON.parse(env['rack.input'].read)
contents = @message_builder.call(request_body_json['description'])
[200, {"Content-Type" => "application/json"}, [{ contents: contents }.to_json]]
end
end

def initialize name
super
@mapper_block = lambda { |args| }
end

dsl do
def builder &block
self.app_block = lambda { RackToMessageAdapter.new(block) }
end
end
end
end
end
end
1 change: 1 addition & 0 deletions pact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'appraisal', '~> 2.2'
gem.add_development_dependency 'conventional-changelog', '~> 1.3'
gem.add_development_dependency 'bump', '~> 0.5'
gem.add_development_dependency 'pact-message'
end
41 changes: 41 additions & 0 deletions spec/support/message_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'pact/message'

# Example data store

class DataStore
def self.greeting_recipient= greeting_recipient
@greeting_recipient = greeting_recipient
end

def self.greeting_recipient
@greeting_recipient
end
end

# Example message producer

class BarProvider
def create_message
{
text: "Hello #{DataStore.greeting_recipient}"
}
end
end

# Provider states

Pact.provider_states_for "Foo" do
provider_state "a world exists" do
set_up do
DataStore.greeting_recipient = "world"
end
end
end

CONFIG = {
"a message" => lambda { BarProvider.new.create_message }
}

Pact.message_provider "Bar" do
builder { |description| CONFIG[description].call }
end
5 changes: 5 additions & 0 deletions tasks/message-test.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'pact/tasks'

Pact::VerificationTask.new(:message) do | pact |
pact.uri 'spec/pacts/foo-bar-message.json', pact_helper: 'spec/support/message_spec_helper.rb'
end
1 change: 1 addition & 0 deletions tasks/pact-test.rake
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace :pact do
Rake::Task['pact:verify:term_v2'].execute
Rake::Task['pact:verify:test_app_with_provider_state_params'].execute
Rake::Task['pact:verify:test_app:wip'].execute
Rake::Task['pact:verify:message'].execute
end

desc "All the verification tests with active support loaded"
Expand Down

0 comments on commit a5181b6

Please sign in to comment.