4
votes

I am using RSpec and Capybara for my feature specs on my Rails 5.1 app. I want to fake the request IP to '1.2.3.4' for a single spec.

I've tried the following with Poltergeist...

before do
  page.driver.add_headers 'REMOTE_HOST' => '1.2.3.4'
end

However, placing a pry in my controller I see that request.headers['REMOTE_HOST'] is 127.0.0.1.

1
Sounds like this is more applicable to a controller spec or request spec than a feature spec. - Thomas Walpole
I want to test different behavior in my views based on the request IP - HarlemSquirrel
That can be done from a view spec or request spec or controller spec (if you tell it to render the views). All places you could modify the request, and/or where its normal to mock/stub things (unlike feature tests) - Thomas Walpole

1 Answers

4
votes

I solved this by stubbing ActionDispatch::Request#remote_ip

allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip) { '1.2.3.4' }

http://guides.rubyonrails.org/action_controller_overview.html#the-request-object

I would prefer altering the actual request if possible.