Class: UmtsCustomMatchers::RedirectBack

Inherits:
Object
  • Object
show all
Defined in:
lib/umts_custom_matchers/redirect_back.rb

Overview

Matcher for redirecting to “back”. Asserts that the HTTP response code is a redirect, and also that the ‘Location` matches the HTTP_REFERER of the request.

Constant Summary collapse

MATCHER_MODULE =
RSpec::Rails::Matchers
STATUS_CODE_MATCHER =
MATCHER_MODULE::HaveHttpStatus::GenericStatus
REDIRECT_PATH_MATCHER =
MATCHER_MODULE::RedirectTo::RedirectTo
ALLOWED_REQUEST_TYPES =
[ActionController::TestRequest].freeze
ALLOWED_RESPONSE_TYPES =
[ActionDispatch::TestResponse].freeze

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ RedirectBack

Returns a new instance of RedirectBack.



22
23
24
# File 'lib/umts_custom_matchers/redirect_back.rb', line 22

def initialize(scope)
  @scope = scope
end

Instance Method Details

#failure_messageObject

:nodoc:



56
57
58
# File 'lib/umts_custom_matchers/redirect_back.rb', line 56

def failure_message
  @message
end

#failure_message_when_negatedObject

:nodoc:



62
63
64
# File 'lib/umts_custom_matchers/redirect_back.rb', line 62

def failure_message_when_negated
  'expected response not to redirect back, but did'
end

#matches?(code) ⇒ Boolean

Returns ‘true` if response is a `302`, and the location matches the `HTTP_REFERER` of the request. `false` otherwise.

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/umts_custom_matchers/redirect_back.rb', line 32

def matches?(code)
  path = 'http://test.host/redirect'
  status_matcher = STATUS_CODE_MATCHER.new :redirect
  path_matcher = REDIRECT_PATH_MATCHER.new @scope, path

  verify_spec_type or return false
  verify_request_type or return false
  verify_response_type or return false
  verify_input_type(code) or return false

  @scope.request.env['HTTP_REFERER'] ||= path
  code.call
  @response = @scope.response

  verify_status_code(status_matcher) or return false
  verify_redirect_path(path_matcher) or return false

  true
end

#supports_block_expectations?Boolean

:nodoc:

Returns:

  • (Boolean)


68
69
70
# File 'lib/umts_custom_matchers/redirect_back.rb', line 68

def supports_block_expectations?
  true
end