Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_controller/assertions/response_assertions.rb

Revision 9059, 7.9 kB (checked in by bitsweat, 9 months ago)

assert_response, don't cry if @exception isn't available in the template

Line 
1 require 'rexml/document'
2 require 'html/document'
3
4 module ActionController
5   module Assertions
6     # A small suite of assertions that test responses from Rails applications.
7     module ResponseAssertions
8       # Asserts that the response is one of the following types:
9       #
10       # * <tt>:success</tt>   - Status code was 200
11       # * <tt>:redirect</tt>  - Status code was in the 300-399 range
12       # * <tt>:missing</tt>   - Status code was 404
13       # * <tt>:error</tt>     - Status code was in the 500-599 range
14       #
15       # You can also pass an explicit status number like assert_response(501)
16       # or its symbolic equivalent assert_response(:not_implemented).
17       # See ActionController::StatusCodes for a full list.
18       #
19       # ==== Examples
20       #
21       #   # assert that the response was a redirection
22       #   assert_response :redirect
23       #
24       #   # assert that the response code was status code 401 (unauthorized)
25       #   assert_response 401
26       #
27       def assert_response(type, message = nil)
28         clean_backtrace do
29           if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
30             assert_block("") { true } # to count the assertion
31           elsif type.is_a?(Fixnum) && @response.response_code == type
32             assert_block("") { true } # to count the assertion
33           elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
34             assert_block("") { true } # to count the assertion
35           else
36             if @response.error?
37               exception = @response.template.instance_variable_get(:@exception)
38               exception_message = exception && exception.message
39               assert_block(build_message(message, "Expected response to be a <?>, but was <?>\n<?>", type, @response.response_code, exception_message.to_s)) { false }
40             else
41               assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
42             end
43           end
44         end
45       end
46
47       # Assert that the redirection options passed in match those of the redirect called in the latest action.
48       # This match can be partial, such that assert_redirected_to(:controller => "weblog") will also
49       # match the redirection of redirect_to(:controller => "weblog", :action => "show") and so on.
50       #
51       # ==== Examples
52       #
53       #   # assert that the redirection was to the "index" action on the WeblogController
54       #   assert_redirected_to :controller => "weblog", :action => "index"
55       #
56       #   # assert that the redirection was to the named route login_url
57       #   assert_redirected_to login_url
58       #
59       def assert_redirected_to(options = {}, message=nil)
60         clean_backtrace do
61           assert_response(:redirect, message)
62           return true if options == @response.redirected_to
63           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
64
65           begin
66             url  = {}
67             original = { :expected => options, :actual => @response.redirected_to.is_a?(Symbol) ? @response.redirected_to : @response.redirected_to.dup }
68             original.each do |key, value|
69               if value.is_a?(Symbol)
70                 value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{value}_url")
71               end
72
73               unless value.is_a?(Hash)
74                 request = case value
75                   when NilClass    then nil
76                   when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil)
77                   else                  recognized_request_for(value)
78                 end
79                 value = request.path_parameters if request
80               end
81
82               if value.is_a?(Hash) # stringify 2 levels of hash keys
83                 if name = value.delete(:use_route)
84                   route = ActionController::Routing::Routes.named_routes[name]
85                   value.update(route.parameter_shell)
86                 end
87
88                 value.stringify_keys!
89                 value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! }
90                 if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash)
91                   original[:actual].stringify_keys!
92                   value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller']
93                 end
94               end
95
96               if value.respond_to?(:[]) && value['controller']
97                 value['controller'] = value['controller'].to_s
98                 if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/')
99                   new_controller_path = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
100                   value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path)
101                 end
102                 value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
103               end
104               url[key] = value
105             end
106
107             @response_diff = url[:actual].diff(url[:expected]) if url[:actual]
108             msg = build_message(message, "expected a redirect to <?>, found one to <?>, a difference of <?> ", url[:expected], url[:actual], @response_diff)
109
110             assert_block(msg) do
111               url[:expected].keys.all? do |k|
112                 if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path)
113                 else parameterize(url[:expected][k]) == parameterize(url[:actual][k])
114                 end
115               end
116             end
117           rescue ActionController::RoutingError # routing failed us, so match the strings only.
118             msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url)
119             url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$}
120             eurl, epath, url, path = [options, @response.redirect_url].collect do |url|
121               u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url]
122               [u, (p.first == '/') ? p : '/' + p]
123             end.flatten
124
125             assert_equal(eurl, url, msg) if eurl && url
126             assert_equal(epath, path, msg) if epath && path
127           end
128         end
129       end
130
131       # Asserts that the request was rendered with the appropriate template file.
132       #
133       # ==== Examples
134       #
135       #   # assert that the "new" view template was rendered
136       #   assert_template "new"
137       #
138       def assert_template(expected = nil, message=nil)
139         clean_backtrace do
140           rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
141           msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
142           assert_block(msg) do
143             if expected.nil?
144               !@response.rendered_with_file?
145             else
146               expected == rendered
147             end
148           end
149         end
150       end
151
152       private
153         # Recognizes the route for a given path.
154         def recognized_request_for(path, request_method = nil)
155           path = "/#{path}" unless path.first == '/'
156
157           # Assume given controller
158           request = ActionController::TestRequest.new({}, {}, nil)
159           request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
160           request.path   = path
161
162           ActionController::Routing::Routes.recognize(request)
163           request
164         end
165
166         # Proxy to to_param if the object will respond to it.
167         def parameterize(value)
168           value.respond_to?(:to_param) ? value.to_param : value
169         end
170     end
171   end
172 end
Note: See TracBrowser for help on using the browser.