Sha256: e90e4f9867c3f3eb64e0505b66c72cb5cb77c70ebaee3d829b4d7898025dab3b

Contents?: true

Size: 1.93 KB

Versions: 24

Compression:

Stored size: 1.93 KB

Contents

require 'hashie'
require 'hashie/extensions/key_conversion'

module Pact
  module HashUtils

    class Converter < Hash
      include Hashie::Extensions::KeyConversion
      include Hashie::Extensions::DeepMerge
    end

    def symbolize_keys hash
      Hash[Converter[hash].symbolize_keys]
    end

    def stringify_keys hash
      Hash[Converter[hash].stringify_keys]
    end

    def deep_merge hash1, hash2
      Converter[hash1].deep_merge(Converter[hash2])
    end
  end
end

class InteractionFactory

  extend Pact::HashUtils

  DEFAULTS = Hash[
    'request' => {
      'path' => '/path',
      'method' => 'get',
    },
    'response' => {
      'body' => {a: 'response body'}
    },
    'description' => 'a description',
    'provider_state' => 'a thing exists'
  ]

  def self.create hash = {}
    Pact::Interaction.from_hash(stringify_keys(deep_merge(DEFAULTS, stringify_keys(hash))))
  end
end


class ConsumerContractFactory
  extend Pact::HashUtils
  DEFAULTS = {:consumer_name => 'consumer',
      :provider_name => 'provider',
      :interactions => [InteractionFactory.create]}

  def self.create overrides = {}
    options = deep_merge(symbolize_keys(DEFAULTS), symbolize_keys(overrides))
    Pact::ConsumerContract.new({:consumer => Pact::ServiceConsumer.new(name: options[:consumer_name]),
      :provider => Pact::ServiceProvider.new(name: options[:provider_name]),
      :interactions => options[:interactions]})
  end
end



class ResponseFactory
  extend Pact::HashUtils
  DEFAULTS = {:status => 200, :body => {a: 'body'}}.freeze
  def self.create_hash overrides = {}
    deep_merge(DEFAULTS, overrides)
  end
end

class RequestFactory
  extend Pact::HashUtils
  DEFAULTS = {:path => '/path', :method => 'get', :query => 'query', :headers => {}}.freeze
  def self.create_hash overrides = {}
   deep_merge(DEFAULTS, overrides)
  end

  def self.create_actual overrides = {}
    Pact::Consumer::Request::Actual.from_hash(create_hash(overrides))
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
pact-1.1.1 spec/support/factories.rb
pact-1.1.0 spec/support/factories.rb
pact-1.1.0.rc5 spec/support/factories.rb
pact-1.1.0.rc4 spec/support/factories.rb
pact-1.1.0.rc3 spec/support/factories.rb
pact-1.0.39 spec/support/factories.rb
pact-1.1.0.rc2 spec/support/factories.rb
pact-1.0.38 spec/support/factories.rb
pact-1.1.0.rc1 spec/support/factories.rb
pact-1.0.37 spec/support/factories.rb
pact-1.0.36 spec/support/factories.rb
pact-1.0.35 spec/support/factories.rb
pact-1.0.34 spec/support/factories.rb
pact-1.0.33 spec/support/factories.rb
pact-1.0.32 spec/support/factories.rb
pact-1.0.31 spec/support/factories.rb
pact-1.0.30 spec/support/factories.rb
pact-1.0.29 spec/support/factories.rb
pact-1.0.28 spec/support/factories.rb
pact-1.0.27 spec/support/factories.rb