Sha256: 3ca58b2afb6df8284a93a9f4a75f73c04221b27c2c4a3fb2b8e038d566155661

Contents?: true

Size: 1.65 KB

Versions: 57

Compression:

Stored size: 1.65 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)] = 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).gsub('-', '_')
        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

57 entries across 57 versions & 1 rubygems

Version Path
pact-1.0.37 lib/pact/provider/request.rb
pact-1.0.36 lib/pact/provider/request.rb
pact-1.0.35 lib/pact/provider/request.rb
pact-1.0.34 lib/pact/provider/request.rb
pact-1.0.33 lib/pact/provider/request.rb
pact-1.0.32 lib/pact/provider/request.rb
pact-1.0.31 lib/pact/provider/request.rb
pact-1.0.30 lib/pact/provider/request.rb
pact-1.0.29 lib/pact/provider/request.rb
pact-1.0.28 lib/pact/provider/request.rb
pact-1.0.27 lib/pact/provider/request.rb
pact-1.0.26 lib/pact/provider/request.rb
pact-1.0.25 lib/pact/provider/request.rb
pact-1.0.24 lib/pact/provider/request.rb
pact-1.0.23 lib/pact/provider/request.rb
pact-1.0.22 lib/pact/provider/request.rb
pact-1.0.21 lib/pact/provider/request.rb