/usr/lib/ruby/vendor_ruby/merb-core/test/helpers/mock_request_helper.rb is in ruby-merb-core 1.1.3+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | require 'tempfile'
module Merb
module Test
module RequestHelper
# FakeRequest sets up a default enviroment which can be overridden either
# by passing and env into initialize or using request['HTTP_VAR'] = 'foo'
class FakeRequest < Request
# ==== Parameters
# env<Hash>:: Environment options that override the defaults.
# req<StringIO>:: The request to set as input for Rack.
def initialize(env = {}, req = StringIO.new)
env.environmentize_keys!
env['rack.input'] = req
@start = Time.now
super(DEFAULT_ENV.merge(env))
end
def self.new(env = {}, req = StringIO.new)
super
end
private
DEFAULT_ENV = Mash.new({
'SERVER_NAME' => 'localhost',
'PATH_INFO' => '/',
'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060214 Camino/1.0',
'SCRIPT_NAME' => '/',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'HTTP_CACHE_CONTROL' => 'max-age=0',
'HTTP_ACCEPT_LANGUAGE' => 'en,ja;q=0.9,fr;q=0.9,de;q=0.8,es;q=0.7,it;q=0.7,nl;q=0.6,sv;q=0.5,nb;q=0.5,da;q=0.4,fi;q=0.3,pt;q=0.3,zh-Hans;q=0.2,zh-Hant;q=0.1,ko;q=0.1',
'HTTP_HOST' => 'localhost',
'REMOTE_ADDR' => '127.0.0.1',
'SERVER_SOFTWARE' => 'Mongrel 1.1',
'HTTP_KEEP_ALIVE' => '300',
'HTTP_REFERER' => 'http://localhost/',
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'HTTP_VERSION' => 'HTTP/1.1',
'REQUEST_URI' => '/',
'SERVER_PORT' => '80',
'GATEWAY_INTERFACE' => 'CGI/1.2',
'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
'HTTP_CONNECTION' => 'keep-alive',
'REQUEST_METHOD' => 'GET'
}) unless defined?(DEFAULT_ENV)
end
# CookieJar keeps track of cookies in a simple Mash.
class CookieJar < Mash
# ==== Parameters
# request<Merb::Request, Merb::FakeRequest>:: The controller request.
def update_from_request(request)
request.cookies.each do |key, value|
if value.blank?
self.delete(key)
else
self[key] = Merb::Parse.unescape(value)
end
end
end
end
# ==== Parameters
# env<Hash>:: A hash of environment keys to be merged into the default list.
# opt<Hash>:: A hash of options (see below).
#
# ==== Options (opt)
# :post_body<String>:: The post body for the request.
# :req<String>::
# The request string. This will only be used if :post_body is left out.
#
# ==== Returns
# FakeRequest:: A Request object that is built based on the parameters.
#
# ==== Notes
# If you pass a post body, the content-type will be set to URL-encoded.
#
# :api: public
# @deprecated
def fake_request(env = {}, opt = {})
if opt[:post_body]
req = opt[:post_body]
env[:content_type] ||= "application/x-www-form-urlencoded"
else
req = opt[:req]
end
FakeRequest.new(env, StringIO.new(req || ''))
end
# Dispatches an action to the given class. This bypasses the router and is
# suitable for unit testing of controllers.
#
# ==== Parameters
# controller_klass<Controller>::
# The controller class object that the action should be dispatched to.
# action<Symbol>:: The action name, as a symbol.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see +fake_request+), including :req or :post_body
# for setting the request body itself.
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# ==== Example
# dispatch_to(MyController, :create, :name => 'Homer' ) do |controller|
# controller.stub!(:current_user).and_return(@user)
# end
#
# ==== Notes
# Does not use routes.
#
# :api: public
# @deprecated
def dispatch_to(controller_klass, action, params = {}, env = {}, &blk)
params = merge_controller_and_action(controller_klass, action, params)
dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
end
# Keep track of cookie values in CookieJar within the context of the
# block; you need to set this up for secific controllers.
#
# ==== Parameters
# *controller_classes:: Controller classes to operate on in the context of the block.
# &blk:: The context to operate on; optionally accepts the cookie jar as an argument.
#
# :api: public
# @deprecated
def with_cookies(*controller_classes, &blk)
cookie_jar = CookieJar.new
before_cb = lambda { |c| c.cookies.update(cookie_jar) }
after_cb = lambda { |c| cookie_jar.update_from_request(c.request) }
controller_classes.each do |klass|
klass._before_dispatch_callbacks << before_cb
klass._after_dispatch_callbacks << after_cb
end
blk.arity == 1 ? blk.call(cookie_jar) : blk.call
controller_classes.each do |klass|
klass._before_dispatch_callbacks.delete before_cb
klass._after_dispatch_callbacks.delete after_cb
end
end
# Dispatches an action to the given class and using HTTP Basic Authentication
# This bypasses the router and is suitable for unit testing of controllers.
#
# ==== Parameters
# controller_klass<Controller>::
# The controller class object that the action should be dispatched to.
# action<Symbol>:: The action name, as a symbol.
# username<String>:: The username.
# password<String>:: The password.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see +fake_request+), including :req or :post_body
# for setting the request body itself.
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# ==== Example
# dispatch_with_basic_authentication_to(MyController, :create, 'Fred', 'secret', :name => 'Homer' ) do |controller|
# controller.stub!(:current_user).and_return(@user)
# end
#
# ==== Notes
# Does not use routes.
#
# :api: public
# @deprecated
def dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk)
env["X_HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{username}:#{password}")}"
params = merge_controller_and_action(controller_klass, action, params)
dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
end
# :api: private
def merge_controller_and_action(controller_klass, action, params)
params[:controller] = controller_klass.name.to_const_path
params[:action] = action.to_s
params
end
# Prepares and returns a request suitable for dispatching with
# dispatch_request. If you don't need to modify the request
# object before dispatching (e.g. to add cookies), you probably
# want to use dispatch_to instead.
#
# ==== Parameters
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see +fake_request+), including :req or :post_body
# for setting the request body itself.
#
# ==== Example
# req = build_request(:id => 1)
# req.cookies['app_cookie'] = "testing"
# dispatch_request(req, MyController, :edit)
#
# ==== Notes
# Does not use routes.
#
# :api: public
# @deprecated
def build_request(params = {}, env = {})
params = Merb::Parse.params_to_query_string(params)
query_string = env[:query_string] || env['QUERY_STRING']
env[:query_string] = query_string ? "#{query_string}&#{params}" : params
post_body = env[:post_body] || env['POST_BODY']
fake_request(env, { :post_body => post_body, :req => env[:req] })
end
# An HTTP GET request that operates through the router.
#
# ==== Parameters
# path<String>:: The path that should go to the router as the request uri.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see +fake_request+).
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# :api: public
# @deprecated
def get(path, params = {}, env = {}, &block)
env[:request_method] = "GET"
mock_request(path, params, env, &block)
end
# An HTTP POST request that operates through the router.
#
# ==== Parameters
# path<String>:: The path that should go to the router as the request uri.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see fake_request).
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# :api: public
# @deprecated
def post(path, params = {}, env = {}, &block)
env[:request_method] = "POST"
mock_request(path, params, env, &block)
end
# An HTTP PUT request that operates through the router.
#
# ==== Parameters
# path<String>:: The path that should go to the router as the request uri.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see fake_request).
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# :api: public
def put(path, params = {}, env = {}, &block)
env[:request_method] = "PUT"
mock_request(path, params, env, &block)
end
# An HTTP DELETE request that operates through the router
#
# ==== Parameters
# path<String>:: The path that should go to the router as the request uri.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see fake_request).
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# :api: public
# @deprecated
def delete(path, params = {}, env = {}, &block)
env[:request_method] = "DELETE"
mock_request(path, params, env, &block)
end
# A generic request that checks the router for the controller and action.
# This request goes through the Merb::Router and finishes at the controller.
#
# ==== Parameters
# path<String>:: The path that should go to the router as the request uri.
# params<Hash>::
# An optional hash that will end up as params in the controller instance.
# env<Hash>::
# An optional hash that is passed to the fake request. Any request options
# should go here (see +fake_request+).
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# ==== Example
# request(path, { :name => 'Homer' }, { :request_method => "PUT" }) do |controller|
# controller.stub!(:current_user).and_return(@user)
# end
#
# ==== Notes
# Uses Routes.
#
# :api: plugin
# @deprecated
def mock_request(path, params = {}, env= {}, &block)
env[:request_method] ||= "GET"
env[:request_uri], env[:query_string] = path.split('?')
multipart = env.delete(:test_with_multipart)
request = build_request(params, env)
opts = check_request_for_route(request) # Check that the request will be routed correctly
controller_name = (opts[:namespace] ? opts.delete(:namespace) + '/' : '') + opts.delete(:controller)
klass = Object.full_const_get(controller_name.snake_case.to_const_string)
action = opts.delete(:action).to_s
params.merge!(opts)
multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block)
end
# The workhorse for the dispatch*to helpers.
#
# ==== Parameters
# request<Merb::Test::RequestHelper::FakeRequest, Merb::Request>::
# A request object that has been setup for testing.
# controller_klass<Merb::Controller>::
# The class object off the controller to dispatch the action to.
# action<Symbol>:: The action to dispatch the request to.
# &blk::
# The controller is yielded to the block provided for actions *prior* to
# the action being dispatched.
#
# ==== Returns
# An instance of +controller_klass+ based on the parameters.
#
# ==== Notes
# Does not use routes.
#
# :api: public
# @deprecated
def dispatch_request(request, controller_klass, action, &blk)
controller = controller_klass.new(request)
yield controller if block_given?
controller._dispatch(action)
Merb.logger.info controller._benchmarks.inspect
Merb.logger.flush
controller
end
# Checks to see that a request is routable.
#
# ==== Parameters
# request<Merb::Test::RequestHelper::FakeRequest, Merb::Request>::
# The request object to inspect.
#
# ==== Raises
# Merb::ControllerExceptions::BadRequest::
# No matching route was found.
#
# ==== Returns
# Hash:: The parameters built based on the matching route.
#
# :api: plugin
# @deprecated
def check_request_for_route(request)
match = ::Merb::Router.match(request)
if match[0].nil? && match[1].empty?
raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request. Request uri: #{request.uri}"
else
match[1]
end
end # check_request_for_route
end # RequestHelper
end # Test
end # Merb
|