Class: CoreLibrary::ComparisonHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/utilities/comparison_helper.rb

Overview

A utility that perform the comparison of the Response body and headers.

Class Method Summary collapse

Class Method Details

.match_body(expected_body, actual_body, check_values: false, check_order: false, check_count: false) ⇒ Object

Compares the received body with the expected body.

Parameters:

  • expected_body (Dynamic)

    The expected body.

  • actual_body (Dynamic)

    The actual body.

  • check_values (Boolean, optional) (defaults to: false)

    A flag which determines if we check values in dictionaries.

  • check_order (Boolean, optional) (defaults to: false)

    A flag which determines if we check the order of array elements.

  • check_count (Boolean, optional) (defaults to: false)

    A flag which determines if we check the count of array elements.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/apimatic-core/utilities/comparison_helper.rb', line 33

def self.match_body(expected_body,
                    actual_body,
                    check_values: false,
                    check_order: false,
                    check_count: false)
  if expected_body.instance_of? Hash
    return false unless actual_body.instance_of? Hash
    for key in expected_body.keys
      return false unless actual_body.keys.include? key
      if check_values or expected_body[key].instance_of? Hash
        return false unless match_body(expected_body[key],
                                                  actual_body[key],
                                                  check_values: check_values,
                                                  check_order: check_order,
                                                  check_count: check_count)
      end
    end
  elsif expected_body.instance_of? Array
    return false unless actual_body.instance_of? Array
    if check_count == true && (expected_body.length != actual_body.length)
      return false
    else
      previous_matches = Array.new
      expected_body.each.with_index do |expected_element, i|
        matches = (actual_body.map.with_index do |received_element, j|
          j if match_body(expected_element,
                                     received_element,
                                     check_values: check_values,
                                     check_order: check_order,
                                     check_count: check_count)
        end).compact
        return false if matches.length == 0
        if check_order == true
          return false if (i != 0 && matches.map{|x| previous_matches.map{|y| y > x}.all?}.all?)
          previous_matches = matches
        end
      end
    end
  elsif expected_body != actual_body
    return false
  end
  return true
end

.match_headers(expected_headers, actual_headers, allow_extra: true) ⇒ Object

Compares the received headers with the expected headers.

Parameters:

  • expected_headers (Hash)

    A hash of expected headers (keys in lower case).

  • actual_headers (Hash)

    A hash of actual headers.

  • allow_extra (Boolean, optional) (defaults to: true)

    A flag which determines if we allow extra headers.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/apimatic-core/utilities/comparison_helper.rb', line 9

def self.match_headers(expected_headers,
                       actual_headers,
                       allow_extra: true)
  return false if ((actual_headers.length < expected_headers.length) ||
    ((allow_extra == false) && (actual_headers.length > expected_headers.length)))

  actual_headers = Hash[actual_headers.map{|k, v| [k.to_s.downcase, v]}]
  expected_headers = Hash[expected_headers.map{|k, v| [k.to_s.downcase, v]}]

  expected_headers.each do |e_key, e_value|
    return false unless actual_headers.key?(e_key)
    return false if ((e_value != nil) &&
      (e_value != actual_headers[e_key]))
  end

  return true
end