Class: URI::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/rc_rest/uri_stub.rb

Overview

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 Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.responsesObject

Returns the value of attribute responses.



40
41
42
# File 'lib/rc_rest/uri_stub.rb', line 40

def responses
  @responses
end

.urisObject

Returns the value of attribute uris.



40
41
42
# File 'lib/rc_rest/uri_stub.rb', line 40

def uris
  @uris
end

Instance Method Details

#openObject



45
46
47
48
49
50
51
52
53
# File 'lib/rc_rest/uri_stub.rb', line 45

def open
  self.class.uris << self.to_s
  response = self.class.responses.shift
  if response.respond_to? :call then
    response.call
  else
    yield StringIO.new(response)
  end
end

#original_openObject



43
# File 'lib/rc_rest/uri_stub.rb', line 43

alias original_open open