@Override @SuppressWarnings("PMD.ShortMethodName") public final <T extends Response> T as(final Class<T> type) { return this.response.as(type); }
@Override public int status() { return this.response.status(); } @Override
@Override public String body() { return this.response.body(); }
/** * Add, update or evict response in cache. * @param req The request to be used as key * @param rsp The response to add/update */ private void updateCache(final Request req, final Response rsp) { if (rsp.headers().containsKey(this.scvh)) { this.cache.put(req, rsp); } else if (rsp.status() == HttpURLConnection.HTTP_OK) { this.cache.remove(req); } }
/** * Fetch response from server. * @param stream The content to send. * @return The obtained response * @throws IOException If an IO exception occurs. */ private Response fetchResponse(final InputStream stream) throws IOException { final long start = System.currentTimeMillis(); final Response response = this.wire.send( this, this.home, this.mtd, this.hdrs, stream, this.connect, this.read ); final URI uri = URI.create(this.home); Logger.info( this, "#fetch(%s %s%s %s): [%d %s] in %[ms]s", this.mtd, uri.getHost(), // @checkstyle AvoidInlineConditionalsCheck (1 line) uri.getPort() > 0 ? String.format(":%d", uri.getPort()) : "", uri.getPath(), response.status(), response.reason(), System.currentTimeMillis() - start ); return response; }
throws IOException { final JsonObjectBuilder json = Json.createObjectBuilder(); json.add("status", response.status()); json.add("reason", response.reason()); final JsonObjectBuilder headers = Json.createObjectBuilder(); for (final Map.Entry<String, List<String>> pair : response.headers().entrySet()) { final JsonArrayBuilder array = Json.createArrayBuilder(); for (final String value : pair.getValue()) { json.add("body", response.body()); if (file.getParentFile().mkdirs()) { Logger.debug(this, "directory created for %s", file);
@Override public ValidationResponse validate(final String html) throws IOException { final Request req = this.request(html); final Response response = req.fetch(); if (response.status() != HttpURLConnection.HTTP_OK) { throw new IOException( response.reason() ); } return this.build( response.as(XmlResponse.class) .registerNs("nu", "http://n.validator.nu/messages/") .assertXPath("//nu:messages") .assertXPath("//nu:source") .xml() ); }
@Override public boolean lock(final String label) throws IOException { final long start = System.currentTimeMillis(); final Response rsp = this.front("lock") .body().formParam("label", label) .formParam("name", this.lck).back() .method(Request.POST) .fetch(); final boolean locked = rsp.status() == HttpURLConnection.HTTP_SEE_OTHER; Logger.info( this, "lock of \"%s\" is %B in %[ms]s: %s", this.lck, locked, System.currentTimeMillis() - start, rsp.body() ); return locked; }
@Override public Map<String, List<String>> headers() { return this.response.headers(); } @Override
@Override public final byte[] binary() { return this.response.binary(); }
@Override public String reason() { return this.response.reason(); } @Override
@Override public final Request back() { return this.response.back(); }
int actualStatusCode = response.status(); try { Assert.assertEquals("Unexpected REST response status code. Status message: " + response.reason(), restTest.getExpectedStatusCode(), actualStatusCode); } catch (Error e) { InputStream content = null; try { String payload = response.body(); System.out.println("------ START ERROR PAYLOAD ------"); if (payload.startsWith("{")) { continue; String expectedHeaderValue = entry.getValue(); List<String> headers = response.headers().get(expectedHeaderName); List<String> ctValueList = response.headers().get("Content-Type"); if (ctValueList == null) { assertNoPayload(restTest, response);
@Override public final Map<String, List<String>> headers() { return this.response.headers(); }
/** * Assume the payload is Text and do some assertions based on the configuration in the REST * Test. * * @param restTest * @param response */ private void assertTextPayload(RestTest restTest, com.jcabi.http.Response response) { InputStream inputStream = null; try { inputStream = new ByteArrayInputStream(response.binary()); List<String> lines = IOUtils.readLines(inputStream); StringBuilder builder = new StringBuilder(); for (String line : lines) { builder.append(line).append("\n"); } String actual = builder.toString(); String expected = restTest.getExpectedResponsePayload(); if (expected != null) { Assert.assertEquals("Response payload (text/plain) mismatch.", expected, actual); } } catch (Exception e) { throw new Error(e); } finally { IOUtils.closeQuietly(inputStream); } }