How do I set HTTP_REFERER when testing in Rails?
Their recommendation translates to the following:
setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end
How to set the referrer in an _integration_ test
Capybara.current_session.driver.header 'Referer', 'http://example.com'
It looks like you're using Capybara, you can use Capybara to explicitly set the referer. You would have to update it whenever you wanted it to change, and if you needed to remove it you could set it to nil
.
Maybe a bit cleaner:
referer = 'http://example.com'
Capybara.current_session.driver.header 'Referer', referer
Rails using request.referer in testing
Two things:
First, this answers your main question:
How do I set HTTP_REFERER when testing in Rails?
Second, it's not a given that request.referer will be set. Most browsers supply the header when you navigate from a previous page; most don't when you hand-enter a URL. HTTP clients can't be assumed to do so overall, and you have to be prepared to get nil from that attribute.
Rails - Functional testing of redirect_to(request.referer)
Got it.
Passing test is:
test "should destroy step" do
assert_difference('Step.count', -1) do
@request.env['HTTP_REFERER'] = 'http://test.com/steps/1'
delete :destroy, :id => @step.to_param
end
assert_redirected_to @request.env['HTTP_REFERER']
end
Thanks to help from: How do I set HTTP_REFERER when testing in Rails?
How do i set a request.referrer inside my RSpec?
Mock it:
specify 'foo' do
controller.request.should_receive(:referer).and_return('http://example.com')
# get whatever
end
Or if you don't care if it doesn't get called, stub it:
controller.request.stub referer: 'http://example.com'
How to set HTTP_REFERER in cucumber+capybara step definitions?
Since I'm using Cucumber::RackTest::Driver, the solution for me at the time of writing is:
Capybara.current_session.driver.header 'Referer', 'http://example.com'
This solution was inspired by the following unmerged pull request on capybara:
https://github.com/thoughtbot/capybara-webkit/issues/198
Rails error: No HTTP_REFERER was set in the request to this action
what is HTTP_REFERER?
The HTTP referer is an HTTP header field that identifies the address of the webpage (i.e. the URI or IRI) that linked to the resource being requested. This is set by ActionController::Request
object.
why redirect to back will trigger this error?
This usually happens when request.env["HTTP_REFERER"]
is not set. (I also wonder to know in which case it is set and not set)
You could refer this answer to fix your issue.
(OR) I would highly prefer to define a custom page for access denied and redirect to it instead of redirecting :back
(which I think bad idea)
For example from cancan
gem docs,
rescue_from CanCan::AccessDenied do |exception|
render :file => "#{Rails.root}/public/403.html", :status => 403, :layout => false
## to avoid deprecation warnings with Rails 3.2.x (and incidentally using Ruby 1.9.3 hash syntax)
## this render call should be:
# render file: "#{Rails.root}/public/403", formats: [:html], status: 403, layout: false
end
Hope this helps!
Rspec testing redirect_to :back
Using RSpec, you can set the referer in a before
block. When I tried to set the referer directly in the test, it didn't seem to work no matter where I put it, but the before block does the trick.
describe BackController < ApplicationController do
before(:each) do
request.env["HTTP_REFERER"] = "where_i_came_from"
end
describe "GET /goback" do
it "redirects back to the referring page" do
get 'goback'
response.should redirect_to "where_i_came_from"
end
end
end
Related Topics
What's the Difference Between Gets.Chomp() VS. Stdin.Gets.Chomp()
Finding Out Current Index in Each Loop (Ruby)
Get the Value of an Instance Variable Given Its Name
Rails: Postgres Permission Denied to Create Database on Rake Db:Create:All
How to Make Instance Variables Private in Ruby
Why Should One Use a Http Server in Front of a Framework Web Server
How to Deal with the Conflict Between Activesupport::JSON and the JSON Gem
Finding the Element of a Ruby Array with the Maximum Value for a Particular Attribute
How to Check to See If My Array Includes an Object
Differencebetween Integer and Fixnum
What Does 'Def Self.Function' Name Mean
How to Count the Number of Records That Have a Unique Value in a Particular Field in Ror
Guard with Rspec on Rails 4 Giving a Lot of Warnings