Sha256: 18c28b0adb11e3ce0ea9cbef7f42b2f2709d21c2a3dc0296cc3fbd7c55addd47

Contents?: true

Size: 1.01 KB

Versions: 6

Compression:

Stored size: 1.01 KB

Contents

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
    self.class.uris << self.to_s
    io = StringIO.new(self.class.responses.shift)
    OpenURI::Meta.init(io)
    yield io
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graticule-0.1.1 test/mocks/uri.rb
graticule-0.1.2 test/mocks/uri.rb
graticule-0.1.3 test/mocks/uri.rb
graticule-0.2.0 test/mocks/uri.rb
graticule-0.2.1 test/mocks/uri.rb
graticule-0.2.2 test/mocks/uri.rb