Class: Net::HTTP::Persistent

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

Overview

This stub overrides Net::HTTP::Persistent's request method to allow programs that use Net::HTTP::Persistent to be easily tested.

Usage

require 'rc_rest/net_http_persistent_stub'

class TestMyClass < MiniTest::Unit::TestCase

def setup
  Net::HTTP::Persistent.responses = []
  Net::HTTP::Persistent.uris = []

  @obj = MyClass.new
end

def test_my_method
  Net::HTTP::Persistent.responses << 'some text request would return'

  result = @obj.my_method

  assert_equal :something_meaninfgul, result

  assert_equal true, Net::HTTP::Persistent.responses.empty?
  assert_equal 1, Net::HTTP::Persistent.uris.length
  assert_equal 'http://example.com/path', Net::HTTP::Persistent.uris.first
end

end

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.responsesObject

List of responses #request should return.

If a String is given a Net::HTTPOK is created

If a proc is given, it is called. The proc should raise an exception or return a Net::HTTPResponse subclass.

Unlike URI::HTTP from rc-rest 3.x and earlier you must return a Net::HTTPResponse subclass.



49
50
51
# File 'lib/rc_rest/net_http_persistent_stub.rb', line 49

def responses
  @responses
end

.urisObject

URIs recorded



54
55
56
# File 'lib/rc_rest/net_http_persistent_stub.rb', line 54

def uris
  @uris
end

Instance Method Details

#original_requestObject



58
# File 'lib/rc_rest/net_http_persistent_stub.rb', line 58

alias original_request request

#request(uri) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rc_rest/net_http_persistent_stub.rb', line 60

def request uri
  self.class.uris << uri.to_s
  response = self.class.responses.shift

  response = response.call if response.respond_to? :call

  return response if Net::HTTPResponse === response

  r = Net::HTTPOK.new '1.0', 200, 'OK'
  r.body = response
  r
end