-
-
Save bhauman/273207 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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