0

I'm looking to test "get" request, controller action via RSPec, with a special header value. I have looked on Stackoverflow & nothing I have tried works. It never actually changes the header.

Here is what I have:

RSpec.describe IftttController, type: :controller do
  context '/v1/status works with proper code' do
    before :each do
      request.headers.merge!({ 'IFTTT-Channel-Key' => ENV['IFTTT_CODE'] })
      get :status
    end
    it 'return status code 200' do
      expect(response.code).to eq('200')
      expect(response).to have_http_status(:ok)
    end
  end
end

The headers never actually show up in my controller: {"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff", "X-Download-Options"=>"noopen", "X-Permitted-Cross-Domain-Policies"=>"none", "Referrer-Policy"=>"strict-origin-when-cross-origin"}

Before you point me to the docs or other StackOverflow questions, I have tried:

This: https://relishapp.com/rspec-staging/rspec-rails/docs/controller-specs/setting-request-headers This: How to set request headers in rspec request spec? This: https://gist.github.com/quadrolls/9203f924f934398f6992162bbbcb1a0c

Nothing seems to actually pass the headers to the controller, whether I use type: :request or type: :controller or anything else that I try.

Jackson Riso
  • 85
  • 1
  • 6
  • I guess there is a difference between headers && request.headers in a rails controller? Why? – Jackson Riso Jan 28 '21 at 05:19
  • You want to use a request spec and not a controller spec. Request specs send actual http requests - controller specs stub out huge chunks of the framework and their use is discouraged.https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec – max Jan 28 '21 at 06:42

0 Answers0