/** * assert that the response has the given status code * * @param res the response to check * @param status the status to check against */ protected final void assertStatus(HTTPResponse res, Status status) { Assert.assertTrue(String.format("Expected %s - was %s", status.getStatusCode(), res.getStatus()), res.getStatus() == status.getStatusCode()); }
/** * assert that the response has a 2XX status code * * @param res the response to check */ protected final void assertOK(HTTPResponse res) { Assert.assertTrue(String.format("Expected OK - was %s", res.getStatus()), res.isStatusOK()); }
private String getAuthToken(String token) { if(token == null || token.isEmpty()) { return null; } String path = this.protocol + "://" + this.server + "/api/auth"; String body = "{\"token\":\"" + token + "\"}"; HTTPRequest httpRequest = WS.url(path).body(body).header("Content-Type", "application/json;charset=UTF-8"); try (HTTPResponse response = httpRequest.put()) { int status = response.getStatus(); if (200 <= status && 300 > status) { String responseAsString = response.getResponseAsString(); if (responseAsString.startsWith("\"")) { responseAsString = responseAsString.substring(1); } if (responseAsString.endsWith("\"")) { responseAsString = responseAsString.substring(0, responseAsString.length() - 1); } return responseAsString; } else { this.logger.warn("Authentication with CloudConductor Server {} failed with status {}", this.server, status); } } return null; } }
private String createTicket(Ticket ticket) { final String json; try { json = MapperFactory.createDefault().writeValueAsString(ticket.toJsonMap()); } catch (JsonProcessingException e) { throw new RuntimeException(e); } String url = "https://" + this.subdomain + ".zendesk.com/api/v2/tickets.json"; HTTPRequest req = WS.url(url).authBasic(this.agentMail + "/token", this.agentToken).contentType(MediaType.APPLICATION_JSON).body(json); try (HTTPResponse post = req.post()) { if (post.getStatus() != 201) { throw new InternalServerErrorException(); } return post.getResponseAsString(); } }