Module: Merb::Test::MultipartRequestHelper

Included in:
ControllerHelper
Defined in:
lib/merb-core/two-oh.rb,
lib/merb-core/test/helpers/multipart_request_helper.rb

Defined Under Namespace

Classes: FileParam, Param, Post

Instance Method Summary collapse

Instance Method Details

#dispatch_multipart_to(controller_klass, action, params = {}, env = {}, &blk) ⇒ Object

Similar to dispatch_to but allows for sending files inside params.

Paramters

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).

&blk

The block is executed in the context of the controller.

Example

dispatch_multipart_to(MyController, :create, :my_file => @a_file ) do |controller|
  controller.stub!(:current_user).and_return(@user)
end

Notes

Set your option to contain a file object to simulate file uploads.

Does not use routes.




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

def dispatch_multipart_to(controller_klass, action, params = {}, env = {}, &blk)
  request = multipart_fake_request(env, params)
  dispatch_request(request, controller_klass, action, &blk)
end

#multipart_fake_request(env = {}, params = {}) ⇒ Object

Parameters

env<Hash>

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

params<Hash>

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

Returns

FakeRequest

A multipart Request object that is built based on the parameters.



165
166
167
168
169
170
171
172
173
174
# File 'lib/merb-core/test/helpers/multipart_request_helper.rb', line 165

def multipart_fake_request(env = {}, params = {})
  if params.empty?
    fake_request(env)
  else
    m = Post.new(params)
    body, head = m.to_multipart
    fake_request(env.merge( :content_type => head, 
                            :content_length => body.length), :post_body => body)
  end
end

#multipart_post(path, params = {}, env = {}, &block) ⇒ Object

An HTTP POST request that operates through the router and uses multipart parameters.

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).

block<Proc>

The block is executed in the context of the controller.

Notes

To include an uploaded file, put a file object as a value in params.



26
27
28
29
# File 'lib/merb-core/two-oh.rb', line 26

def multipart_post(path, params = {}, env = {})
  env[:method] = "POST"
  multipart_request(path, params, env)
end

#multipart_put(path, params = {}, env = {}, &block) ⇒ Object

An HTTP PUT request that operates through the router and uses multipart parameters.

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).

block<Proc>

The block is executed in the context of the controller.

Notes

To include an uplaoded file, put a file object as a value in params.



45
46
47
48
# File 'lib/merb-core/two-oh.rb', line 45

def multipart_put(path, params = {}, env = {}, &block)
  env[:method] = "PUT"
  multipart_request(path, params, env)
end

#multipart_request(path, params = {}, env = {}) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/merb-core/two-oh.rb', line 3

def multipart_request(path, params = {}, env = {})
  multipart = Merb::Test::MultipartRequestHelper::Post.new(params)
  body, head = multipart.to_multipart
  env["CONTENT_TYPE"] = head
  env["CONTENT_LENGTH"] = body.size
  env[:input] = StringIO.new(body)
  request(path, env)
end