Module: Merb::Test::RequestHelper

Included in:
ControllerHelper, RouteHelper
Defined in:
merb-core/lib/merb-core/test/helpers/request_helper.rb,
merb-core/lib/merb-core/test/helpers/mock_request_helper.rb

Defined Under Namespace

Classes: CookieJar, FakeRequest

Instance Method Summary (collapse)

Instance Method Details

- (Object) build_request(params = {}, env = {})

Deprecated.
Note:

Does not use routes.

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.

Examples:

req = build_request(:id => 1)
req.cookies['app_cookie'] = "testing"
dispatch_request(req, MyController, :edit)

Parameters:

  • params (Hash) (defaults to: {})

    An optional hash that will end up as params in the controller instance.

  • env (Hash) (defaults to: {})

    An optional hash that is passed to the fake request. Any request options should go here including :req or :post_body for setting the request body itself.

See Also:



193
194
195
196
197
198
199
200
201
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 193

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

- (Hash) check_request_for_route(request)

Deprecated.

Checks to see that a request is routable.

Parameters:

Returns:

  • (Hash)

    The parameters built based on the matching route.

Raises:



287
288
289
290
291
292
293
294
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 287

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

- (Object) describe_input(input)



10
11
12
13
14
15
16
17
18
# File 'merb-core/lib/merb-core/test/helpers/request_helper.rb', line 10

def describe_input(input)
  if input.respond_to?(:controller_name)
    "#{input.controller_name}##{input.action_name}"
  elsif input.respond_to?(:original_env)
    describe_request(input)
  else
    input
  end
end

- (Object) describe_request(rack)



6
7
8
# File 'merb-core/lib/merb-core/test/helpers/request_helper.rb', line 6

def describe_request(rack)
  "a #{rack.original_env[:method] || rack.original_env["REQUEST_METHOD"] || "GET"} to '#{rack.url}'"
end

- (Object) dispatch_request(request, controller_klass, action) {|controller| ... }

Deprecated.
Note:

Does not use routes.

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 (Class)

    The class object of the Controller to dispatch the action to.

  • &blk

    The controller is yielded to the block provided for actions prior to the action being dispatched.

  • action (#to_s)

    The action to dispatch to.

Yield Parameters:

Returns:

  • An instance of controller_klass based on the parameters.



264
265
266
267
268
269
270
271
272
273
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 264

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

- (Object) dispatch_to(controller_klass, action, params = {}, env = {}, &blk)

Deprecated.

Deprecation note questionable.

Note:

Does not use routes.

Dispatches an action to the given class. This bypasses the router and is suitable for unit testing of controllers.

Examples:

dispatch_to(MyController, :create, :name => 'Homer' ) do |controller|
  controller.stub!(:current_user).and_return(@user)
end

Parameters:

  • controller_klass (Class)

    The class object of the Controller to dispatch the action to.

  • action (#to_s)

    The action to dispatch to.

  • params (Hash) (defaults to: {})

    An optional hash that will end up as params in the controller instance.

  • env (Hash) (defaults to: {})

    An optional hash that is passed to the fake request. Any request options should go here 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.

See Also:



109
110
111
112
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 109

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

- (Object) dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk)

Deprecated.
Note:

Does not use routes.

Dispatches an action to the given class and using HTTP Basic Authentication This bypasses the router and is suitable for unit testing of controllers.

Examples:

dispatch_with_basic_authentication_to(MyController, :create, 'Fred', 'secret', :name => 'Homer' ) do |controller|
  controller.stub!(:current_user).and_return(@user)
end

Parameters:

  • username (String)

    The username.

  • password (String)

    The password.

  • controller_klass (Class)

    The class object of the Controller to dispatch the action to.

  • action (#to_s)

    The action to dispatch to.

  • params (Hash) (defaults to: {})

    An optional hash that will end up as params in the controller instance.

  • env (Hash) (defaults to: {})

    An optional hash that is passed to the fake request. Any request options should go here 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.



155
156
157
158
159
160
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 155

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

- (FakeRequest) fake_request(env = {}, opt = {})

Deprecated.
Note:

If you pass a post body, the content-type will be set to URL-encoded.

Return a FakeRequest built with provided parameters.

Parameters:

  • env (Hash) (defaults to: {})

    A hash of environment keys to be merged into the default list.

  • opt (Hash) (defaults to: {})

    A hash of options.

Options Hash (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.



79
80
81
82
83
84
85
86
87
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 79

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

- (Object) merge_controller_and_action(controller_klass, action, params)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



163
164
165
166
167
168
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 163

def merge_controller_and_action(controller_klass, action, params)
  params[:controller] = controller_klass.name.to_const_path
  params[:action]     = action.to_s

  params
end

- (Object) mock_request(path, params = {}, env = {}, &block)

Deprecated.
Note:

Uses Routes.

A generic request that checks the router for the controller and action. This request goes through the Merb::Router and finishes at the controller.

Examples:

request(path, { :name => 'Homer' }, { :request_method => "PUT" }) do |controller|
  controller.stub!(:current_user).and_return(@user)
end

Parameters:

  • path (String)

    The path that should go to the router as the request uri.

  • params (Hash) (defaults to: {})

    An optional hash that will end up as params in the controller instance.

  • env (Hash) (defaults to: {})

    An optional hash that is passed to the fake request. Any request options should go here.

  • &blk

    The controller is yielded to the block provided for actions prior to the action being dispatched.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 226

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

- (Object) status_code(input)



20
21
22
# File 'merb-core/lib/merb-core/test/helpers/request_helper.rb', line 20

def status_code(input)
  input.respond_to?(:status) ? input.status : input
end

- (Object) with_cookies(*controller_classes, &blk)

Deprecated.

Keep track of cookie values in CookieJar within the context of the block; you need to set this up for specific controllers.

Parameters:

  • controller_classes (Array<Class>)

    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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'merb-core/lib/merb-core/test/helpers/mock_request_helper.rb', line 124

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