Created
September 5, 2012 21:00
-
-
Save lmars/3644676 to your computer and use it in GitHub Desktop.
Sinatra based domain redirect app
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 RedirectTo | |
def initialize(expected, session) | |
@expected = expected | |
@session = session | |
end | |
def matches?(visit_proc) | |
instance_eval(&visit_proc) | |
@status = @session.status_code | |
@actual = @session.response_headers['Location'] | |
@status == 302 && @actual == @expected | |
end | |
def visiting(url) | |
@session.driver.get(url) | |
end | |
def failure_message_for_should | |
if @status != 302 | |
"Expected a 302 response but got a #{@status}" | |
else | |
"Expected a redirect to '#{@expected}' but got '#{@actual}'" | |
end | |
end | |
def failure_message_for_should_not | |
"Expected to not redirect to '#{@expected}' but did" | |
end | |
end | |
def redirect_to(url) | |
RedirectTo.new(url, page) | |
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
# Required gems: | |
# | |
# * sinatra | |
require 'sinatra/base' | |
class Redirector < Sinatra::Base | |
def self.domain(domain, &block) | |
@domain = domain | |
instance_eval(&block) | |
end | |
def self.redirect(source, location) | |
get source, :host_name => @domain do | |
redirect to(location) | |
end | |
end | |
domain 'domain-1.example.com' do | |
redirect '/path', 'http://example.com/path-for-domain-1' | |
end | |
domain 'domain-2.example.com' do | |
redirect '/path', 'http://example.com/path-for-domain-2' | |
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
# Required gems: | |
# | |
# * rspec | |
# * capybara | |
require 'redirector' | |
require 'capybara/rspec' | |
Capybara.app = Redirector | |
require 'redirect_to_matcher' | |
describe Redirector, :type => :request do | |
specify do | |
expect { visiting 'http://domain-1.example.com/path' }.to \ | |
redirect_to('http://example.com/path-for-domain-1') | |
end | |
specify do | |
expect { visiting 'http://domain-2.example.com/path' }.to \ | |
redirect_to('http://example.com/path-for-domain-2') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment