Class: Spec::Rails::Matchers::RedirectTo
- Inherits:
-
Object
- Object
- Spec::Rails::Matchers::RedirectTo
- Includes:
- ActionController::StatusCodes
- Defined in:
- lib/spec/rails/matchers/redirect_to.rb
Overview
:nodoc:
Instance Method Summary collapse
- #actual_hash ⇒ Object
- #actual_redirect_to_valid_route ⇒ Object
- #description ⇒ Object
- #expected_hash ⇒ Object
- #expected_url ⇒ Object
- #failure_message_for_should ⇒ Object
- #failure_message_for_should_not ⇒ Object
- #hash_from_url(url) ⇒ Object
-
#initialize(request, expected) ⇒ RedirectTo
constructor
A new instance of RedirectTo.
- #matches?(response_or_controller) ⇒ Boolean
- #path_hash(url) ⇒ Object
- #query_hash(url) ⇒ Object
- #with(options) ⇒ Object
Constructor Details
#initialize(request, expected) ⇒ RedirectTo
Returns a new instance of RedirectTo.
9 10 11 12 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 9 def initialize(request, expected) @expected = expected @request = request end |
Instance Method Details
#actual_hash ⇒ Object
39 40 41 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 39 def actual_hash hash_from_url @actual end |
#actual_redirect_to_valid_route ⇒ Object
47 48 49 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 47 def actual_redirect_to_valid_route actual_hash end |
#description ⇒ Object
99 100 101 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 99 def description "redirect to #{@expected.inspect}" end |
#expected_hash ⇒ Object
43 44 45 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 43 def expected_hash hash_from_url expected_url end |
#expected_url ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 70 def expected_url case @expected when Hash return ActionController::UrlRewriter.new(@request, {}).rewrite(@expected) when :back return @request.env['HTTP_REFERER'] when %r{^\w+://.*} return @expected else return "http://#{@request.host}" + (@expected.split('')[0] == '/' ? '' : '/') + @expected end end |
#failure_message_for_should ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 83 def if @redirected if @status_matched return %Q{expected redirect to #{@expected.inspect}, got redirect to #{@actual.inspect}} else return %Q{expected redirect to #{@expected.inspect} with status #{@expected_status}, got #{@actual_status}} end else return %Q{expected redirect to #{@expected.inspect}, got no redirect} end end |
#failure_message_for_should_not ⇒ Object
95 96 97 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 95 def return %Q{expected not to be redirected to #{@expected.inspect}, but was} if @redirected end |
#hash_from_url(url) ⇒ Object
51 52 53 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 51 def hash_from_url(url) query_hash(url).merge(path_hash(url)).with_indifferent_access end |
#matches?(response_or_controller) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 14 def matches?(response_or_controller) response = response_or_controller.respond_to?(:response) ? response_or_controller.response : response_or_controller @redirected = response.redirect? @actual = response.redirect_url return false unless @redirected if @expected_status @actual_status = interpret_status(response.code.to_i) @status_matched = @expected_status == @actual_status else @status_matched = true end if @expected.instance_of? Hash return false unless @actual =~ %r{^\w+://#{@request.host}} return false unless actual_redirect_to_valid_route return actual_hash == expected_hash && @status_matched else return @actual == expected_url && @status_matched end end |
#path_hash(url) ⇒ Object
55 56 57 58 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 55 def path_hash(url) path = url.sub(%r{^\w+://#{@request.host}(?::\d+)?}, "").split("?", 2)[0] ActionController::Routing::Routes.recognize_path path, { :method => :get } end |
#query_hash(url) ⇒ Object
60 61 62 63 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 60 def query_hash(url) query = url.split("?", 2)[1] || "" Rack::Utils.parse_query(query) end |
#with(options) ⇒ Object
65 66 67 68 |
# File 'lib/spec/rails/matchers/redirect_to.rb', line 65 def with() @expected_status = interpret_status([:status]) self end |