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
56 57 58 59 60 61 62 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 56 def kind_for(status) { 301 => 'permanent', 302 => 'temporary', 307 => 'temporary' }[status] end |
#locations_match?(expected, actual) ⇒ Boolean
33 34 35 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 33 def locations_match?(expected, actual) actual =~ %r{#{expected}/?} end |
#permanent_redirect?(status) ⇒ Boolean
52 53 54 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 52 def permanent_redirect?(status) status == 301 end |
#redirect?(status, kind: nil) ⇒ Boolean
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 37 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 22 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 6 def (exception, status, actual_location, kind) return Util.(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 Util.(errors) end |
#redirect_result(url_or_domain_name) ⇒ Object
28 29 30 31 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 28 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
24 25 26 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 24 def redirects_correctly?(status, actual_loc, expected_loc, kind) redirect?(status, kind: kind) && locations_match?(expected_loc, actual_loc) end |
#temp_redirect?(status) ⇒ Boolean
48 49 50 |
# File 'lib/rspec/webservice_matchers/redirect_helpers.rb', line 48 def temp_redirect?(status) [302, 307].include?(status) end |