module WebMockHTTPClients

def build_webmock_response(httpclient_response, body = nil)

def build_webmock_response(httpclient_response, body = nil)
  webmock_response = WebMock::Response.new
  webmock_response.status = [httpclient_response.status, httpclient_response.reason]
  webmock_response.headers = {}.tap do |hash|
    httpclient_response.header.all.each do |(key, value)|
      if hash.has_key?(key)
        hash[key] = Array(hash[key]) + [value]
      else
        hash[key] = value
      end
    end
  end
  if body
    webmock_response.body = body
  elsif httpclient_response.content.respond_to?(:read)
    webmock_response.body = httpclient_response.content.read
    body = HTTP::Message::Body.new
    body.init_response(StringIO.new(webmock_response.body))
    httpclient_response.body = body
  else
    webmock_response.body = httpclient_response.content
  end
  webmock_response
end