If you're not already using Rack::Test
for this then you should be. Rack::Test
is better suited than Capybara for testing API requests. It can be configured in your rspec/spec_helper.rb
RSpec.configure do |config|
# ...
config.include Rack::Test::Methods
end
When you're configured to use Rack::Test, you can set headers before the request like so:
it 'POST /api/enpoint authenticates successfully' do
header 'Authorization', '...'
post "/api/endpoint", params
expect(last_response).to be_ok
end
This will be accessible in your controller as request.headers['HTTP_AUTHORIZATION']
.
The source code for this method can be found here - https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb#L127-L141