Sha256: 5420a6764685c6fb68afa1f23073aaa23d0e959bcb87f1b9b5a643bbb42c3934

Contents?: true

Size: 1.44 KB

Versions: 8

Compression:

Stored size: 1.44 KB

Contents

module Pact
  module Consumer

    module RackRequestHelper
      REQUEST_KEYS = {
        'REQUEST_METHOD' => :method,
        'REQUEST_PATH' => :path,
        'QUERY_STRING' => :query,
        'rack.input' => :body
      }

      def params_hash env
        env["QUERY_STRING"].split("&").collect{| param| param.split("=")}.inject({}){|params, param| params[param.first] = URI.decode(param.last); params }
      end

      def request_as_hash_from env
        request = env.inject({}) do |memo, (k, v)|
          request_key = REQUEST_KEYS[k]
          memo[request_key] = v if request_key
          memo
        end

        request[:headers] = headers_from env
        body_string = request[:body].read

        if body_string.empty?
          request.delete :body
        else
          body_is_json = request[:headers]['Content-Type'] =~ /json/
          request[:body] =  body_is_json ? JSON.parse(body_string) : body_string
        end
        request[:method] = request[:method].downcase
        request
      end

      private

      def headers_from env
        headers = env.reject{ |key, value| !(key.start_with?("HTTP") || key == 'CONTENT_TYPE')}
        headers.inject({}) do | hash, header |
          hash[standardise_header(header.first)] = header.last
          hash
        end
      end

      def standardise_header header
        header.gsub(/^HTTP_/, '').split("_").collect{|word| word[0] + word[1..-1].downcase}.join("-")
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
pact-1.0.20 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.19 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.18 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.15 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.13 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.12 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.11 lib/pact/consumer/mock_service/rack_request_helper.rb
pact-1.0.10 lib/pact/consumer/mock_service/rack_request_helper.rb