Permalink
Browse files

Move a new method out of Net::HTTP, to prevent polluting the class wi…

…th more monkeypatching than necessary
  • Loading branch information...
1 parent 8d1c128 commit ca54030bb86637a23e49065a3585a28ecc42b370 @chrisk chrisk committed Apr 11, 2010
Showing with 25 additions and 24 deletions.
  1. +1 −17 lib/fake_web/ext/net_http.rb
  2. +17 −0 lib/fake_web/utility.rb
  3. +0 −7 test/test_fake_web.rb
  4. +7 −0 test/test_utility.rb
@@ -35,24 +35,8 @@ def socket_type_with_fakeweb
alias_method :socket_type, :socket_type_with_fakeweb
end
- def request_uri(request)
- protocol = use_ssl? ? "https" : "http"
-
- path = request.path
- path = URI.parse(request.path).request_uri if request.path =~ /^http/
-
- if request["authorization"] =~ /^Basic /
- userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
- userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
- else
- userinfo = ""
- end
-
- uri = "#{protocol}://#{userinfo}#{self.address}:#{self.port}#{path}"
- end
-
def request_with_fakeweb(request, body = nil, &block)
- uri = request_uri(request)
+ uri = FakeWeb::Utility.request_uri_as_string(self, request)
method = request.method.downcase.to_sym
if FakeWeb.registered_uri?(method, uri)
View
@@ -18,6 +18,23 @@ def self.strip_default_port_from_uri(uri)
end
end
+ # Returns a string with a normalized version of a Net::HTTP request's URI.
+ def self.request_uri_as_string(net_http, request)
+ protocol = net_http.use_ssl? ? "https" : "http"
+
+ path = request.path
+ path = URI.parse(request.path).request_uri if request.path =~ /^http/
+
+ if request["authorization"] =~ /^Basic /
+ userinfo = FakeWeb::Utility.decode_userinfo_from_header(request["authorization"])
+ userinfo = FakeWeb::Utility.encode_unsafe_chars_in_userinfo(userinfo) + "@"
+ else
+ userinfo = ""
+ end
+
+ uri = "#{protocol}://#{userinfo}#{net_http.address}:#{net_http.port}#{path}"
+ end
+
# Wrapper for URI escaping that switches between URI::Parser#escape and
# URI.escape for 1.9-compatibility
def self.uri_escape(*args)
View
@@ -323,13 +323,6 @@ def test_register_unimplemented_response
end
end
- def test_request_uri
- uri = URI.parse('http://www.example.com/index.html')
- http = Net::HTTP.new(uri.host, uri.port)
- request = Net::HTTP::Get.new(uri.path)
- assert_equal 'http://www.example.com:80/index.html', http.request_uri(request)
- end
-
def test_real_http_request
FakeWeb.allow_net_connect = true
setup_expectations_for_real_apple_hot_news_request
View
@@ -67,6 +67,13 @@ def test_strip_default_port_from_uri_does_not_modify_strings_that_do_not_start_w
assert_equal uri, FakeWeb::Utility.strip_default_port_from_uri(uri)
end
+ def test_request_uri_as_string
+ uri = URI.parse('http://www.example.com/index.html')
+ http = Net::HTTP.new(uri.host, uri.port)
+ request = Net::HTTP::Get.new(uri.path)
+ assert_equal 'http://www.example.com:80/index.html', FakeWeb::Utility.request_uri_as_string(http, request)
+ end
+
def test_uri_escape_delegates_to_uri_parser_when_available
parsing_object = URI.const_defined?(:Parser) ? URI::Parser.any_instance : URI
parsing_object.expects(:escape).with("string", /unsafe/).returns("escaped")

0 comments on commit ca54030

Please sign in to comment.