1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
require 'test_helper'
class TestFakeWebOpenURI < Test::Unit::TestCase
def test_content_for_registered_uri
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
assert_equal 'test example content', FakeWeb.response_for(:get, 'http://mock/test_example.txt').body
end
def test_mock_open
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
assert_equal 'test example content', URI.open('http://mock/test_example.txt').read
end
def test_mock_open_with_string_as_registered_uri
FakeWeb.register_uri(:get, 'http://mock/test_string.txt', :body => 'foo')
assert_equal 'foo', URI.open('http://mock/test_string.txt').string
end
def _test_real_open
FakeWeb.allow_net_connect = true
setup_expectations_for_real_apple_hot_news_request
resp = URI.open('http://images.apple.com/main/rss/hotnews/hotnews.rss')
assert_equal "200", resp.status.first
body = resp.read
assert body.include?('Apple')
assert body.include?('News')
end
def test_mock_open_that_raises_exception
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => StandardError)
assert_raises(StandardError) do
URI.open('http://mock/raising_exception.txt')
end
end
def test_mock_open_that_raises_an_http_error
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError)
assert_raises(OpenURI::HTTPError) do
URI.open('http://mock/raising_exception.txt')
end
end
def test_mock_open_that_raises_an_http_error_with_a_specific_status
FakeWeb.register_uri(:get, 'http://mock/raising_exception.txt', :exception => OpenURI::HTTPError, :status => ['123', 'jodel'])
exception = assert_raises(OpenURI::HTTPError) do
URI.open('http://mock/raising_exception.txt')
end
assert_equal '123', exception.io.code
assert_equal 'jodel', exception.io.message
end
def test_mock_open_with_block
FakeWeb.register_uri(:get, 'http://mock/test_example.txt', :body => fixture_path("test_example.txt"))
body = URI.open('http://mock/test_example.txt') { |f| f.readlines }
assert_equal 'test example content', body.first
end
end
|