Class: Spec::Rails::Matchers::RedirectTo

Inherits:
Object
  • Object
show all
Includes:
ActionController::StatusCodes
Defined in:
lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb,
lib/vendor/plugins/rspec-rails/lib/spec/rails/matchers/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.



7
8
9
10
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 7

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

Instance Method Details

#actual_hashObject



25
26
27
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 25

def actual_hash
  hash_from_url @actual
end

#actual_redirect_to_valid_routeObject



33
34
35
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 33

def actual_redirect_to_valid_route
  actual_hash
end

#descriptionObject



76
77
78
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 76

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

#expected_hashObject



29
30
31
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 29

def expected_hash
  hash_from_url expected_url
end

#expected_urlObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 51

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



64
65
66
67
68
69
70
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 64

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

#failure_message_for_shouldObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/matchers/redirect_to.rb', line 83

def failure_message_for_should
  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_notObject



95
96
97
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/matchers/redirect_to.rb', line 95

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

#hash_from_url(url) ⇒ Object



37
38
39
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 37

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

#matches?(response_or_controller) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 12

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



72
73
74
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 72

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

#path_hash(url) ⇒ Object



41
42
43
44
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 41

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



46
47
48
49
# File 'lib/vendor/plugins/make_resourceful/spec/rspec-rails/redirect_to.rb', line 46

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

#with(options) ⇒ Object



65
66
67
68
# File 'lib/vendor/plugins/rspec-rails/lib/spec/rails/matchers/redirect_to.rb', line 65

def with(options)
  @expected_status = interpret_status(options[:status])
  self
end