How do I change the default "www.example.com" domain for testing in rails?
Ruby on-RailsRspecRuby on-Rails Problem Overview
I have a rails application which acts differently depending on what domain it's accessed at (for example www.myapp.com will invoke differently to user.myapp.com). In production use this all works fine but my test code always sees a hostname of "www.example.com".
Is there a clean way of having a test specify the hostname it's pretending to access?
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
-
Integration/Request Specs (inheriting from
ActionDispatch::IntegrationTest
):host! 'my.awesome.host'
See the docs, section 5.1 Helpers Available for Integration Tests.
alternatively, configure it globally for request specs at spec_helper.rb level:
RSpec.configure do |config|
config.before(:each, type: :request) do
host! 'my.awesome.host'
end
end
-
Controller Specs (inheriting from
ActionController::TestCase
)@request.host = 'my.awesome.host'
See the docs, section 4.4 Instance Variables Available.
-
Feature Specs (through Capybara)
Capybara.default_host = 'http://my.awesome.host' # Or to configure domain for route helpers: default_url_options[:host] = 'my.awesome.host'
From @AminAriana's answer
-
View Specs (inheriting from
ActionView::TestCase
)@request.host = 'my.awesome.host'
...or through RSpec:
controller.request.host = 'my.awesome.host'
See the rspec-rails
view spec docs.
Solution 2 - Ruby on-Rails
@request.host = 'user.myapp.com'
Solution 3 - Ruby on-Rails
Feature specs
In Feature specs, host! has been deprecated. Add these to your spec_helper.rb
:
# Configure Capybara expected host
Capybara.app_host = "http://test.domain"
# Configure actual routes host during test
before(:each) do
default_url_options[:host] = <myhost>
end
Request specs
In Request specs, keep using host! :
host! "test.domain"
Alternatively refactor it in before(:each)
blocks, or configure it globally for request specs at spec_helper.rb
level:
RSpec.configure do |config|
config.before(:each, type: :request) do
host! "test.domain"
end
end
Solution 4 - Ruby on-Rails
For Rspec Request specs, use before(:each) { host! 'example.com' }
See more at: https://relishapp.com/rspec/rspec-rails/v/3-6/docs/request-specs/request-spec https://github.com/rspec/rspec-rails/issues/1662#issuecomment-241201056
Solution 5 - Ruby on-Rails
I believe you can modify the HTTP_HOST
or SERVER_NAME
environment vars to change the request that goes to the router:
ENV['SERVER_NAME'] = "user.myapp.com"
See raw_host_with_port
in actionpack/lib/action_controller/request.rb.
Solution 6 - Ruby on-Rails
Another thing to remember is to make sure to use the correct session instance so that you can properly encapsulate the url helpers.
Integration tests provide you with a default session. You can call all session methods directly from your tests
test "should integrate well" do
https!
get users_path
assert_response :success
end
All these helpers are using the default session instance, which if not changed, goes to "www.example.com". As has been mentioned the host can be changed by doing host!("my.new.host")
If you create multiple sessions using the open_session method, you must ALWAYS use that instance to call the helper methods. This will properly encapsulate the request. Otherwise rails will call the default session instance which may use a different host:
test "should integrate well" do
sess = open_session
sess.host! "my.awesome.host"
sess.get users_url #=> WRONG! will use default session object to build url.
sess.get sess.users_url #=> Correctly invoking url writer from my custom session with new host.
sess.assert_response :success
end
If you intended to use the default session object, then you'll have to alter that host as well:
test "should integrate well" do
sess = open_session
sess.host! "my.awesome.host"
host! sess.host #=> Set default session host to my custom session host.
sess.get users_url
end
Solution 7 - Ruby on-Rails
@request.host = 'user.myapp.com'
is not right.
should use host!('user.myapp.com')
Solution 8 - Ruby on-Rails
I tried many variations of @request.host
, host!
, and post path, args, {'SERVER_NAME' => my_secret_domain}
without success, both as controller tests and feature tests. Very aggravating, as so many others reported success with those approaches.
The solution for me was:
request.headers["SERVER_NAME"] = my_secret_domain
post path, args
I'm running ruby 2.1.5p273, rspec 3.1.7 and Rails 4.2.0
Solution 9 - Ruby on-Rails
None of the ways suggested in other answers at the point worked for me. This worked:
Capybara.configure { |config| config.default_host = "my.domain.com" }
Solution 10 - Ruby on-Rails
Yet another answer:
request.host = "user.myapp.com"
I know it resembles the correct answer, but please bear with me. I don't like assignment operation in test just to set things up, I'd prefer an explicit stub. Interestingly, stubbing like this won't work:
allow(request).to receive(:host).and_return("user.myapp.com")
I personally prefer stubbing over assignment, that way I get 2 benefit, one is that it will be validated by rspec's verify double, second is that it is explicitly saying that is a stub, not part of the test excercise.