Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/rack/no-www.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ def initialize(app)

def call(env)
if env['HTTP_HOST'] =~ STARTS_WITH_WWW
[301, no_www_request(env), ["Moved Permanently\n"]]
url = given_url(env)
new_url = remove_www(url)

puts "[Rack::NoWWW] #{url} -> #{new_url}"

[301, headers(new_url), ["Moved Permanently\n"]]
else
@app.call(env)
end
end

private
def no_www_request(env)
{ 'Location' => Rack::Request.new(env).url.sub(/www\./i, ''),
'Content-Type' => 'text/html' }
def headers(new_url)
{ 'Location' => new_url, 'Content-Type' => 'text/html' }
end

def given_url(env)
Rack::Request.new(env).url
end

def remove_www(url)
url.sub(/www\./i, '')
end

end
Expand Down
15 changes: 11 additions & 4 deletions spec/rack_no_www_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ def app

describe "when receiving a request with a 'www'" do

before(:each) do
request '/', {'HTTP_HOST' => 'www.example.org' }
end
subject{request '/', {'HTTP_HOST' => 'www.example.org' }}

it "should issue a 301 redirect" do
subject
last_response.status.should == 301
end

it "should redirect to the URL without the 'www'" do
subject
last_response.headers['Location'].should == "http://example.org/"
end

it "should have a text/html content type" do
subject
last_response.headers['Content-Type'].should == "text/html"
end

it "should have a body of 'Moved Permanently\\n'" do
subject
last_response.body.should == "Moved Permanently\n"
end


it "should write a log to STDOUT" do
out = capture_stdout{subject}
out.should eq "[Rack::NoWWW] http://www.example.org/ -> http://example.org/\n"
end

end

describe "when receiving a request without a 'www'" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require "bundler/setup"
require "rack/test"
require "rack/no-www"
require "#{File.dirname(__FILE__)}/support/capture_stdout"
10 changes: 10 additions & 0 deletions spec/support/capture_stdout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def capture_stdout#(&block)
original_stdout = $stdout
$stdout = fake = StringIO.new
begin
yield
ensure
$stdout = original_stdout
end
fake.string
end