Class: Rspec::Rails23::Matchers::Controllers::RedirectTo

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails23/matchers/controllers/redirect_to.rb

Overview

:nodoc:

Defined Under Namespace

Classes: QueryParameterParser

Instance Method Summary collapse

Constructor Details

#initialize(request, expected) ⇒ RedirectTo

Returns a new instance of RedirectTo.



8
9
10
11
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 8

def initialize(request, expected)
  @expected = expected
  @request = request
end

Instance Method Details

#actual_hashObject



26
27
28
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 26

def actual_hash
  hash_from_url @actual
end

#actual_redirect_to_valid_routeObject



34
35
36
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 34

def actual_redirect_to_valid_route
  actual_hash
end

#descriptionObject



77
78
79
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 77

def description
  "redirect to #{@actual.inspect}"
end

#expected_hashObject



30
31
32
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 30

def expected_hash
  hash_from_url expected_url
end

#expected_urlObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 52

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_messageObject



65
66
67
68
69
70
71
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 65

def failure_message
  if @redirected
    return %Q{expected redirect to #{@expected.inspect}, got redirect to #{@actual.inspect}}
  else
    return %Q{expected redirect to #{@expected.inspect}, got no redirect}
  end
end

#hash_from_url(url) ⇒ Object



38
39
40
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 38

def hash_from_url(url)
  query_hash(url).merge(path_hash(url)).with_indifferent_access
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 13

def matches?(response)
  @redirected = response.redirect?
  @actual = response.redirect_url
  return false unless @redirected
  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
  else
    return @actual == expected_url
  end
end

#negative_failure_messageObject



73
74
75
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 73

def negative_failure_message
  return %Q{expected not to be redirected to #{@expected.inspect}, but was} if @redirected
end

#path_hash(url) ⇒ Object



42
43
44
45
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 42

def path_hash(url)
  path = url.sub(%r{^\w+://#{@request.host}(?::\d+)?}, "").split("?", 2)[0]
  ActionController::Routing::Routes.recognize_path path
end

#query_hash(url) ⇒ Object



47
48
49
50
# File 'lib/rspec/rails23/matchers/controllers/redirect_to.rb', line 47

def query_hash(url)
  query = url.split("?", 2)[1] || ""
  QueryParameterParser.parse_query_parameters(query, @request)
end