test/utils_test.rb in faraday-0.9.0.rc5 vs test/utils_test.rb in faraday-0.9.0.rc6
- old
+ new
@@ -3,35 +3,56 @@
class TestUtils < Faraday::TestCase
def setup
@url = "http://example.com/abc"
end
- def teardown
- Faraday::Utils.default_uri_parser = nil
+ # emulates ActiveSupport::SafeBuffer#gsub
+ FakeSafeBuffer = Struct.new(:string) do
+ def to_s() self end
+ def gsub(regex)
+ string.gsub(regex) {
+ match, = $&, '' =~ /a/
+ yield match
+ }
+ end
end
+ def test_escaping_safe_buffer
+ str = FakeSafeBuffer.new('$32,000.00')
+ assert_equal '%2432%2C000.00', Faraday::Utils.escape(str)
+ end
+
def test_parses_with_default
- assert_equal %(#<Method: Kernel.URI>), Faraday::Utils.default_uri_parser.to_s
- uri = normalize(@url)
- assert_equal 'example.com', uri.host
+ with_default_uri_parser(nil) do
+ uri = normalize(@url)
+ assert_equal 'example.com', uri.host
+ end
end
def test_parses_with_URI
- Faraday::Utils.default_uri_parser = ::URI
- assert_equal %(#<Method: URI.parse>), Faraday::Utils.default_uri_parser.to_s
- uri = normalize(@url)
- assert_equal 'example.com', uri.host
+ with_default_uri_parser(::URI) do
+ uri = normalize(@url)
+ assert_equal 'example.com', uri.host
+ end
end
def test_parses_with_block
- Faraday::Utils.default_uri_parser = lambda do |uri|
- "booya#{"!" * uri.size}"
+ with_default_uri_parser(lambda {|u| "booya#{"!" * u.size}" }) do
+ assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
end
-
- assert_equal 'booya!!!!!!!!!!!!!!!!!!!!!!', normalize(@url)
end
def normalize(url)
Faraday::Utils::URI(url)
+ end
+
+ def with_default_uri_parser(parser)
+ old_parser = Faraday::Utils.default_uri_parser
+ begin
+ Faraday::Utils.default_uri_parser = parser
+ yield
+ ensure
+ Faraday::Utils.default_uri_parser = old_parser
+ end
end
end