Sha256: dea34bed199a5b95e5c0ec1f39bce88b0dcc1f55a8a0d1fcbb3b2ebdec34e212

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

require 'json'
require 'pact/reification'
require 'pact/shared/null_expectation'

module Pact
  module Provider
    module Request
      class Replayable

        def initialize expected_request
          @expected_request = expected_request
        end

        def method
          expected_request.method
        end

        def path
          expected_request.full_path
        end

        def body
          case expected_request.body
          when String then expected_request.body
          when NullExpectation then ''
          else
            reified_body
          end
        end

        def headers
          request_headers = {}
          return request_headers if expected_request.headers.is_a?(Pact::NullExpectation)
          expected_request.headers.each do |key, value|
            key = key.upcase
            if key.match(/CONTENT.TYPE/)
              request_headers['CONTENT_TYPE'] = value
            else
              request_headers[formatted_request_header_key(key)] = value
            end
          end
          request_headers
        end

        private
        attr_reader :expected_request

        def reified_body
          rb = Pact::Reification.from_term(expected_request.body)
          if rb.is_a?(String)
            rb
          else
            JSON.dump(rb)
          end
        end

        def formatted_request_header_key(key)
          'HTTP_' + key.to_s.gsub('-', '_')
        end
      end      
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pact-1.0.20 lib/pact/provider/request.rb
pact-1.0.19 lib/pact/provider/request.rb