Class: Pact::Request::Expected

Inherits:
Base
  • Object
show all
Defined in:
lib/pact/consumer_contract/request.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{:allow_unexpected_keys => false}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#body, #headers, #method, #path, #query

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#content_type, #content_type?, #full_path, #method_and_path, #modifies_resource?, #specified?, #to_hash

Methods included from SymbolizeKeys

included, #symbolize_keys

Constructor Details

#initialize(method, path, headers, body, query, options = {}, generators = {}) ⇒ Expected

Returns a new instance of Expected.



23
24
25
26
27
# File 'lib/pact/consumer_contract/request.rb', line 23

def initialize(method, path, headers, body, query, options = {}, generators = {})
  super(method, path, headers, body, query)
  @generators = generators
  @options = options
end

Instance Attribute Details

#generatorsObject

Returns the value of attribute generators.



9
10
11
# File 'lib/pact/consumer_contract/request.rb', line 9

def generators
  @generators
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/pact/consumer_contract/request.rb', line 9

def options
  @options
end

Class Method Details

.from_hash(hash) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pact/consumer_contract/request.rb', line 11

def self.from_hash(hash)
  sym_hash = symbolize_keys hash
  method = sym_hash.fetch(:method)
  path = sym_hash.fetch(:path)
  query = sym_hash.fetch(:query, key_not_found)
  headers = sym_hash.fetch(:headers, key_not_found)
  body = sym_hash.fetch(:body, key_not_found)
  options = sym_hash.fetch(:options, {})
  generators = sym_hash.fetch(:generators, {})
  new(method, path, headers, body, query, options, generators)
end

Instance Method Details

#difference(actual_request) ⇒ Object



40
41
42
43
44
45
# File 'lib/pact/consumer_contract/request.rb', line 40

def difference(actual_request)
  require 'pact/matchers' # avoid recusive loop between pact/reification, pact/matchers and this file
  request_diff = Pact::Matchers.diff(to_hash_without_body_or_query, actual_request.to_hash_without_body_or_query)
  request_diff.merge!(query_diff(actual_request.query))
  request_diff.merge!(body_diff(actual_request.body))
end

#matches?(actual_request) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/pact/consumer_contract/request.rb', line 29

def matches?(actual_request)
  difference(actual_request).empty?
end

#matches_route?(actual_request) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/pact/consumer_contract/request.rb', line 33

def matches_route? actual_request
  require 'pact/matchers' # avoid recusive loop between pact/reification, pact/matchers and this file
  route = {:method => method.upcase, :path => path}
  other_route = {:method => actual_request.method.upcase, :path => actual_request.path}
  Pact::Matchers.diff(route, other_route).empty?
end