Skip to content

Instantly share code, notes, and snippets.

@bhauman
Forked from trevorturk/no_www.rb
Created January 9, 2010 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhauman/273207 to your computer and use it in GitHub Desktop.
Save bhauman/273207 to your computer and use it in GitHub Desktop.
class NoWWW
STARTS_WITH_WWW = /^www\./i
def initialize(app)
@app = app
end
def call(env)
if env['HTTP_HOST'] =~ STARTS_WITH_WWW
[301, { 'Location' => Rack::Request.new(env).url.sub(/www\./i, '') }, ['Redirecting...']]
else
@app.call(env)
end
end
end
require 'rubygems'
require '../no_www'
require 'test/unit'
require 'rack/test'
class NoWWWTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Rack::Builder.new do
use NoWWW
run Proc.new {|env| [200, {"Content-Type" => "text/html"}, "the body"] }
end
end
def test_for_redirect
get '/', { }, { 'HTTP_HOST' => 'www.test.host'}
assert_equal 301, last_response.status
assert_equal last_response['Location'].to_s, 'http://test.host/'
end
def test_for_keeping_path
get '/wowza', { }, { 'HTTP_HOST' => 'www.test.host'}
assert_equal 301, last_response.status
assert_equal last_response['Location'].to_s, 'http://test.host/wowza'
end
def test_for_pass_through
get '/', { }, { 'HTTP_HOST' => 'test.host'}
assert_equal 200, last_response.status
get '/wowza', { }, { 'HTTP_HOST' => 'test.host'}
assert_equal 200, last_response.status
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment