Module: Pact::Consumer::RackRequestHelper

Included in:
MockService::ControlServer::App, MockService::RequestHandlers::BaseRequestHandler
Defined in:
lib/pact/consumer/mock_service/rack_request_helper.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#params_hash(env) ⇒ Object



15
16
17
# File 'lib/pact/consumer/mock_service/rack_request_helper.rb', line 15

def params_hash env
  Pact::Query.parse_string(env["QUERY_STRING"])
end

#request_as_hash_from(env) ⇒ Object



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

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