class Typhoeus::Expectation

#=> true
expected == actual
actual = Typhoeus.get(“www.example.com”)
Typhoeus.stub(“www.example.com”).and_return(expected)
expected = Typhoeus::Response.new
@example Stub a request and get specified response.
responses were returned one by one.
wether they match. If thats the case, the attached
to the request url and options in order to evaluate
an url and options, like a request. They were compared
of the stubbing mechanism. An expectation contains
This class represents an expectation. It is part

def all

Returns:
  • (Array) - The expectations.

Other tags:
    Example: Return expectations. -
def all
  @expectations ||= []
end

def and_return(response)

Returns:
  • (void) -

Other tags:
    Example: Add response. -
def and_return(response)
  responses << response
end

def clear

Other tags:
    Example: Clear expectations. -
def clear
  all.clear
end

def find_by(request)

Other tags:
    Api: - private

Returns:
  • (Expectation) - The matching expectation.

Other tags:
    Example: Find expectation. -
def find_by(request)
  all.find do |expectation|
    expectation.matches?(request)
  end
end

def initialize(url, options = {})

Other tags:
    Api: - private

Returns:
  • (Expectation) - The created expactation.

Other tags:
    Example: Create expactation. -
def initialize(url, options = {})
  @url = url
  @options = options
  @response_counter = 0
  @from = nil
end

def matches?(request)

Other tags:
    Api: - private

Returns:
  • (Boolean) - True when matches, else false.

Parameters:
  • request (Request) -- The request to check.

Other tags:
    Example: Check if request matches. -
def matches?(request)
  url_match?(request.url) &&
    (options ? options.all?{ |k,v| request.original_options[k] == v } : true)
end

def response

Other tags:
    Api: - private

Returns:
  • (Response) - The response.

Other tags:
    Example: Return response. -
def response
  response = responses.fetch(@response_counter, responses.last)
  @response_counter += 1
  response.mock = @from || true
  response
end

def responses

Other tags:
    Api: - private

Returns:
  • (Array) - The responses.

Other tags:
    Example: Return responses. -
def responses
  @responses ||= []
end

def stubbed_from(value)

Other tags:
    Api: - private

Returns:
  • (Expectation) - Returns self.

Parameters:
  • value (String) -- Value to set.

Other tags:
    Example: Mark expecation. -
def stubbed_from(value)
  @from = value
  self
end

def url_match?(request_url)

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

regexp were checked, nil is always true. Else false.
The url can be a string, regex or nil. String and
Check wether the url matches the request url.
def url_match?(request_url)
  case url
  when String
    url == request_url
  when Regexp
    !!request_url.match(url)
  when nil
    true
  else
    false
  end
end