Setting http headers RSpec 2.4 / Rails 3
Asked Answered
V

3

17

I am getting started with RSpec. I have a new rails 3 app which uses the HTTP_ACCEPT_HEADER or the request 2 letter subdomain to set the application language and redirect accordingly. I am successfully testing my redirection code using Cucumber.

Now I want to write my controller specs and I need to set the request subdomain before my test.

In my cucumber steps, I can specify:

header 'HTTP_HOST', 'es.mysite.local'
visit '/'

But when I try to do this in a spec file

header 'HTTP_HOST', 'es.mysite.local'
get 'index'

I get this error:

Failure/Error: header 'HTTP_HOST', "es.mysite.local"
 LoadError:
   no such file to load -- action_controller/integration

Any clue on how to solve this?

Variance answered 9/2, 2011 at 16:53 Comment(0)
O
30

Try this:

request.env['HTTP_HOST'] = 'es.mysite.local'
get 'index'
Obstruction answered 9/2, 2011 at 18:45 Comment(2)
Thank you a lot! I had already tried that, but I now realize that another code example was reaching my debug point first.Variance
Thanks! It works today with Rails 3.0.7 and avoided me to put full urls with subdomains everywhere.Conduit
R
2

The previous answer is correct, and in general the name of the header should be in all caps, prefixed with HTTP_, and separated with underscores. For example the "If-Modified-Since" header can be set with:

request.env['HTTP_IF_MODIFIED_SINCE'] = Time.now.httpdate
Roveover answered 29/3, 2013 at 23:21 Comment(0)
T
0

Setting an HTTP header for every request in rspec is also possible. Add to your spec_helper inside the RSpec.configure do |config| block:

config.before(:each) do |x|
    x.request.env['HTTP_ACCEPT_LANGUAGE'] = 'de-CH,de;q=0.8,en;q=0.6'
end
Tammara answered 4/12, 2013 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.