Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails testing default request host

I would like all my unit tests to use www.test.host instead of the default test.host.

I tried setting ENV['HTTP_HOST'] in config/environments/test.rb, but that didn't get it.

My purpose is to avoid a redirect in my controller test, the output of inspecting the response object in my test is:

#<ActionController::TestResponse:0x000001059ed378, ..., @header={"Location"=>"http://www.test.host", ... , @status=301, @body=["<html><body>You are being <a href=\"http://www.test.host\">redirected</a>.</body></html>"], ... , "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80",... , "HTTP_HOST"=>"test.host", "REMOTE_ADDR"=>"0.0.0.0" ...>>

If it makes a difference, I'm using Rails3 and RSPEC2

like image 847
SooDesuNe Avatar asked Oct 23 '11 21:10

SooDesuNe


1 Answers

You do it by setting request.host in your test. You can do it globally by adding this to your test_helper.rb (in Rails 3.1 anyway, not sure about previous versions, but I think it's similar):

class ActionController::TestCase
  setup do
    request.host = "www.test.host"
  end
end
like image 186
gtd Avatar answered Oct 28 '22 17:10

gtd