Sha256: 67981815c792b88368de5552e6a28e9c010ccb56c4545ea3432bcf26dac1ac9b

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8
RSpec::Matchers.define :have_requests_with_status_code do |expected|
  match do |actual|
    domains_eq? actual, expected
  end

  failure_message do |actual|
    expected_domains = format_domains(expected)
    actual_domains   = format_domains(extract_domains(actual))

    "expected that \"#{actual.current_url}\" references with the given http status codes:\n#{expected_domains}\n\nBut it references with the given http status codes:\n#{actual_domains}\n"
  end

  failure_message_when_negated do |actual|
    expected_domains = format_domains(expected)
    actual_domains   = format_domains(extract_domains(actual))

    "expected that \"#{actual.current_url}\" not references domains with the given http status codes:\n#{expected_domains}\n\nBut it references with the given http status codes:\n#{actual_domains}\n"
  end

  description do
    "to reference domains with the given http status codes:\n#{format_domains(expected)}"
  end

  def domains_eq?(actual, expected)
    actual_domains = extract_domains(actual)

    expected.all? { |key, value| Array(actual_domains[key]) == Array(value) }
  end

  def extract_domains(page)
    page.driver.network_traffic.reduce({}) { |memo, o| memo[Addressable::URI.parse(o.url).host] = get_status_codes(o.response_parts); memo}
  end

  def format_domains(domains)
    domains.collect { |key, value| format "* %s (%s)", key, Array(value).collect { |s| "\"#{s}\""}.join(', ')}.join("\n")
  end

  def get_status_codes(response_parts)
    response_parts.collect { |rp| rp.status }.uniq
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
proxy_tester-0.1.10 lib/proxy_tester/rspec/matchers/have_requests_with_status_code.rb
proxy_tester-0.1.8 lib/proxy_tester/rspec/matchers/have_requests_with_status_code.rb