Module Merb::Test::RequestHelper
In: merb-core/lib/merb-core/test/helpers/mock_request_helper.rb
merb-core/lib/merb-core/test/helpers/request_helper.rb

Methods

Included Modules

MakeRequest

Classes and Modules

Class Merb::Test::RequestHelper::CookieJar
Class Merb::Test::RequestHelper::FakeRequest

Public Instance methods

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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 212
212:       def build_request(params = {}, env = {})
213:         params             = Merb::Parse.params_to_query_string(params)
214: 
215:         query_string = env[:query_string] || env['QUERY_STRING']
216:         env[:query_string] = query_string ? "#{query_string}&#{params}" : params
217:         
218:         post_body = env[:post_body] || env['POST_BODY']
219:         fake_request(env, { :post_body => post_body, :req => env[:req] })
220:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 390
390:       def check_request_for_route(request)
391:         match =  ::Merb::Router.match(request)
392:         if match[0].nil? && match[1].empty?
393:           raise ::Merb::ControllerExceptions::BadRequest, "No routes match the request. Request uri: #{request.uri}"
394:         else
395:           match[1]
396:         end
397:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 296
296:       def delete(path, params = {}, env = {}, &block)
297:         env[:request_method] = "DELETE"
298:         mock_request(path, params, env, &block)
299:       end

[Source]

    # File merb-core/lib/merb-core/test/helpers/request_helper.rb, line 65
65:       def describe_input(input)
66:         if input.respond_to?(:controller_name)
67:           "#{input.controller_name}##{input.action_name}"
68:         elsif input.respond_to?(:original_env)
69:           describe_request(input)
70:         else
71:           input
72:         end
73:       end

[Source]

    # File merb-core/lib/merb-core/test/helpers/request_helper.rb, line 61
61:       def describe_request(rack)
62:         "a #{rack.original_env[:method] || rack.original_env["REQUEST_METHOD"] || "GET"} to '#{rack.url}'"
63:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 364
364:       def dispatch_request(request, controller_klass, action, &blk)
365:         controller = controller_klass.new(request)
366:         yield controller if block_given?
367:         controller._dispatch(action)
368: 
369:         Merb.logger.info controller._benchmarks.inspect
370:         Merb.logger.flush
371: 
372:         controller
373:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 116
116:       def dispatch_to(controller_klass, action, params = {}, env = {}, &blk)
117:         params = merge_controller_and_action(controller_klass, action, params)
118:         dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
119:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 174
174:       def dispatch_with_basic_authentication_to(controller_klass, action, username, password, params = {}, env = {}, &blk)
175:         env["X_HTTP_AUTHORIZATION"] = "Basic #{Base64.encode64("#{username}:#{password}")}"
176:         
177:         params = merge_controller_and_action(controller_klass, action, params)        
178:         dispatch_request(build_request(params, env), controller_klass, action.to_s, &blk)
179:       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

[Source]

    # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 79
79:       def fake_request(env = {}, opt = {})
80:         if opt[:post_body]
81:           req = opt[:post_body]
82:           env[:content_type] ||= "application/x-www-form-urlencoded"
83:         else
84:           req = opt[:req]
85:         end
86:         FakeRequest.new(env, StringIO.new(req || ''))
87:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 237
237:       def get(path, params = {}, env = {}, &block)
238:         env[:request_method] = "GET"
239:         mock_request(path, params, env, &block)
240:       end

:api: private

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 182
182:       def merge_controller_and_action(controller_klass, action, params)
183:         params[:controller] = controller_klass.name.to_const_path
184:         params[:action]     = action.to_s
185:         
186:         params
187:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 325
325:       def mock_request(path, params = {}, env= {}, &block)
326:         env[:request_method] ||= "GET"
327:         env[:request_uri], env[:query_string] = path.split('?')
328:         
329:         multipart = env.delete(:test_with_multipart)
330: 
331:         request = build_request(params, env)
332: 
333:         opts = check_request_for_route(request) # Check that the request will be routed correctly
334:         controller_name = (opts[:namespace] ? opts.delete(:namespace) + '/' : '') + opts.delete(:controller)
335:         klass = Object.full_const_get(controller_name.snake_case.to_const_string)
336:         
337:         action = opts.delete(:action).to_s
338:         params.merge!(opts)
339: 
340:         multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block)
341:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 257
257:       def post(path, params = {}, env = {}, &block)
258:         env[:request_method] = "POST"
259:         mock_request(path, params, env, &block)
260:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 276
276:       def put(path, params = {}, env = {}, &block)
277:         env[:request_method] = "PUT"
278:         mock_request(path, params, env, &block)
279:       end

[Source]

    # File merb-core/lib/merb-core/test/helpers/request_helper.rb, line 79
79:       def requesting(*args)   request(*args) end

[Source]

    # File merb-core/lib/merb-core/test/helpers/request_helper.rb, line 80
80:       def response_for(*args) request(*args) end

[Source]

    # File merb-core/lib/merb-core/test/helpers/request_helper.rb, line 75
75:       def status_code(input)
76:         input.respond_to?(:status) ? input.status : input
77:       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

[Source]

     # File merb-core/lib/merb-core/test/helpers/mock_request_helper.rb, line 130
130:       def with_cookies(*controller_classes, &blk)
131:         cookie_jar = CookieJar.new
132:         before_cb = lambda { |c| c.cookies.update(cookie_jar) }
133:         after_cb  = lambda { |c| cookie_jar.update_from_request(c.request) }
134:         controller_classes.each do |klass|
135:           klass._before_dispatch_callbacks << before_cb
136:           klass._after_dispatch_callbacks  << after_cb
137:         end
138:         blk.arity == 1 ? blk.call(cookie_jar) : blk.call
139:         controller_classes.each do |klass|
140:           klass._before_dispatch_callbacks.delete before_cb
141:           klass._after_dispatch_callbacks.delete after_cb
142:         end
143:       end

[Validate]