| 1 |
require 'rexml/document' |
|---|
| 2 |
require 'html/document' |
|---|
| 3 |
|
|---|
| 4 |
module ActionController |
|---|
| 5 |
module Assertions |
|---|
| 6 |
|
|---|
| 7 |
module ResponseAssertions |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 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 } |
|---|
| 31 |
elsif type.is_a?(Fixnum) && @response.response_code == type |
|---|
| 32 |
assert_block("") { true } |
|---|
| 33 |
elsif type.is_a?(Symbol) && @response.response_code == ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[type] |
|---|
| 34 |
assert_block("") { true } |
|---|
| 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 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 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) |
|---|
| 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 == '/' |
|---|
| 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 |
|---|
| 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 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 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 |
|
|---|
| 154 |
def recognized_request_for(path, request_method = nil) |
|---|
| 155 |
path = "/#{path}" unless path.first == '/' |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 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 |
|
|---|
| 167 |
def parameterize(value) |
|---|
| 168 |
value.respond_to?(:to_param) ? value.to_param : value |
|---|
| 169 |
end |
|---|
| 170 |
end |
|---|
| 171 |
end |
|---|
| 172 |
end |
|---|