Module: RSpec::WebserviceMatchers::RedirectHelpers

Defined in:
lib/rspec/webservice_matchers/redirect_helpers.rb

Instance Method Summary collapse

Instance Method Details

#kind_for(status) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 58

def kind_for(status)
  {
    301 => 'permanent',
    302 => 'temporary',
    307 => 'temporary'
  }[status]
end

#locations_match?(expected, actual) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 35

def locations_match?(expected, actual)
  actual =~ %r{#{expected}/?}
end

#permanent_redirect?(status) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 54

def permanent_redirect?(status)
  status == 301
end

#redirect?(status, kind: nil) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 39

def redirect?(status, kind: nil)
  case kind
  when :permanent
    permanent_redirect?(status)
  when :temporary
    temp_redirect?(status)
  else
    temp_redirect?(status) || permanent_redirect?(status)
  end
end

#redirect_failure_message(exception, status, actual_location, kind) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 8

def redirect_failure_message(exception, status, actual_location, kind)
  return WebTest::Util.error_message(exception) if exception

  errors = []
  if redirect? status
    unless redirect? status, kind: kind
      errors << "received a #{kind_for(status)} redirect"
    end
    unless locations_match? expected, actual_location
      errors << "received location #{actual_location}"
    end
  else
    errors << "not a redirect: received status #{status}"
  end

  WebTest::Util.error_message(errors)
end

#redirect_result(url_or_domain_name) ⇒ Object



30
31
32
33
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 30

def redirect_result(url_or_domain_name)
  status, headers = WebTest::Util.head(url_or_domain_name)
  [status, headers['location']]
end

#redirects_correctly?(status, actual_loc, expected_loc, kind) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 26

def redirects_correctly?(status, actual_loc, expected_loc, kind)
  redirect?(status, kind: kind) && locations_match?(expected_loc, actual_loc)
end

#temp_redirect?(status) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 50

def temp_redirect?(status)
  [302, 307].include?(status)
end