Sunday, 25 August 2013

Sending custom headers through RSpec

Sending custom headers through RSpec

Given my API consumers are required to send a customer HTTP header like this:
# curl -H 'X-SomeHeader: 123' http://127.0.0.1:3000/api/api_call.json
Then I can read this header in a before_filter method like this:
class ApiController < ApplicationController
before_filter :log_request
private
def log_request
logger.debug "Header: #{request.env['HTTP_X_SOMEHEADER']}"
...
end
end
So far great. Now I would like to test this using RSpec as there is a
change in behavior:
describe ApiController do
it "should process the header" do
@request.env['HTTP_X_SOMEHEADER'] = '123'
get :api_call
...
end
end
However, the request received in ApiController will not be able to find
the header variable.
When trying the same code with the HTTP_ACCEPT_LANGUAGE header, it will
work. Are custom headers filtered somewhere?

No comments:

Post a Comment