From 93839cf5584406b99e52544040b475fc262afb32 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 8 Feb 2018 18:02:59 +1100 Subject: [PATCH] feat: add request and response to message --- .../consumer_contract/consumer_contract.rb | 2 +- lib/pact/consumer_contract/interaction.rb | 8 ++++ lib/pact/consumer_contract/message.rb | 39 ++++++++++++++++++- lib/pact/consumer_contract/message/content.rb | 3 ++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/pact/consumer_contract/consumer_contract.rb b/lib/pact/consumer_contract/consumer_contract.rb index 517559a..3e37212 100644 --- a/lib/pact/consumer_contract/consumer_contract.rb +++ b/lib/pact/consumer_contract/consumer_contract.rb @@ -37,7 +37,7 @@ def self.from_hash(hash) elsif hash[:messages] hash[:messages].collect { |hash| Message.from_hash(hash)} else - [] + [] # or raise an error? end new( diff --git a/lib/pact/consumer_contract/interaction.rb b/lib/pact/consumer_contract/interaction.rb index 2c9979f..ada6eb6 100644 --- a/lib/pact/consumer_contract/interaction.rb +++ b/lib/pact/consumer_contract/interaction.rb @@ -36,6 +36,14 @@ def to_hash } end + def http? + true + end + + def message? + false + end + def validate! raise Pact::InvalidInteractionError.new(self) unless description && request && response end diff --git a/lib/pact/consumer_contract/message.rb b/lib/pact/consumer_contract/message.rb index 1ab6c72..c1f7d7d 100644 --- a/lib/pact/consumer_contract/message.rb +++ b/lib/pact/consumer_contract/message.rb @@ -3,6 +3,8 @@ require 'pact/shared/active_support_support' require 'pact/matching_rules' require 'pact/errors' +require 'pact/consumer/request' +require 'pact/consumer_contract/response' module Pact class Message @@ -13,8 +15,8 @@ class Message def initialize attributes = {} @description = attributes[:description] - @request = attributes[:content] @provider_state = attributes[:provider_state] || attributes[:providerState] + @content = attributes[:content] end def self.from_hash hash @@ -31,6 +33,41 @@ def to_hash } end + + def request + @request ||= Pact::Consumer::Request::Actual.from_hash( + path: '/', + method: 'POST', + query: nil, + headers: {'Content-Type' => 'application/json'}, + body: { + description: description, + providerStates: [{ + name: provider_state + }] + } + ) + end + + # custom media type? + def response + @response ||= Pact::Response.new( + status: 200, + headers: {'Content-Type' => 'application/json'}, + body: { + content: content + } + ) + end + + def http? + false + end + + def message? + true + end + def validate! raise Pact::InvalidMessageError.new(self) unless description && content end diff --git a/lib/pact/consumer_contract/message/content.rb b/lib/pact/consumer_contract/message/content.rb index d337e2d..636bea2 100644 --- a/lib/pact/consumer_contract/message/content.rb +++ b/lib/pact/consumer_contract/message/content.rb @@ -4,6 +4,9 @@ class Content < Hash include ActiveSupportSupport include SymbolizeKeys + def initialize hash + merge!(hash) + end end end end