Class: Manticore::StubbedResponse
- Defined in:
- lib/manticore/stubbed_response.rb
Overview
StubbedResponse is a special subclass of Response that may be used to test your code that uses Manticore without actually having Manticore make requests.
Instance Attribute Summary
Attributes inherited from Response
#background, #callback_result, #called, #code, #context, #future, #headers, #request
Class Method Summary collapse
-
.stub(stubs = {}) ⇒ Manticore::StubbedResponse
Helper that instantiates a Response and stubs out its body, response code, etc.
Instance Method Summary collapse
-
#body(&block) ⇒ Object
(also: #read_body)
Returns the stubbed body of this response.
-
#call ⇒ Object
Used by Manticore::Client to invoke the request tied to this response.
-
#cookies ⇒ Object
Returns the stubbed cookies of this response.
-
#final_url ⇒ String
Simulates the final URL of a redirected request.
-
#initialize(client = nil, request = nil, context = nil) ⇒ StubbedResponse
constructor
A new instance of StubbedResponse.
-
#stub(stubs) ⇒ Manticore::StubbedResponse
Stub out a Manticore::RequestStub.
Methods inherited from Response
#[], #called?, #length, #message, #on_cancelled, #on_complete, #on_failure, #on_success, #times_retried
Constructor Details
#initialize(client = nil, request = nil, context = nil) ⇒ StubbedResponse
Returns a new instance of StubbedResponse.
22 23 24 |
# File 'lib/manticore/stubbed_response.rb', line 22 def initialize(client = nil, request = nil, context = nil) super end |
Class Method Details
.stub(stubs = {}) ⇒ Manticore::StubbedResponse
Helper that instantiates a Response and stubs out its body, response code, etc
18 19 20 |
# File 'lib/manticore/stubbed_response.rb', line 18 def self.stub(stubs = {}) new.stub(stubs) end |
Instance Method Details
#body(&block) ⇒ Object Also known as: read_body
Returns the stubbed body of this response.
64 65 66 67 68 69 70 71 |
# File 'lib/manticore/stubbed_response.rb', line 64 def body(&block) call_once if block_given? yield body else @body end end |
#call ⇒ Object
Used by Manticore::Client to invoke the request tied to this response
50 51 52 53 |
# File 'lib/manticore/stubbed_response.rb', line 50 def call @called = true handleResponse @stubs end |
#cookies ⇒ Object
Returns the stubbed cookies of this response. This is the union of cookies from the ‘:cookies` option key and any `set-cookie` headers passed.
77 78 79 80 |
# File 'lib/manticore/stubbed_response.rb', line 77 def call_once @cookies end |
#final_url ⇒ String
Simulates the final URL of a redirected request. Returns the value of headers
58 59 60 61 |
# File 'lib/manticore/stubbed_response.rb', line 58 def final_url call_once @headers["location"] || @request.getURI.to_string end |
#stub(stubs) ⇒ Manticore::StubbedResponse
Stub out a Manticore::RequestStub.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/manticore/stubbed_response.rb', line 34 def stub(stubs) if stubs.key? :cookies stubs[:cookies].keys.each { |key| stubs[:cookies][key] = Array(stubs[:cookies][key]) } end stubs[:code] ||= 200 stubs[:headers] ||= {} stubs[:headers] = Hash[*stubs[:headers].flat_map { |k, v| [k.downcase, v] }] stubs[:headers]["content-length"] ||= stubs[:body].length.to_s if stubs.key?(:body) @stubs = stubs self end |