class Typhoeus::Expectation

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.

@example 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

@example 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

@example 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

Attributes

base_url[R]

@api private

from[R]

@api private

options[R]

@api private

Public Class Methods

all() click to toggle source

Returns all expectations.

@example Return expectations.

Typhoeus::Expectation.all

@return [ Array<Typhoeus::Expectation> ] The expectations.

# File lib/typhoeus/expectation.rb, line 60
def all
  @expectations ||= []
end
clear() click to toggle source

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

@example Clear expectations.

Typhoeus::Expectation.clear
# File lib/typhoeus/expectation.rb, line 70
def clear
  all.clear
end
find_by(request) click to toggle source

@api private

# File lib/typhoeus/expectation.rb, line 93
def find_by(request)
  all.find do |expectation|
    expectation.matches?(request)
  end
end
new(base_url, options = {}) click to toggle source

Creates an expectation.

@example Create expectation.

Typhoeus::Expectation.new(base_url)

@return [ Expectation ] The created expectation.

@api private

# File lib/typhoeus/expectation.rb, line 108
def initialize(base_url, options = {})
  @base_url = base_url
  @options = options
  @response_counter = 0
  @from = nil
end
response_for(request) click to toggle source

Returns stubbed response matching the provided request.

@example Find response

Typhoeus::Expectation.response_for(request)

@return [ Typhoeus::Response ] The stubbed response from a

matching expectation, or nil if no matching expectation
is found.

@api private

# File lib/typhoeus/expectation.rb, line 85
def response_for(request)
  expectation = find_by(request)
  return nil if expectation.nil?

  expectation.response(request)
end

Public Instance Methods

and_return(response=nil, &block) click to toggle source

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

@example Add response.

expectation.and_return(response)

@return [ void ]

# 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) click to toggle source

Checks whether this expectation matches the provided request.

@example Check if request matches.

expectation.matches? request

@param [ Request ] request The request to check.

@return [ Boolean ] True when matches, else false.

@api private

# File lib/typhoeus/expectation.rb, line 154
def matches?(request)
  url_match?(request.base_url) && options_match?(request)
end
response(request) click to toggle source

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

@example Return response.

expectation.response

@return [ Response ] The response.

@api private

# 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
responses() click to toggle source

Return canned responses.

@example Return responses.

expectation.responses

@return [ Array<Typhoeus::Response> ] The responses.

@api private

# File lib/typhoeus/expectation.rb, line 166
def responses
  @responses ||= []
end
stubbed_from(value) click to toggle source

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

@example Mark expectation.

expectation.from(:webmock)

@param [ String ] value Value to set.

@return [ Expectation ] Returns self.

@api private

# File lib/typhoeus/expectation.rb, line 126
def stubbed_from(value)
  @from = value
  self
end

Private Instance Methods

options_match?(request) click to toggle source

Check whether the options matches the request options. I checks options and original options.

# File lib/typhoeus/expectation.rb, line 194
def options_match?(request)
  (options ? options.all?{ |k,v| request.original_options[k] == v || request.options[k] == v } : true)
end
url_match?(request_url) click to toggle source

Check whether the #base_url matches the request url. The #base_url can be a string, regex or nil. String and regexp are checked, nil is always true, else false.

Nil serves as a placeholder in case you want to match all urls.

# File lib/typhoeus/expectation.rb, line 204
def url_match?(request_url)
  case base_url
  when String
    base_url == request_url
  when Regexp
    base_url === request_url
  when nil
    true
  else
    false
  end
end