Appending headers to Rspec controller tests

Dave Long picture Dave Long · Dec 11, 2013 · Viewed 11.3k times · Source

I'm trying to write out tests for a controller of mine that takes in requests from external services. So far this is my test:

describe ApplyController do
  context 'when valid' do
    let(:parameters) do
      file = File.join File.dirname(__FILE__), '..', 'samples', 'Indeed.json'
      JSON.parse(File.read file)
    end
    let(:signature) { 'GC02UVj0d4bqa5peNFHdPQAZ2BI=' }

    subject(:response) { post :indeed, parameters, 'X-Indeed-Signature' => signature }

    it 'returns 200 ok if Request is valid' do
      expect(response.status).to eq 200
    end
  end
end

This should work according to the documentation I could find.

My controller right now looks something like this:

class ApplyController < Application Controller
  def indeed
    binding.pry
  end
end

When I get into Pry in my test and try to check the value of request.headers['X-Indeed-Signature'] I always just get nil

Is there something that I am missing? I am using Rails 3.2 and Rspec 3

Answer

John Naegle picture John Naegle · Dec 11, 2013

I think you want (straight from one of your links)

it "returns 200 ok"
  @request.headers['X-Indeed-Signature'] = signature
  post :indeed, parameters
  response.status.should == 200
end

You don't need subject(:response)