Sha256: a0b3ceff564b127fa04448ec885e9dc5a80f0620883b9bd601e600e3879e4646

Contents?: true

Size: 1.52 KB

Versions: 13

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

require "hashdiff"
require "json"

module WebMock
  class RequestBodyDiff

    def initialize(request_signature, request_stub)
      @request_signature = request_signature
      @request_stub      = request_stub
    end

    def body_diff
      return {} unless request_signature_diffable? && request_stub_diffable?

      Hashdiff.diff(request_signature_body_hash, request_stub_body_hash)
    end

    attr_reader :request_signature, :request_stub
    private :request_signature, :request_stub

    private

    def request_signature_diffable?
      request_signature.json_headers? && request_signature_parseable_json?
    end

    def request_stub_diffable?
      request_stub_body.is_a?(Hash) || request_stub_parseable_json?
    end

    def request_signature_body_hash
      JSON.parse(request_signature.body)
    end

    def request_stub_body_hash
      return request_stub_body if request_stub_body.is_a?(Hash)

      JSON.parse(request_stub_body)
    end

    def request_stub_body
      request_stub.request_pattern &&
        request_stub.request_pattern.body_pattern &&
        request_stub.request_pattern.body_pattern.pattern
    end

    def request_signature_parseable_json?
      parseable_json?(request_signature.body)
    end

    def request_stub_parseable_json?
      parseable_json?(request_stub_body)
    end

    def parseable_json?(body_pattern)
      return false unless body_pattern.is_a?(String)

      JSON.parse(body_pattern)
      true
    rescue JSON::ParserError
      false
    end
  end
end

Version data entries

13 entries across 13 versions & 4 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/request_body_diff.rb
webmock-3.24.0 lib/webmock/request_body_diff.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/webmock-3.23.1/lib/webmock/request_body_diff.rb
webmock-3.23.0 lib/webmock/request_body_diff.rb
webmock-3.22.0 lib/webmock/request_body_diff.rb
webmock-3.21.2 lib/webmock/request_body_diff.rb
webmock-3.21.1 lib/webmock/request_body_diff.rb
webmock-3.21.0 lib/webmock/request_body_diff.rb
webmock-3.20.0 lib/webmock/request_body_diff.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/request_body_diff.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/webmock-3.19.1/lib/webmock/request_body_diff.rb
webmock-3.19.1 lib/webmock/request_body_diff.rb
webmock-3.19.0 lib/webmock/request_body_diff.rb