### Question:
FetchResponseUtils { public static Map<String, Object> getResponseAsJson(HttpResponse response, String id, String body, boolean getFullHeaders) { Map<String, Object> resp = Maps.newHashMap(); if (id != null) { resp.put("id", id); } resp.put("rc", response.getHttpStatusCode()); resp.put("body", body); Map<String, Collection<String>> headers = Maps.newHashMap(); if (getFullHeaders) { addAllHeaders(headers, response); } else { addHeaders(headers, response, "set-cookie"); addHeaders(headers, response, "location"); } if (!headers.isEmpty()) { resp.put("headers", headers); } for (Map.Entry<String, String> entry : response.getMetadata().entrySet()) { resp.put(entry.getKey(), entry.getValue()); } return resp; } private  FetchResponseUtils(); static Map<String, Object> getResponseAsJson(HttpResponse response, String id,
      String body, boolean getFullHeaders);  }### Answer:
@Test public void testSimpleResponse() throws Exception { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(999) .create(); Map<String, Object> obj = FetchResponseUtils.getResponseAsJson(response, "key", "body", false); JsonAssert.assertObjectEquals("{'rc':999,'id':'key',body:'body'}", obj); }
@Test public void testMetadata() throws Exception { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(999) .setMetadata("metaname", "metavalue") .setMetadata("more meta", "more value") .create(); Map<String, Object> obj = FetchResponseUtils.getResponseAsJson(response, null, "body", false); JsonAssert.assertObjectEquals( "{rc:999,body:'body',metaname:'metavalue','more meta':'more value'}", obj); }
@Test public void testHeaders() throws Exception { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(999) .setHeader("Set-Cookie", "cookie") .setHeader("location", "here") .create(); Map<String, Object> obj = FetchResponseUtils.getResponseAsJson(response, "key", "body", false); JsonAssert.assertObjectEquals( "{rc:999,id:'key',body:'body',headers:{set-cookie:['cookie'],location:['here']}}", obj); }
@Test public void testMultiValuedHeaders() throws Exception { HttpResponse response = new HttpResponseBuilder() .setHttpStatusCode(999) .addHeader("Set-Cookie", "cookie") .addHeader("Set-Cookie", "cookie2") .addHeader("Set-Cookie", "cookie3") .create(); Map<String, Object> obj = FetchResponseUtils.getResponseAsJson(response, "key", "body", false); JsonAssert.assertObjectEquals( "{rc:999,id:'key',body:'body',headers:{set-cookie:['cookie','cookie2','cookie3']}}", obj); }