diff --git a/lib/pact/consumer/configuration/dsl.rb b/lib/pact/consumer/configuration/dsl.rb index 6a212da0..3017c9f3 100644 --- a/lib/pact/consumer/configuration/dsl.rb +++ b/lib/pact/consumer/configuration/dsl.rb @@ -1,7 +1,6 @@ require 'pact/consumer/configuration/service_consumer' module Pact - module Consumer module DSL def service_consumer name, &block diff --git a/lib/pact/provider/configuration/dsl.rb b/lib/pact/provider/configuration/dsl.rb index 82e1ab8f..51b7f8f2 100644 --- a/lib/pact/provider/configuration/dsl.rb +++ b/lib/pact/provider/configuration/dsl.rb @@ -1,4 +1,5 @@ require 'pact/provider/configuration/service_provider_dsl' +require 'pact/provider/configuration/message_provider_dsl' module Pact @@ -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 \ No newline at end of file +end diff --git a/lib/pact/provider/configuration/message_provider_dsl.rb b/lib/pact/provider/configuration/message_provider_dsl.rb new file mode 100644 index 00000000..a87196b6 --- /dev/null +++ b/lib/pact/provider/configuration/message_provider_dsl.rb @@ -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 diff --git a/pact.gemspec b/pact.gemspec index 72f3df16..09e0b6ec 100644 --- a/pact.gemspec +++ b/pact.gemspec @@ -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 diff --git a/spec/support/message_spec_helper.rb b/spec/support/message_spec_helper.rb new file mode 100644 index 00000000..13a0f046 --- /dev/null +++ b/spec/support/message_spec_helper.rb @@ -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 diff --git a/tasks/message-test.rake b/tasks/message-test.rake new file mode 100644 index 00000000..8fef6243 --- /dev/null +++ b/tasks/message-test.rake @@ -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 diff --git a/tasks/pact-test.rake b/tasks/pact-test.rake index 86fcaeb4..28247981 100644 --- a/tasks/pact-test.rake +++ b/tasks/pact-test.rake @@ -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"