Skip to content

Commit

Permalink
slightly more flexible redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
atmos committed Jul 28, 2009
1 parent a5e55b8 commit 98918b4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 21 deletions.
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -22,7 +22,12 @@ testing

Just run rake...

rack-redirect
just using rack-redirect's middleware
- forwards on requests from http://example.org to the next app (PENDING: TODO)
- redirects from http://alpha.example.com to http://example.org/
- redirects from http://alpha.example.org/nate to http://example.org/nate
- redirects from http://alpha.example.org/nate?trip_id=42 to http://example.org/nate?trip_id=42
using rack-redirect's middleware with a prefix of 'www.'
- redirects from http://example.org to http://www.example.org/
- redirects from http://alpha.example.com to http://www.example.org/
- redirects from http://example.org/nate to http://www.example.org/nate
Expand Down
9 changes: 8 additions & 1 deletion lib/app.rb
Expand Up @@ -2,11 +2,18 @@ module EY
module Solo
module Rack
class Redirect
attr_accessor :prefix
def initialize(app, &block)
@app = app
@prefix = nil
yield self if block_given?
end

def call(env)
parts = env['SERVER_NAME'].split('.')
p1, p2 = parts.pop, parts.pop

destination = "#{env['rack.url_scheme']}://www.#{p2}.#{p1}"
destination = "#{env['rack.url_scheme']}://#{@prefix}#{p2}.#{p1}"
destination << "#{env['PATH_INFO']}"
destination << "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].empty?

Expand Down
65 changes: 51 additions & 14 deletions spec/rack-redirect_spec.rb
@@ -1,23 +1,60 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "rack-redirect" do
it 'redirects from http://example.org to http://www.example.org/' do
get '/'
last_response['Location'].should eql('http://www.example.org/')
end
describe "with a value of 'www.'" do
def app
@app ||= Rack::Builder.new do
use EY::Solo::Rack::Redirect do |app|
app.prefix = 'www.'
end
run lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['Hello there, gorgeous'] ] }
end
end
it 'redirects from http://example.org to http://www.example.org/' do
get '/'
last_response['Location'].should eql('http://www.example.org/')
end

it 'redirects from http://alpha.example.com to http://www.example.org/' do
get '/', {}, {'SERVER_NAME' => 'alpha.example.org'}
last_response['Location'].should eql('http://www.example.org/')
end
it 'redirects from http://alpha.example.com to http://www.example.org/' do
get '/', {}, {'SERVER_NAME' => 'alpha.example.org'}
last_response['Location'].should eql('http://www.example.org/')
end

it 'redirects from http://example.org/nate to http://www.example.org/nate' do
get '/nate'
last_response['Location'].should eql('http://www.example.org/nate')
end

it 'redirects from http://example.org/nate to http://www.example.org/nate' do
get '/nate'
last_response['Location'].should eql('http://www.example.org/nate')
it 'redirects from http://example.org/nate?trip_id=42 to http://www.example.org/nate?trip_id=42' do
get '/nate', :trip_id => 42
last_response['Location'].should eql('http://www.example.org/nate?trip_id=42')
end
end
describe "without specifying a prefix" do
def app
@app ||= Rack::Builder.new do
use EY::Solo::Rack::Redirect
run lambda { |env| [200, { 'Content-Type' => 'text/plain' }, ['Hello there, gorgeous'] ] }
end
end
it 'forwards on requests from http://example.org to the next app' do
pending
last_response.body.should eql("Hello there, gorgeous")
end

it 'redirects from http://alpha.example.com to http://example.org/' do
get '/', {}, {'SERVER_NAME' => 'alpha.example.org'}
last_response['Location'].should eql('http://example.org/')
end

it 'redirects from http://alpha.example.org/nate to http://example.org/nate' do
get '/nate', {}, {'SERVER_NAME' => 'alpha.example.org'}
last_response['Location'].should eql('http://example.org/nate')
end

it 'redirects from http://example.org/nate?trip_id=42 to http://www.example.org/nate?trip_id=42' do
get '/nate', :trip_id => 42
last_response['Location'].should eql('http://www.example.org/nate?trip_id=42')
it 'redirects from http://alpha.example.org/nate?trip_id=42 to http://example.org/nate?trip_id=42' do
get '/nate', {:trip_id => 42}, {'SERVER_NAME' => 'alpha.example.org'}
last_response['Location'].should eql('http://example.org/nate?trip_id=42')
end
end
end
5 changes: 0 additions & 5 deletions spec/spec_helper.rb
Expand Up @@ -5,10 +5,5 @@
require 'rack/test'

Spec::Runner.configure do |config|
def app
@app ||= Rack::Builder.new do
run EY::Solo::Rack::Redirect.new
end
end
config.include(Rack::Test::Methods)
end

0 comments on commit 98918b4

Please sign in to comment.