File: request_signature_snippet.rb

package info (click to toggle)
ruby-webmock 3.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,859; makefile: 6
file content (63 lines) | stat: -rw-r--r-- 1,647 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

require "pp"

module WebMock
  class RequestSignatureSnippet

    attr_reader :request_signature, :request_stub

    def initialize(request_signature)
      @request_signature = request_signature
      @request_stub = RequestStub.from_request_signature(request_signature)
    end

    def stubbing_instructions
      return unless WebMock.show_stubbing_instructions?

      "You can stub this request with the following snippet:\n\n" +
        WebMock::StubRequestSnippet.new(request_stub).to_s
    end

    def request_stubs
      return if WebMock::StubRegistry.instance.request_stubs.empty?

      text = "registered request stubs:\n".dup
      WebMock::StubRegistry.instance.request_stubs.each do |stub|
        text << "\n#{WebMock::StubRequestSnippet.new(stub).to_s(false)}"
        add_body_diff(stub, text) if WebMock.show_body_diff?
      end
      text
    end

    private

    def add_body_diff(stub, text)
      body_diff_str = signature_stub_body_diff(stub)
      text << "\n\n#{body_diff_str}" unless body_diff_str.empty?
    end

    def signature_stub_body_diff(stub)
      diff = RequestBodyDiff.new(request_signature, stub).body_diff
      diff.empty? ? "" : "Body diff:\n #{pretty_print_to_string(diff)}"
    end

    def request_params
      @request_params ||=
        if request_signature.json_headers?
          JSON.parse(request_signature.body)
        else
          ""
        end
    end

    def pretty_print_to_string(string_to_print)
      StringIO.open("".dup) do |stream|
        PP.pp(string_to_print, stream)
        stream.rewind
        stream.read
      end
    end

  end
end