Module: Pact::Consumer::RackRequestHelper

Included in:
InteractionDelete, InteractionReplay, LogGet, MissingInteractionsGet, MockServiceAdministrationEndpoint, VerificationGet
Defined in:
lib/pact/consumer/mock_service/rack_request_helper.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#params_hash(env) ⇒ Object



12
13
14
# File 'lib/pact/consumer/mock_service/rack_request_helper.rb', line 12

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

#request_as_hash_from(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pact/consumer/mock_service/rack_request_helper.rb', line 16

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