Class: WebMock::StubRequestSnippet

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

Instance Method Summary collapse

Constructor Details

#initialize(request_stub) ⇒ StubRequestSnippet

Returns a new instance of StubRequestSnippet.



5
6
7
# File 'lib/webmock/stub_request_snippet.rb', line 5

def initialize(request_stub)
  @request_stub = request_stub
end

Instance Method Details

#body_patternObject



9
10
11
# File 'lib/webmock/stub_request_snippet.rb', line 9

def body_pattern
  request_pattern.body_pattern
end

#to_s(with_response = true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/webmock/stub_request_snippet.rb', line 13

def to_s(with_response = true)
  request_pattern = @request_stub.request_pattern
  string = "stub_request(:#{request_pattern.method_pattern.to_s},".dup
  string << " \"#{request_pattern.uri_pattern.to_s}\")"

  with = "".dup

  if (request_pattern.body_pattern)
    with << "\n    body: #{request_pattern.body_pattern.to_s}"
  end

  if (request_pattern.headers_pattern)
    with << "," unless with.empty?

    with << "\n    headers: #{request_pattern.headers_pattern.pp_to_s}"
  end
  string << ".\n  with(#{with})" unless with.empty?
  if with_response
    if request_pattern.headers_pattern && request_pattern.headers_pattern.matches?({ 'Accept' => "application/json" })
      string << ".\n  to_return(status: 200, body: \"{}\", headers: {})"
    else
      string << ".\n  to_return(status: 200, body: \"\", headers: {})"
    end
  end
  string
end