Setting a domain name (env['HTTP_HOST']) to use in Rack::Test

This may not be the best way to do this, but this is what I used at the top of my test file so that a helper method has a value for Rack’s env['HTTP_HOST']:

1
2
3
4
5
6
7
class FundastacheUserTest < Test::Unit::TestCase
  ...
  def env
    last_request.env['HTTP_HOST']="example.org"
    last_request.env
  end
  ...

The test tests to see that using the activation link (that a user gets in an email) activates the user. It looks something like this:

1
2
3
4
5
6
7
8
9
10
11
12
  def test_using_activation_link_should_activate_account
    create_user
    user=User.first
    path=activation_link(user)
    path=~/http\:\/\/example.org(.*)/
    get $1
    follow_redirect!
    assert last_response.ok?
    assert last_response.body.include?('Account activated!')
    user=User.first
    assert user.activated
  end

and the helper method looks something like this:

1
2
3
4
5
6
7
8
9
10
 
module FundAStache
  module Helpers    
    ...
    def activation_link(user)
      "http://#{env['HTTP_HOST']}/user/activate/#{user.activation_token}"
    end
    ...
  end
end
Posted: March 13th, 2010 | Author: jay | Filed under: Code | Tags: , , , , , | No Comments »

Here’s what my test_helper.rb file looks like…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require 'fundastache'
require 'test/unit'
require "rack/test"
require "rack/flash/test"
 
ENV['RACK_ENV'] = 'test'
 
# set test environment
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
 
DataMapper.setup(:default, "sqlite3::memory:") 
DataMapper.auto_migrate!
Posted: March 9th, 2010 | Author: jay | Filed under: Code | Tags: , , , | No Comments »