Class: Philiprehberger::HttpMock::StubDefinition
- Inherits:
-
Object
- Object
- Philiprehberger::HttpMock::StubDefinition
- Defined in:
- lib/philiprehberger/http_mock/stub_definition.rb
Overview
Defines a request stub with matching criteria and response
Instance Attribute Summary collapse
-
#call_count ⇒ Integer
readonly
Number of times this stub has been matched.
-
#constraints ⇒ Hash
readonly
Additional matching criteria.
-
#method ⇒ Symbol
readonly
The HTTP method.
-
#url ⇒ String, Regexp
readonly
The URL pattern.
Instance Method Summary collapse
-
#called? ⇒ Boolean
Whether this stub has been called at least once.
-
#initialize(method, url) ⇒ StubDefinition
constructor
A new instance of StubDefinition.
-
#matches?(request) ⇒ Boolean
Check if a request matches this stub.
-
#response_for(request) ⇒ Response
Return the response for this stub, advancing sequence if applicable.
-
#to_raise(exception) ⇒ self
Configure the stub to raise an exception instead of returning a response.
-
#to_return(status: 200, body: '', headers: {}) {|Request| ... } ⇒ self
Set the response to return when this stub matches.
-
#to_return_in_sequence(responses) ⇒ self
Set a sequence of responses to cycle through.
-
#to_timeout ⇒ self
Configure the stub to raise a TimeoutError.
-
#with(body: nil, headers: nil) ⇒ self
Set additional matching constraints.
Constructor Details
#initialize(method, url) ⇒ StubDefinition
Returns a new instance of StubDefinition.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 21 def initialize(method, url) @method = method @url = url @constraints = {} @response = Response.new @response_sequence = nil @response_callback = nil @raise_exception = nil @call_count = 0 end |
Instance Attribute Details
#call_count ⇒ Integer (readonly)
Returns number of times this stub has been matched.
17 18 19 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 17 def call_count @call_count end |
#constraints ⇒ Hash (readonly)
Returns additional matching criteria.
14 15 16 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 14 def constraints @constraints end |
#method ⇒ Symbol (readonly)
Returns the HTTP method.
8 9 10 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 8 def method @method end |
#url ⇒ String, Regexp (readonly)
Returns the URL pattern.
11 12 13 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 11 def url @url end |
Instance Method Details
#called? ⇒ Boolean
Whether this stub has been called at least once
126 127 128 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 126 def called? @call_count.positive? end |
#matches?(request) ⇒ Boolean
Check if a request matches this stub
134 135 136 137 138 139 140 141 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 134 def matches?(request) return false unless request.method == @method return false unless url_matches?(request.url) return false unless body_matches?(request.body) return false unless headers_match?(request.headers) true end |
#response_for(request) ⇒ Response
Return the response for this stub, advancing sequence if applicable
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 103 def response_for(request) @call_count += 1 if @raise_exception raise @raise_exception.is_a?(Class) ? @raise_exception.new : @raise_exception elsif @response_callback result = @response_callback.call(request) Response.new( status: result.fetch(:status, 200), body: result.fetch(:body, ''), headers: result.fetch(:headers, {}) ) elsif @response_sequence index = [@call_count - 1, @response_sequence.length - 1].min @response_sequence[index] else @response end end |
#to_raise(exception) ⇒ self
Configure the stub to raise an exception instead of returning a response
67 68 69 70 71 72 73 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 67 def to_raise(exception) @raise_exception = exception @response = nil @response_sequence = nil @response_callback = nil self end |
#to_return(status: 200, body: '', headers: {}) {|Request| ... } ⇒ self
Set the response to return when this stub matches
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 50 def to_return(status: 200, body: '', headers: {}, &block) if block @response_callback = block @response = nil @response_sequence = nil else @response = Response.new(status: status, body: body, headers: headers) @response_callback = nil @response_sequence = nil end self end |
#to_return_in_sequence(responses) ⇒ self
Set a sequence of responses to cycle through
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 86 def to_return_in_sequence(responses) @response_sequence = responses.map do |resp| Response.new( status: resp.fetch(:status, 200), body: resp.fetch(:body, ''), headers: resp.fetch(:headers, {}) ) end @response = nil @response_callback = nil self end |
#to_timeout ⇒ self
Configure the stub to raise a TimeoutError
78 79 80 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 78 def to_timeout to_raise(TimeoutError.new("Request timed out: #{@method.upcase} #{@url}")) end |
#with(body: nil, headers: nil) ⇒ self
Set additional matching constraints
37 38 39 40 41 |
# File 'lib/philiprehberger/http_mock/stub_definition.rb', line 37 def with(body: nil, headers: nil) @constraints[:body] = body if body @constraints[:headers] = headers if headers self end |