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 an url and options, like a request. They were compared to the request url and options in order to evaluate wether they match. If thats the case, the attached responses were 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

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



74
75
76
77
78
79
# File 'lib/typhoeus/expectation.rb', line 74

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



20
21
22
# File 'lib/typhoeus/expectation.rb', line 20

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



26
27
28
# File 'lib/typhoeus/expectation.rb', line 26

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



23
24
25
# File 'lib/typhoeus/expectation.rb', line 23

def options
  @options
end

Class Method Details

.allArray<Typhoeus::Expectation>

Returns all expectations.

Examples:

Return expectations.

Typhoeus::Expectation.all

Returns:

Since:

  • 0.5.0



36
37
38
# File 'lib/typhoeus/expectation.rb', line 36

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



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

def clear
  all.clear
end

.find_by(request) ⇒ 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.

Returns expecation matching the provided request.

Examples:

Find expectation.

Typhoeus::Expectation.find_by(request)

Returns:

Since:

  • 0.5.0



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

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

Instance Method Details

#and_return(response) ⇒ 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



104
105
106
# File 'lib/typhoeus/expectation.rb', line 104

def and_return(response)
  responses << 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 wether 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



119
120
121
# File 'lib/typhoeus/expectation.rb', line 119

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

#responseResponse

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 were returned one by one.

Examples:

Return response.

expectation.response

Returns:

Since:

  • 0.5.0



145
146
147
148
149
150
# File 'lib/typhoeus/expectation.rb', line 145

def response
  response = responses.fetch(@response_counter, responses.last)
  @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



131
132
133
# File 'lib/typhoeus/expectation.rb', line 131

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, eg. webmock.

Examples:

Mark expectation.

expectation.from(:webmock)

Parameters:

  • value (String)

    Value to set.

Returns:

Since:

  • 0.5.0



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

def stubbed_from(value)
  @from = value
  self
end