harshs08
harshs08

Reputation: 730

RSpec Post with a file, params and header field

I am trying to test out an api using RSpec for uploading file. The api request is a simple POST request with a file along with some params and also a header field.

I tried making the POST request with the following code:

post "media.upload", { client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json' },
{ 'HTTP_PROXY_SSL' => 'true' }, :upload => @file

But I get the following error:

Failure/Error: post "cm/media.upload", { client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json' },
     ArgumentError:
       wrong number of arguments (4 for 1..3)

The @file variable is defined as:

@file = fixture_file_upload(Rails.root + 'spec/fixtures/images/abc.jpg', 'image/jpeg')

So my question is how to do a POST request in RSpec with a file, parameters and also some headers?

Upvotes: 3

Views: 1278

Answers (1)

infused
infused

Reputation: 24347

Move the upload param in with the rest of the request params:

params = {upload: @file, client_id: @client.id, token: @client.token, type: @type, name: @name, format: 'json'}
headers = {'HTTP_PROXY_SSL' => 'true'}

post 'media.upload', params, headers

Upvotes: 2

Related Questions