Module: RSpec::WebserviceMatchers::RedirectHelpers
- Defined in:
- lib/rspec/webservice_matchers/redirect_helpers.rb
Instance Method Summary collapse
- #kind_for(status) ⇒ Object
- #locations_match?(expected, actual) ⇒ Boolean
- #permanent_redirect?(status) ⇒ Boolean
- #redirect?(status, kind: nil) ⇒ Boolean
- #redirect_failure_message(exception, status, actual_location, kind) ⇒ Object
- #redirect_result(url_or_domain_name) ⇒ Object
- #redirects_correctly?(status, actual_loc, expected_loc, kind) ⇒ Boolean
- #temp_redirect?(status) ⇒ Boolean
Instance Method Details
#kind_for(status) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 55 def kind_for(status) { 301 => 'permanent', 302 => 'temporary', 307 => 'temporary' }[status] end |
#locations_match?(expected, actual) ⇒ Boolean
32 33 34 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 32 def locations_match?(expected, actual) actual =~ %r{#{expected}/?} end |
#permanent_redirect?(status) ⇒ Boolean
51 52 53 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 51 def permanent_redirect?(status) status == 301 end |
#redirect?(status, kind: nil) ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 36 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
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 6 def (exception, status, actual_location, kind) return Util.(exception) if exception errors = [] 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 unless redirect? status errors << "not a redirect: received status #{status}" end Util.(errors) end |
#redirect_result(url_or_domain_name) ⇒ Object
27 28 29 30 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 27 def redirect_result(url_or_domain_name) status, headers = Util.head(url_or_domain_name) [status, headers['location']] end |
#redirects_correctly?(status, actual_loc, expected_loc, kind) ⇒ Boolean
23 24 25 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 23 def redirects_correctly?(status, actual_loc, expected_loc, kind) redirect?(status, kind: kind) && locations_match?(expected_loc, actual_loc) end |
#temp_redirect?(status) ⇒ Boolean
47 48 49 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 47 def temp_redirect?(status) [302, 307].include?(status) end |