HarlemSquirrel
HarlemSquirrel

Reputation: 10124

How can I set the request IP in a Capybara feature spec?

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.

Upvotes: 5

Views: 1562

Answers (1)

HarlemSquirrel
HarlemSquirrel

Reputation: 10124

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.

Upvotes: 7

Related Questions