Skip to content

Commit

Permalink
Fix a bug where trailing slashes were considered significant for requ…
Browse files Browse the repository at this point in the history
…ests to the root of a domain
  • Loading branch information
chrisk committed Apr 12, 2009
1 parent c38e2ab commit c98f1d8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/fake_web/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ def response_for(method, uri, &block)
private

def normalize_uri(uri)
case uri
when URI then uri
else
uri = 'http://' + uri unless uri.match('^https?://')
parsed_uri = URI.parse(uri)
parsed_uri.query = sort_query_params(parsed_uri.query)
parsed_uri
end
normalized_uri =
case uri
when URI then uri
else
uri = 'http://' + uri unless uri.match('^https?://')
parsed_uri = URI.parse(uri)
parsed_uri.query = sort_query_params(parsed_uri.query)
parsed_uri
end
normalized_uri.normalize
end

def sort_query_params(query)
Expand Down
62 changes: 62 additions & 0 deletions test/test_trailing_slashes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require File.join(File.dirname(__FILE__), "test_helper")

class TestFakeWebTrailingSlashes < Test::Unit::TestCase

def setup
FakeWeb.clean_registry
@original_allow_net_connect = FakeWeb.allow_net_connect?
end

def teardown
FakeWeb.allow_net_connect = @old_allow_net_conncet
end

def test_registering_root_without_slash_and_ask_predicate_method_with_slash
FakeWeb.register_uri(:get, "http://www.google.com", :string => "Google")
assert FakeWeb.registered_uri?(:get, "http://www.google.com/")
end

def test_registering_root_without_slash_and_request
FakeWeb.register_uri(:get, "http://www.google.com", :string => "Google")
response = Net::HTTP.start("www.google.com") { |query| query.get('/') }
assert_equal "Google", response.body
end

def test_registering_root_with_slash_and_ask_predicate_method_without_slash
FakeWeb.register_uri(:get, "http://www.google.com/", :string => "Google")
assert FakeWeb.registered_uri?(:get, "http://www.google.com")
end

def test_registering_root_with_slash_and_request
FakeWeb.register_uri(:get, "http://www.google.com/", :string => "Google")
response = Net::HTTP.start("www.google.com") { |query| query.get('/') }
assert_equal "Google", response.body
end

def test_registering_path_without_slash_and_ask_predicate_method_with_slash
FakeWeb.register_uri(:get, "http://www.example.com/users", :string => "User list")
assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users/")
end

def test_registering_path_without_slash_and_request_with_slash
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:get, "http://www.example.com/users", :string => "User list")
assert_raise FakeWeb::NetConnectNotAllowedError do
response = Net::HTTP.start("www.example.com") { |query| query.get('/users/') }
end
end

def test_registering_path_with_slash_and_ask_predicate_method_without_slash
FakeWeb.register_uri(:get, "http://www.example.com/users/", :string => "User list")
assert !FakeWeb.registered_uri?(:get, "http://www.example.com/users")
end

def test_registering_path_with_slash_and_request_without_slash
FakeWeb.allow_net_connect = false
FakeWeb.register_uri(:get, "http://www.example.com/users/", :string => "User list")
assert_raise FakeWeb::NetConnectNotAllowedError do
response = Net::HTTP.start("www.example.com") { |query| query.get('/users') }
end
end

end

0 comments on commit c98f1d8

Please sign in to comment.