Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / lib / action_controller / assertions / response_assertions.rb
blob29eb2b033ab9a4ed8b9e82f9609cb543570f573c
1 require 'rexml/document'
2 require 'html/document'
4 module ActionController
5   module Assertions
6     module ResponseAssertions
7       # Asserts that the response is one of the following types:
8       #
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
13       #
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)
18         clean_backtrace do
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
25           else
26             assert_block(build_message(message, "Expected response to be a <?>, but was <?>", type, @response.response_code)) { false }
27           end
28         end
29       end
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)
35         clean_backtrace do
36           assert_response(:redirect, message)
37           return true if options == @response.redirected_to
38           ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty?
40           begin
41             url  = {}
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")
46               end
48               unless value.is_a?(Hash)
49                 request = case value
50                   when NilClass    then nil
51                   when /^\w+:\/\// then recognized_request_for(%r{^(\w+://.*?(/|$|\?))(.*)$} =~ value ? $3 : nil)
52                   else                  recognized_request_for(value)
53                 end
54                 value = request.path_parameters if request
55               end
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)
61                 end
63                 value.stringify_keys!
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']
68                 end
69               end
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)
76                 end
77                 value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash
78               end
79               url[key] = value
80             end
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)
85             assert_block(msg) do
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])
89                 end
90               end
91             end
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]
98             end.flatten
100             assert_equal(eurl, url, msg) if eurl && url
101             assert_equal(epath, path, msg) if epath && path
102           end
103         end
104       end
106       # Asserts that the request was rendered with the appropriate template file.
107       def assert_template(expected = nil, message=nil)
108         clean_backtrace do
109           rendered = expected ? @response.rendered_file(!expected.include?('/')) : @response.rendered_file
110           msg = build_message(message, "expecting <?> but rendering with <?>", expected, rendered)
111           assert_block(msg) do
112             if expected.nil?
113               !@response.rendered_with_file?
114             else
115               expected == rendered
116             end
117           end
118         end
119       end
121       private
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
129           request.path   = path
131           ActionController::Routing::Routes.recognize(request)
132           request
133         end
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
138         end
139     end
140   end