Sha256: c29bb5d5d31d40d93336510e28f735cce7e9be52ce1dd88f39587b7fa10e7c31

Contents?: true

Size: 1.67 KB

Versions: 42

Compression:

Stored size: 1.67 KB

Contents

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

module Pact
  module Provider
    module Request
      class Replayable

        # See https://github.com/rack/rack/blob/e7d741c6282ca4cf4e01506f5681e6e6b14c0b32/SPEC#L87-89
        NO_HTTP_PREFIX = ["CONTENT-TYPE", "CONTENT-LENGTH"]

        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|
            request_headers[rack_request_header_for(key)] = Pact::Reification.from_term(value)
          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 rack_request_header_for header
          with_http_prefix(header.to_s.upcase).tr('-', '_')
        end

        def rack_request_value_for value
          Array(value).join("\n")
        end

        def with_http_prefix header
          NO_HTTP_PREFIX.include?(header) ? header : "HTTP_#{header}"
        end
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
pact-1.65.3 lib/pact/provider/request.rb
pact-1.65.2 lib/pact/provider/request.rb
pact-1.65.1 lib/pact/provider/request.rb
pact-1.65.0 lib/pact/provider/request.rb
pact-1.64.0 lib/pact/provider/request.rb
pact-1.63.0 lib/pact/provider/request.rb
pact-1.62.0 lib/pact/provider/request.rb
pact-1.61.0 lib/pact/provider/request.rb
pact-1.60.0 lib/pact/provider/request.rb
pact-1.59.0 lib/pact/provider/request.rb
pact-1.58.0 lib/pact/provider/request.rb
pact-1.57.0 lib/pact/provider/request.rb
pact-1.56.0 lib/pact/provider/request.rb
pact-1.55.7 lib/pact/provider/request.rb
pact-1.55.6 lib/pact/provider/request.rb
pact-1.55.5 lib/pact/provider/request.rb
pact-1.55.4 lib/pact/provider/request.rb
pact-1.55.3 lib/pact/provider/request.rb
pact-1.55.2 lib/pact/provider/request.rb
pact-1.55.1 lib/pact/provider/request.rb