1 require 'rexml/document'
2 require 'html/document'
4 module ActionController
6 module ResponseAssertions
7 # Asserts that the response is one of the following types:
9 # * <tt>:success</tt> - Status code was 200
10 # * <tt>:redirect</tt> - Status code was in the 300-399 range
11 # * <tt>:missing</tt> - Status code was 404
12 # * <tt>:error</tt> - Status code was in the 500-599 range
14 # You can also pass an explicit status number like assert_response(501)
15 # or its symbolic equivalent assert_response(:not_implemented).
16 # See ActionController::StatusCodes for a full list.
17 def assert_response(type, message = nil)
19 if [ :success, :missing, :redirect, :error ].include?(type) && @response.send("#{type}?")
20 assert_block("") { true } # to count the assertion
21 elsif type.is_a?(Fixnum) && @response.response_code == type
22 assert_block("") { true } # to count the assertion
23 elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type]
24 assert_block("") { true } # to count the assertion
26 assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
31 # Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial,
32 # such that assert_redirected_to(:controller => "weblog") will also match the redirection of
33 # redirect_to(:controller => "weblog", :action => "show") and so on.
34 def assert_redirected_to(options = {}, message=nil)
36 assert_response(:redirect, message)
37 return true if options == @response.redirected_to
38 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
42 original = { :expected => options, :actual => @response.redirected_to.is_a?(Symbol) ? @response.redirected_to : @response.redirected_to.dup }
43 original.each do |key, value|
44 if value.is_a?(Symbol)
45 value = @controller.respond_to?(value, true) ? @controller.send(value) : @controller.send("hash_for_#{value}_url")
48 unless value.is_a?(Hash)
50 when NilClass then nil
51 when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil)
52 else recognized_request_for(value)
54 value = request.path_parameters if request
57 if value.is_a?(Hash) # stringify 2 levels of hash keys
58 if name = value.delete(:use_route)
59 route = ActionController::Routing::Routes.named_routes[name]
60 value.update(route.parameter_shell)
64 value.values.select { |v| v.is_a?(Hash) }.collect { |v| v.stringify_keys! }
65 if key == :expected && value['controller'] == @controller.controller_name && original[:actual].is_a?(Hash)
66 original[:actual].stringify_keys!
67 value.delete('controller') if original[:actual]['controller'].nil? || original[:actual]['controller'] == value['controller']
71 if value.respond_to?(:[]) && value['controller']
72 value['controller'] = value['controller'].to_s
73 if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/')
74 new_controller_path = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path)
75 value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path)
77 value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
82 @response_diff = url[:actual].diff(url[:expected]) if url[:actual]
83 msg = build_message(message, "expected a redirect to <?>, found one to <?>, a difference of <?> ", url[:expected], url[:actual], @response_diff)
86 url[:expected].keys.all? do |k|
87 if k == :controller then url[:expected][k] == ActionController::Routing.controller_relative_to(url[:actual][k], @controller.class.controller_path)
88 else parameterize(url[:expected][k]) == parameterize(url[:actual][k])
92 rescue ActionController::RoutingError # routing failed us, so match the strings only.
93 msg = build_message(message, "expected a redirect to <?>, found one to <?>", options, @response.redirect_url)
94 url_regexp = %r{^(\w+://.*?(/|$|\?))(.*)$}
95 eurl, epath, url, path = [options, @response.redirect_url].collect do |url|
96 u, p = (url_regexp =~ url) ? [$1, $3] : [nil, url]
97 [u, (p.first == '/') ? p : '/' + p]
100 assert_equal(eurl, url, msg) if eurl && url
101 assert_equal(epath, path, msg) if epath && path
106 # Asserts that the request was rendered with the appropriate template file.
107 def assert_template(expected = nil, message=nil)
109 rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
110 msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
113 !@response.rendered_with_file?
122 # Recognizes the route for a given path.
123 def recognized_request_for(path, request_method = nil)
124 path = "/#{path}" unless path.first == '/'
126 # Assume given controller
127 request = ActionController::TestRequest.new({}, {}, nil)
128 request.env["REQUEST_METHOD"] = request_method.to_s.upcase if request_method
131 ActionController::Routing::Routes.recognize(request)
135 # Proxy to to_param if the object will respond to it.
136 def parameterize(value)
137 value.respond_to?(:to_param) ? value.to_param : value