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.16.0 lib/pact/provider/request.rb
pact-1.15.0 lib/pact/provider/request.rb
pact-1.14.0 lib/pact/provider/request.rb
pact-1.13.0 lib/pact/provider/request.rb
pact-1.12.1 lib/pact/provider/request.rb
pact-1.12.0 lib/pact/provider/request.rb
pact-1.11.1 lib/pact/provider/request.rb
pact-1.11.0 lib/pact/provider/request.rb
pact-1.10.0 lib/pact/provider/request.rb
pact-1.9.6 lib/pact/provider/request.rb
pact-1.9.5 lib/pact/provider/request.rb
pact-1.9.4 lib/pact/provider/request.rb
pact-1.9.3 lib/pact/provider/request.rb
pact-1.9.2 lib/pact/provider/request.rb
pact-1.9.1 lib/pact/provider/request.rb
pact-1.9.0 lib/pact/provider/request.rb
pact-1.8.1 lib/pact/provider/request.rb
pact-1.8.0 lib/pact/provider/request.rb
pact-1.7.0 lib/pact/provider/request.rb
pact-1.6.0 lib/pact/provider/request.rb