require 'test/unit'
require 'test/uri_stub'
require 'yahoo'
class Yahoo::Test < Yahoo
def initialize(*args)
@host = 'api.test.yahoo.com'
@service_name = 'TestService'
@version = 'Vtest'
@method = 'test'
super
end
def test
get :test_param => 5
end
def parse_response(xml)
return xml
end
end
class TestYahoo < Test::Unit::TestCase
def setup
URI::HTTP.responses = []
URI::HTTP.uris = []
@t = Yahoo::Test.new 'APP_ID'
end
def test_check_error_IO
io = StringIO.new 'you broked it'
@t.check_error io
rescue Yahoo::Error => e
assert_equal 'you broked it', e.message
else
flunk 'expected an error'
end
def test_check_error_REXML__Document
xml = REXML::Document.new 'you broked it'
@t.check_error xml
rescue Yahoo::Error => e
assert_equal 'you broked it', e.message
else
flunk 'expected an error'
end
def test_get
xml = 'stuff'
URI::HTTP.responses << xml
result = @t.test
assert_equal xml, result.to_s
end
def test_get_error
def @t.make_url(*args) # HACK extend uri_stub with error raising ability
u = Object.new
def u.open
xml = 'you did the bad thing'
raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
end
return u
end
assert_raise Yahoo::Error do @t.test end
end
def test_initialize
assert_equal 'http://api.test.yahoo.com/TestService/Vtest/test',
@t.instance_variable_get(:@url).to_s
end
def test_make_url
url = @t.make_url :test_param_1 => 'test test',
:test_param_2 => 'tset tset'
expected = 'http://api.test.yahoo.com/TestService/Vtest/test?appid=APP_ID&output=xml&test_param_1=test%20test&test_param_2=tset%20tset'
assert_equal expected, url.to_s
end
end