class WebMock::RequestStub
Attributes
request_pattern[RW]
Public Class Methods
from_request_signature(signature)
click to toggle source
# File lib/webmock/request_stub.rb, line 97 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
new(method, uri)
click to toggle source
# File lib/webmock/request_stub.rb, line 6 def initialize(method, uri) @request_pattern = RequestPattern.new(method, uri) @responses_sequences = [] self end
Public Instance Methods
has_responses?()
click to toggle source
# File lib/webmock/request_stub.rb, line 71 def has_responses? !@responses_sequences.empty? end
matches?(request_signature)
click to toggle source
# File lib/webmock/request_stub.rb, line 89 def matches?(request_signature) self.request_pattern.matches?(request_signature) end
response()
click to toggle source
# File lib/webmock/request_stub.rb, line 60 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
then()
click to toggle source
# File lib/webmock/request_stub.rb, line 75 def then self end
times(number)
click to toggle source
# File lib/webmock/request_stub.rb, line 79 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
to_rack(app, options={})
click to toggle source
# File lib/webmock/request_stub.rb, line 42 def to_rack(app, options={}) @responses_sequences << ResponsesSequence.new([RackResponse.new(app)]) end
to_raise(*exceptions)
click to toggle source
# File lib/webmock/request_stub.rb, line 46 def to_raise(*exceptions) @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e| ResponseFactory.response_for(exception: e) }) self end
Also aliased as: and_raise
to_return(*response_hashes, &block)
click to toggle source
# File lib/webmock/request_stub.rb, line 17 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
Also aliased as: and_return
to_return_json(*response_hashes)
click to toggle source
# File lib/webmock/request_stub.rb, line 27 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) resp_h.merge( headers: {content_type: 'application/json'}.merge(headers.to_h), body: body.is_a?(Hash) ? body.to_json : body ) end to_return(json_response_hashes) end
Also aliased as: and_return_json
to_s()
click to toggle source
# File lib/webmock/request_stub.rb, line 93 def to_s self.request_pattern.to_s end
to_timeout()
click to toggle source
# File lib/webmock/request_stub.rb, line 54 def to_timeout @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(should_timeout: true)]) self end
Also aliased as: and_timeout
with(params = {}, &block)
click to toggle source
# File lib/webmock/request_stub.rb, line 12 def with(params = {}, &block) @request_pattern.with(params, &block) self end