Sha256: 5f3466360c16799664f8b0ee7f65dda7cee30fea807cb7096a3413b89237e6c2

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# encoding: UTF-8
require 'uri/http'
require 'open-uri'

##
# This stub overrides OpenURI's open method to allow programs that use OpenURI
# to be easily tested.
#
# == Usage
#
#   require 'rc_rest/uri_stub'
#
#   class TestMyClass < Test::Unit::TestCase
#
#     def setup
#       URI::HTTP.responses = []
#       URI::HTTP.uris = []
#
#       @obj = MyClass.new
#     end
#
#     def test_my_method
#       URI::HTTP.responses << 'some text open would ordinarily return'
#
#       result = @obj.my_method
#
#       assert_equal :something_meaninfgul, result
#
#       assert_equal true, URI::HTTP.responses.empty?
#       assert_equal 1, URI::HTTP.uris.length
#       assert_equal 'http://example.com/path', URI::HTTP.uris.first
#     end
#
#  end

class URI::HTTP # :nodoc:

  class << self
    attr_accessor :responses, :uris
  end

  alias original_open open

  def open(*args)
    self.class.uris << self.to_s
    io = StringIO.new(self.class.responses.shift)
    OpenURI::Meta.init(io)
    yield io if block_given?
    io
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graticule-2.7.2 test/mocks/uri.rb
graticule-2.7.1 test/mocks/uri.rb
graticule-2.7.0 test/mocks/uri.rb
graticule-2.6.0 test/mocks/uri.rb
graticule-2.5.0 test/mocks/uri.rb
graticule-2.4.0 test/mocks/uri.rb