Class: WebMock::RequestStub
- Inherits:
-
Object
- Object
- WebMock::RequestStub
- Defined in:
- lib/webmock/request_stub.rb
Instance Attribute Summary collapse
-
#request_pattern ⇒ Object
Returns the value of attribute request_pattern.
Class Method Summary collapse
Instance Method Summary collapse
- #has_responses? ⇒ Boolean
-
#initialize(method, uri) ⇒ RequestStub
constructor
A new instance of RequestStub.
- #matches?(request_signature) ⇒ Boolean
- #response ⇒ Object
- #then ⇒ Object
- #times(number) ⇒ Object
- #to_rack(app, options = {}) ⇒ Object
- #to_raise(*exceptions) ⇒ Object (also: #and_raise)
- #to_return(*response_hashes, &block) ⇒ Object (also: #and_return)
- #to_return_json(*response_hashes) ⇒ Object (also: #and_return_json)
- #to_s ⇒ Object
- #to_timeout ⇒ Object (also: #and_timeout)
- #with(params = {}, &block) ⇒ Object
Constructor Details
permalink #initialize(method, uri) ⇒ RequestStub
Returns a new instance of RequestStub.
8 9 10 11 12 |
# File 'lib/webmock/request_stub.rb', line 8 def initialize(method, uri) @request_pattern = RequestPattern.new(method, uri) @responses_sequences = [] self end |
Instance Attribute Details
permalink #request_pattern ⇒ Object
Returns the value of attribute request_pattern.
6 7 8 |
# File 'lib/webmock/request_stub.rb', line 6 def request_pattern @request_pattern end |
Class Method Details
permalink .from_request_signature(signature) ⇒ Object
[View source] [View on GitHub]
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/webmock/request_stub.rb', line 116 def self.from_request_signature(signature) stub = self.new(signature.method.to_sym, signature.uri.to_s) if signature.body.to_s != '' body = if signature.url_encoded? WebMock::Util::QueryMapper.query_to_values(signature.body, notation: Config.instance.query_values_notation) else signature.body end stub.with(body: body) end if (signature.headers && !signature.headers.empty?) stub.with(headers: signature.headers) end stub end |
Instance Method Details
permalink #has_responses? ⇒ Boolean
90 91 92 |
# File 'lib/webmock/request_stub.rb', line 90 def has_responses? !@responses_sequences.empty? end |
permalink #matches?(request_signature) ⇒ Boolean
108 109 110 |
# File 'lib/webmock/request_stub.rb', line 108 def matches?(request_signature) self.request_pattern.matches?(request_signature) end |
permalink #response ⇒ Object
[View source] [View on GitHub]
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/webmock/request_stub.rb', line 79 def response if @responses_sequences.empty? WebMock::Response.new elsif @responses_sequences.length > 1 @responses_sequences.shift if @responses_sequences.first.end? @responses_sequences.first.next_response else @responses_sequences[0].next_response end end |
permalink #then ⇒ Object
[View source] [View on GitHub]
94 95 96 |
# File 'lib/webmock/request_stub.rb', line 94 def then self end |
permalink #times(number) ⇒ Object
[View source] [View on GitHub]
98 99 100 101 102 103 104 105 106 |
# File 'lib/webmock/request_stub.rb', line 98 def times(number) raise "times(N) accepts integers >= 1 only" if !number.is_a?(Integer) || number < 1 if @responses_sequences.empty? raise "Invalid WebMock stub declaration." + " times(N) can be declared only after response declaration." end @responses_sequences.last.times_to_repeat += number-1 self end |
permalink #to_rack(app, options = {}) ⇒ Object
[View source] [View on GitHub]
61 62 63 |
# File 'lib/webmock/request_stub.rb', line 61 def to_rack(app, ={}) @responses_sequences << ResponsesSequence.new([RackResponse.new(app)]) end |
permalink #to_raise(*exceptions) ⇒ Object Also known as: and_raise
[View source] [View on GitHub]
65 66 67 68 69 70 |
# File 'lib/webmock/request_stub.rb', line 65 def to_raise(*exceptions) @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e| ResponseFactory.response_for(exception: e) }) self end |
permalink #to_return(*response_hashes, &block) ⇒ Object Also known as: and_return
[View source] [View on GitHub]
19 20 21 22 23 24 25 26 |
# File 'lib/webmock/request_stub.rb', line 19 def to_return(*response_hashes, &block) if block @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(block)]) else @responses_sequences << ResponsesSequence.new([*response_hashes].flatten.map {|r| ResponseFactory.response_for(r)}) end self end |
permalink #to_return_json(*response_hashes) ⇒ Object Also known as: and_return_json
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/webmock/request_stub.rb', line 29 def to_return_json(*response_hashes) raise ArgumentError, '#to_return_json does not support passing a block' if block_given? json_response_hashes = [*response_hashes].flatten.map do |resp_h| headers, body = resp_h.values_at(:headers, :body) json_body = if body.respond_to?(:call) ->(request_signature) { b = if body.respond_to?(:arity) && body.arity == 1 body.call(request_signature) else body.call end b = b.to_json unless b.is_a?(String) b } elsif !body.is_a?(String) body.to_json else body end resp_h.merge( headers: {content_type: 'application/json'}.merge(headers.to_h), body: json_body ) end to_return(json_response_hashes) end |
permalink #to_s ⇒ Object
[View source] [View on GitHub]
112 113 114 |
# File 'lib/webmock/request_stub.rb', line 112 def to_s self.request_pattern.to_s end |
permalink #to_timeout ⇒ Object Also known as: and_timeout
[View source] [View on GitHub]
73 74 75 76 |
# File 'lib/webmock/request_stub.rb', line 73 def to_timeout @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(should_timeout: true)]) self end |
permalink #with(params = {}, &block) ⇒ Object
[View source] [View on GitHub]
14 15 16 17 |
# File 'lib/webmock/request_stub.rb', line 14 def with(params = {}, &block) @request_pattern.with(params, &block) self end |