Class: Typhoeus::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/typhoeus/expectation.rb

Overview

This class represents an expectation. It is part of the stubbing mechanism. An expectation contains a url and options, like a request. They are compared to the request url and options in order to evaluate whether they match. If that’s the case, the attached responses are returned one by one.

Examples:

Stub a request and get specified response.

expected = Typhoeus::Response.new
Typhoeus.stub("www.example.com").and_return(expected)

actual = Typhoeus.get("www.example.com")
expected == actual
#=> true

Stub a request and get a lazily-constructed response containing data from actual widgets that exist in the system when the stubbed request is made.

Typhoeus.stub("www.example.com/widgets") do
  actual_widgets = Widget.all
  Typhoeus::Response.new(
    :body => actual_widgets.inject([]) do |ids, widget|
      ids << widget.id
    end.join(",")
  )
end

Stub a request and get a lazily-constructed response in the format requested.

Typhoeus.stub("www.example.com") do |request|
  accept = (request.options[:headers]||{})['Accept'] || "application/json"
  format = accept.split(",").first
  body_obj = { 'things' => [ { 'id' => 'foo' } ] }

  Typhoeus::Response.new(
    :headers => {
      'Content-Type' => format
    },
    :body => SERIALIZERS[format].serialize(body_obj)
  )
end

Since:

  • 0.5.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, options = {}) ⇒ Expectation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates an expectation.

Examples:

Create expectation.

Typhoeus::Expectation.new(base_url)

Since:

  • 0.5.0



108
109
110
111
112
113
# File 'lib/typhoeus/expectation.rb', line 108

def initialize(base_url, options = {})
  @base_url = base_url
  @options = options
  @response_counter = 0
  @from = nil
end

Instance Attribute Details

#base_urlObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



44
45
46
# File 'lib/typhoeus/expectation.rb', line 44

def base_url
  @base_url
end

#fromObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



50
51
52
# File 'lib/typhoeus/expectation.rb', line 50

def from
  @from
end

#optionsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



47
48
49
# File 'lib/typhoeus/expectation.rb', line 47

def options
  @options
end

Class Method Details

.allArray<Typhoeus::Expectation>

Returns all expectations.

Examples:

Return expectations.

Typhoeus::Expectation.all

Returns:

Since:

  • 0.5.0



60
61
62
# File 'lib/typhoeus/expectation.rb', line 60

def all
  @expectations ||= []
end

.clearObject

Clears expectations. This is handy while testing, and you want to make sure that you don’t get canned responses.

Examples:

Clear expectations.

Typhoeus::Expectation.clear

Since:

  • 0.5.0



70
71
72
# File 'lib/typhoeus/expectation.rb', line 70

def clear
  all.clear
end

.find_by(request) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0



93
94
95
96
97
# File 'lib/typhoeus/expectation.rb', line 93

def find_by(request)
  all.find do |expectation|
    expectation.matches?(request)
  end
end

.response_for(request) ⇒ Typhoeus::Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns stubbed response matching the provided request.

Examples:

Find response

Typhoeus::Expectation.response_for(request)

Returns:

  • (Typhoeus::Response)

    The stubbed response from a matching expectation, or nil if no matching expectation is found.

Since:

  • 0.5.0



85
86
87
88
89
90
# File 'lib/typhoeus/expectation.rb', line 85

def response_for(request)
  expectation = find_by(request)
  return nil if expectation.nil?

  expectation.response(request)
end

Instance Method Details

#and_return(response = nil, &block) ⇒ void

This method returns an undefined value.

Specify what should be returned, when this expectation is hit.

Examples:

Add response.

expectation.and_return(response)

Since:

  • 0.5.0



138
139
140
141
# File 'lib/typhoeus/expectation.rb', line 138

def and_return(response=nil, &block)
  new_response = (response.nil? ? block : response)
  responses.push(*new_response)
end

#matches?(request) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks whether this expectation matches the provided request.

Examples:

Check if request matches.

expectation.matches? request

Parameters:

  • request (Request)

    The request to check.

Returns:

  • (Boolean)

    True when matches, else false.

Since:

  • 0.5.0



154
155
156
# File 'lib/typhoeus/expectation.rb', line 154

def matches?(request)
  url_match?(request.base_url) && options_match?(request)
end

#response(request) ⇒ Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the response. When there are multiple responses, they are returned one by one.

Examples:

Return response.

expectation.response

Returns:

Since:

  • 0.5.0



180
181
182
183
184
185
186
187
188
# File 'lib/typhoeus/expectation.rb', line 180

def response(request)
  response = responses.fetch(@response_counter, responses.last)
  if response.respond_to?(:call)
    response = response.call(request)
  end
  @response_counter += 1
  response.mock = @from || true
  response
end

#responsesArray<Typhoeus::Response>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return canned responses.

Examples:

Return responses.

expectation.responses

Returns:

Since:

  • 0.5.0



166
167
168
# File 'lib/typhoeus/expectation.rb', line 166

def responses
  @responses ||= []
end

#stubbed_from(value) ⇒ Expectation

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set from value to mark an expectaion. Useful for other libraries, e.g. WebMock.

Examples:

Mark expectation.

expectation.from(:webmock)

Parameters:

  • value (String)

    Value to set.

Returns:

Since:

  • 0.5.0



126
127
128
129
# File 'lib/typhoeus/expectation.rb', line 126

def stubbed_from(value)
  @from = value
  self
end