Search code examples
ruby-on-railsrubyrspecbddrspec-rails

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?


Solution

  • 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