How do I set HTTP_REFERER when testing in Rails?
The accepted answer doesn't work for integration tests because the @request
variable doesn't exist.
According to RailsGuides, you can pass headers to the helpers.
Rails <= 4:
test "blah" do
get root_path, {}, {'HTTP_REFERER' => 'http://foo.com'}
...
end
Rails >= 5:
test "blah" do
get root_path, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://foo.com" }
...
end
Their recommendation translates to the following:
setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end