From c04fca716d833a77b3f4852be072591609eec099 Mon Sep 17 00:00:00 2001 From: John Joseph Bachir Date: Sat, 1 Jun 2013 12:21:53 -0500 Subject: [PATCH 1/2] explicitely invoke test subject in each example To facilitate capturing STDOUT in a subsequent changeset, and also I find this to be better style. --- spec/rack_no_www_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/spec/rack_no_www_spec.rb b/spec/rack_no_www_spec.rb index 56109af..d1ad774 100644 --- a/spec/rack_no_www_spec.rb +++ b/spec/rack_no_www_spec.rb @@ -11,23 +11,25 @@ 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 From 1ac16e34ba7d60a092245b5e0bb4faa460d852c0 Mon Sep 17 00:00:00 2001 From: John Joseph Bachir Date: Sun, 2 Jun 2013 00:29:41 -0500 Subject: [PATCH 2/2] log redirects to STDOUT --- lib/rack/no-www.rb | 20 ++++++++++++++++---- spec/rack_no_www_spec.rb | 7 ++++++- spec/spec_helper.rb | 1 + spec/support/capture_stdout.rb | 10 ++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 spec/support/capture_stdout.rb diff --git a/lib/rack/no-www.rb b/lib/rack/no-www.rb index e1dc081..376b96a 100644 --- a/lib/rack/no-www.rb +++ b/lib/rack/no-www.rb @@ -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 diff --git a/spec/rack_no_www_spec.rb b/spec/rack_no_www_spec.rb index d1ad774..c0b8816 100644 --- a/spec/rack_no_www_spec.rb +++ b/spec/rack_no_www_spec.rb @@ -32,7 +32,12 @@ def app 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1b4c0a4..0c03c06 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,3 +2,4 @@ require "bundler/setup" require "rack/test" require "rack/no-www" +require "#{File.dirname(__FILE__)}/support/capture_stdout" diff --git a/spec/support/capture_stdout.rb b/spec/support/capture_stdout.rb new file mode 100644 index 0000000..210537a --- /dev/null +++ b/spec/support/capture_stdout.rb @@ -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