Module: Merb::Test::MultipartRequestHelper

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

Defined Under Namespace

Classes: FileParam, Param, Post

Instance Method Summary (collapse)

Instance Method Details

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

Note:

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

Note:

Does not use routes.

Similar to dispatch_to but allows for sending files inside params.

Examples:

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

See Also:



107
108
109
110
# File 'merb-core/lib/merb-core/test/helpers/multipart_request_helper.rb', line 107

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

- (RequestHelper::FakeRequest) multipart_fake_request(env = {}, params = {})



160
161
162
163
164
165
166
167
168
169
# File 'merb-core/lib/merb-core/test/helpers/multipart_request_helper.rb', line 160

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

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

Note:

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

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



13
14
15
16
# File 'merb-core/lib/merb-core/two-oh.rb', line 13

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

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

Note:

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

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



19
20
21
22
# File 'merb-core/lib/merb-core/two-oh.rb', line 19

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

- (Object) multipart_request(path, params = {}, env = {})



3
4
5
6
7
8
9
10
# File 'merb-core/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