Class: WebMock::RequestStub

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/request_stub.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri) ⇒ RequestStub

Returns a new instance of RequestStub.



6
7
8
9
10
# File 'lib/webmock/request_stub.rb', line 6

def initialize(method, uri)
  @request_pattern = RequestPattern.new(method, uri)
  @responses_sequences = []
  self
end

Instance Attribute Details

#request_patternObject

Returns the value of attribute request_pattern.



4
5
6
# File 'lib/webmock/request_stub.rb', line 4

def request_pattern
  @request_pattern
end

Class Method Details

.from_request_signature(signature) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/webmock/request_stub.rb', line 82

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

#has_responses?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/webmock/request_stub.rb', line 56

def has_responses?
  !@responses_sequences.empty?
end

#matches?(request_signature) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/webmock/request_stub.rb', line 74

def matches?(request_signature)
  self.request_pattern.matches?(request_signature)
end

#responseObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/webmock/request_stub.rb', line 45

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

#thenObject



60
61
62
# File 'lib/webmock/request_stub.rb', line 60

def then
  self
end

#times(number) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/webmock/request_stub.rb', line 64

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 = {}) ⇒ Object



27
28
29
# File 'lib/webmock/request_stub.rb', line 27

def to_rack(app, options={})
  @responses_sequences << ResponsesSequence.new([RackResponse.new(app)])
end

#to_raise(*exceptions) ⇒ Object Also known as: and_raise



31
32
33
34
35
36
# File 'lib/webmock/request_stub.rb', line 31

def to_raise(*exceptions)
  @responses_sequences << ResponsesSequence.new([*exceptions].flatten.map {|e|
    ResponseFactory.response_for(exception: e)
  })
  self
end

#to_return(*response_hashes, &block) ⇒ Object Also known as: and_return



17
18
19
20
21
22
23
24
# 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

#to_sObject



78
79
80
# File 'lib/webmock/request_stub.rb', line 78

def to_s
  self.request_pattern.to_s
end

#to_timeoutObject Also known as: and_timeout



39
40
41
42
# File 'lib/webmock/request_stub.rb', line 39

def to_timeout
  @responses_sequences << ResponsesSequence.new([ResponseFactory.response_for(should_timeout: true)])
  self
end

#with(params = {}, &block) ⇒ Object



12
13
14
15
# File 'lib/webmock/request_stub.rb', line 12

def with(params = {}, &block)
  @request_pattern.with(params, &block)
  self
end