public String get(String key, Map<String, String> headers) { try { String url = basicUrl; if (key != null) { url = basicUrl + "/" + key; if (basicUrl.endsWith("/")) url = basicUrl + key; } HttpRequest request = HttpClientBuilder.doGET(url); if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) request.setHeader(header.getKey(), header.getValue()); } HttpResponse response = client.execute(request); int statusCode = response.getResponseCode(); log.warning("Response Code : " + statusCode); return response.getResponseBodyAsString(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
public String post(String key) { try { String url = basicUrl; if (key != null) { url = basicUrl + "/" + key; if (basicUrl.endsWith("/")) url = basicUrl + key; } HttpRequest request = HttpClientBuilder.doPOST(url); if (params != null) request.setEntity(params); HttpResponse response = client.execute(request); int statusCode = response.getResponseCode(); log.warning("Response Code : " + statusCode); if (statusCode == 302) { String location = response.getHeader("Location"); if (location != null) { return location; } } if (statusCode != 200) return statusCode + " " + response.getResponseBodyAsString(); else return response.getResponseBodyAsString(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } }
@SuppressWarnings("unchecked") @Test @RunAsClient @InSequence(2) public void testCreatePerson() throws Exception { final String name = "ce-arq test for EAP"; final String email = "*****@*****.**"; final String phoneNumber = "555987654321"; JSONObject p = new JSONObject(); p.put("name", name); p.put("email", email); p.put("phoneNumber", phoneNumber); HttpClient client = HttpClientBuilder.untrustedConnectionClient(); HttpRequest request = HttpClientBuilder.doPOST(getUrl() + "/rest/members"); request.setHeader("Content-Type", "application/json"); request.setEntity(p.toString()); HttpResponse response = client.execute(request, execOptions); assertEquals(200, response.getResponseCode()); JSONObject remotePerson = getPerson(1); Person localPerson = new Person(1, name, email, phoneNumber); assertPeopleAreSame(remotePerson, localPerson); }