private JSONObject getPerson(int id) throws Exception { HttpClient client = HttpClientBuilder.untrustedConnectionClient(); HttpRequest request = HttpClientBuilder.doGET(getUrl() + "/rest/members/" + id); HttpResponse response = client.execute(request, execOptions); JSONParser jsonParser = new JSONParser(); return (JSONObject) jsonParser.parse(response.getResponseBodyAsString()); }
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); } }
@Test @RunAsClient @InSequence(1) public void testInitialState() throws Exception { log.info("Trying URL " + getUrl()); HttpClient client = HttpClientBuilder.untrustedConnectionClient(); HttpRequest request = HttpClientBuilder.doGET(getUrl() + "/rest/members"); HttpResponse response = client.execute(request, execOptions); JSONParser jsonParser = new JSONParser(); JSONArray array = (JSONArray) jsonParser.parse(response.getResponseBodyAsString()); assertEquals(array.size(), 1); JSONObject remotePerson = getPerson(0); Person localPerson = new Person(0, "John Smith", "*****@*****.**", "2125551212"); assertPeopleAreSame(remotePerson, localPerson); }
public Client(String basicUrl) throws Exception { this.basicUrl = trimPort(basicUrl); client = HttpClientBuilder.create() .untrustedConnectionClientBuilder() .setCookieStore(cookieStore) .build(); }
@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); }