Skip to content

Instantly share code, notes, and snippets.

@lmars
Created September 5, 2012 21:00
Show Gist options
  • Save lmars/3644676 to your computer and use it in GitHub Desktop.
Save lmars/3644676 to your computer and use it in GitHub Desktop.
Sinatra based domain redirect app
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
# 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
# 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