Sha256: b459e4d42ae4f7a8bff3b5738eac807a9f8c37b96cab05f80bc5368ee6adea74

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

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 '<Error><Message>you broked it</Message></Error>'
    @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 '<Error><Message>you broked it</Message></Error>'
    @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 = '<Result>stuff</Result>'
    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 = '<Error><Message>you did the bad thing</Message></Error>'
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yahoo-1.0.0 test/test_yahoo.rb