Class: Manticore::StubbedResponse

Inherits:
Response
  • Object
show all
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.

Examples:

Standard usage with Rspec


response = Manticore::StubbedResponse.stub(body: "body", code: 200).call
Manticore.should_receive(:get).with("http://www.google.com/").and_return(response)
response.body.should == "body"
response.code.should == 200

Since:

  • 0.3.0

Instance Attribute Summary

Attributes inherited from Response

#background, #callback_result, #called, #code, #context, #future, #headers, #request

Class Method Summary collapse

Instance Method Summary collapse

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.

Since:

  • 0.3.0


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

Parameters:

  • stubs (Hash) (defaults to: {})

    Hash of parameters to stub. See #stub.

Returns:

Since:

  • 0.3.0


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.

Since:

  • 0.3.0


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

#callObject

Used by Manticore::Client to invoke the request tied to this response

Since:

  • 0.3.0


50
51
52
53
# File 'lib/manticore/stubbed_response.rb', line 50

def call
  @called = true
  handleResponse @stubs
end

#cookiesObject

Returns the stubbed cookies of this response. This is the union of cookies from the `:cookies` option key and any `set-cookie` headers passed.

Since:

  • 0.3.0


77
78
79
80
# File 'lib/manticore/stubbed_response.rb', line 77

def cookies
  call_once
  @cookies
end

#final_urlString

Simulates the final URL of a redirected request. Returns the value of headers

Returns:

  • (String)

    The final URL

Since:

  • 0.3.0


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.

Parameters:

  • stubs (Hash)

    Parameters to stub out

Options Hash (stubs):

  • body (String)

    The body that should be returned

  • code (Integer)

    Response code to simulate

  • headers (Hash)

    Response headers as a hash

  • cookies (Array<Manticore::Cookie>)

Returns:

Since:

  • 0.3.0


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