Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@scottwater
Created February 17, 2010 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottwater/306645 to your computer and use it in GitHub Desktop.
Save scottwater/306645 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'consistenturls'
#plugin
validate_url_requests
get '/' do
"Hello World"
end
get %r{(.+)} do |catch_all|
"I am the catch all #{catch_all}<br />#{request.url}"
end
not_found do
"The path #{request.url} does not exist"
end
require 'demo_app'
require 'test/unit'
require 'rack/test'
set :environment, :test
class ConsistentUrlsTests < Test::Unit::TestCase
include Rack::Test::Methods
def app
@app || Sinatra::Application
end
def test_urls_with_trailing_slashes_will_be_redirected
execute_url_test('/some-random-url/', '/some-random-url')
execute_url_test('/some-random-url/second-path/', '/some-random-url/second-path')
end
def test_urls_with_trailing_slashes_and_querystrings_will_be_redirected
execute_url_test('/some-random-url/?abc=123', '/some-random-url?abc=123')
execute_url_test('/some-random-URL/?abc=123&z=abc', '/some-random-url?abc=123&z=abc')
end
def test_case_sensitive_url_will_be_redirected
execute_url_test('/CAPS-LOCK-ROCKS', '/caps-lock-rocks')
end
def test_case_sensitive_url_with_trailing_slash_will_be_redirected
execute_url_test('/CAPS-LOCK-ROCKS/', '/caps-lock-rocks')
end
def test_case_sensitive_url_with_query_string_will_be_redirected
execute_url_test('/CAPS-LOCK-ROCKS?abc=123&def=HIJ', '/caps-lock-rocks?abc=123&def=HIJ')
execute_url_test('/CAPS-LOCK-ROCKS/ROCKS/It/ROCKs/?abc=123&def=HIJ', '/caps-lock-rocks/rocks/it/rocks?abc=123&def=HIJ')
end
def execute_url_test(url_to_test, expected_redirect)
get url_to_test
follow_redirect!
assert_equal last_request.url, 'http://example.org' + expected_redirect
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment