From 2e488923afedda02960a01dc8c38cef347e48ab1 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 13 Feb 2018 09:33:57 +1100 Subject: [PATCH] feat: update message classes to support pact-message --- lib/pact/consumer_contract/interaction.rb | 2 +- lib/pact/consumer_contract/message.rb | 178 ++++++++++-------- lib/pact/consumer_contract/message/content.rb | 26 ++- .../consumer_contract_spec.rb | 2 +- 4 files changed, 120 insertions(+), 88 deletions(-) diff --git a/lib/pact/consumer_contract/interaction.rb b/lib/pact/consumer_contract/interaction.rb index ada6eb6..297ab83 100644 --- a/lib/pact/consumer_contract/interaction.rb +++ b/lib/pact/consumer_contract/interaction.rb @@ -6,7 +6,7 @@ require 'pact/errors' module Pact - class Interaction + class Interaction include ActiveSupportSupport include SymbolizeKeys diff --git a/lib/pact/consumer_contract/message.rb b/lib/pact/consumer_contract/message.rb index c1f7d7d..ca4fb28 100644 --- a/lib/pact/consumer_contract/message.rb +++ b/lib/pact/consumer_contract/message.rb @@ -5,100 +5,118 @@ require 'pact/errors' require 'pact/consumer/request' require 'pact/consumer_contract/response' +require 'pact/consumer_contract/message/content' module Pact - class Message - include ActiveSupportSupport - include SymbolizeKeys - - attr_accessor :description, :content, :provider_state - - def initialize attributes = {} - @description = attributes[:description] - @provider_state = attributes[:provider_state] || attributes[:providerState] - @content = attributes[:content] - end - - def self.from_hash hash - content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules']) - content = Pact::Message::Content.new(content_hash) - new(symbolize_keys(hash).merge(content: content)) - end - - def to_hash - { - description: description, - provider_state: provider_state, - content: content.to_hash, - } - end - - - def request - @request ||= Pact::Consumer::Request::Actual.from_hash( - path: '/', - method: 'POST', - query: nil, - headers: {'Content-Type' => 'application/json'}, - body: { + class ConsumerContract + class Message + include Pact::ActiveSupportSupport + include Pact::SymbolizeKeys + + attr_accessor :description, :content, :provider_state + + def initialize attributes = {} + @description = attributes[:description] + @provider_state = attributes[:provider_state] || attributes[:providerState] + @content = attributes[:content] + end + + def self.from_hash hash + content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules']) + content = Pact::ConsumerContract::Message::Content.new(content_hash) + new(symbolize_keys(hash).merge(content: content)) + end + + def to_hash + { description: description, - providerStates: [{ - name: provider_state - }] + provider_state: provider_state, + content: content.to_hash, } - ) - end - - # custom media type? - def response - @response ||= Pact::Response.new( - status: 200, - headers: {'Content-Type' => 'application/json'}, - body: { - content: content + end + + # todo move this proper decorator + def as_json + { + description: description, + providerState: provider_state, + content: content.as_json } - ) - end + 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 - def http? - false - end + # custom media type? + def response + @response ||= Pact::Response.new( + status: 200, + headers: {'Content-Type' => 'application/json'}, + body: { + content: content + } + ) + end - def message? - true - end + def http? + false + end - def validate! - raise Pact::InvalidMessageError.new(self) unless description && content - end + def message? + true + end - def == other - other.is_a?(Message) && to_hash == other.to_hash - end + def validate! + raise Pact::InvalidMessageError.new(self) unless description && content + end + + def == other + other.is_a?(Message) && to_hash == other.to_hash + end - def matches_criteria? criteria - criteria.each do | key, value | - unless match_criterion self.send(key.to_s), value - return false + def matches_criteria? criteria + criteria.each do | key, value | + unless match_criterion self.send(key.to_s), value + return false + end end + true end - true - end - def match_criterion target, criterion - target == criterion || (criterion.is_a?(Regexp) && criterion.match(target)) - end + def match_criterion target, criterion + target == criterion || (criterion.is_a?(Regexp) && criterion.match(target)) + end + + def eq? other + self == other + end - def eq? other - self == other - end + def description_with_provider_state_quoted + provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\"" + end - def description_with_provider_state_quoted - provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\"" - end + def to_s + to_hash.to_s + end + end + end +end - def to_s - to_hash.to_s - end - end +module Pact::Message + def self.new *args + Pact::ConsumerContract::Message.new(*args) + end end diff --git a/lib/pact/consumer_contract/message/content.rb b/lib/pact/consumer_contract/message/content.rb index 636bea2..ad7bb68 100644 --- a/lib/pact/consumer_contract/message/content.rb +++ b/lib/pact/consumer_contract/message/content.rb @@ -1,11 +1,25 @@ module Pact - class Message - class Content < Hash - include ActiveSupportSupport - include SymbolizeKeys + class ConsumerContract + class Message + class Content + include ActiveSupportSupport + include SymbolizeKeys - def initialize hash - merge!(hash) + def initialize content + @content = content + end + + def to_s + if @content.is_a?(Hash) || @content.is_a?(Array) + @content.to_json + else + @content.to_s + end + end + + def as_json + @content + end end end end diff --git a/spec/lib/pact/consumer_contract/consumer_contract_spec.rb b/spec/lib/pact/consumer_contract/consumer_contract_spec.rb index 481a15e..78a6070 100644 --- a/spec/lib/pact/consumer_contract/consumer_contract_spec.rb +++ b/spec/lib/pact/consumer_contract/consumer_contract_spec.rb @@ -62,7 +62,7 @@ module Pact it "should have messages" do expect(loaded_pact.interactions).to be_instance_of Array - expect(loaded_pact.interactions.first).to be_instance_of Pact::Message + expect(loaded_pact.interactions.first).to be_instance_of Pact::ConsumerContract::Message end it "should have a consumer" do