private HttpResponse unzip() throws IOException { Map<String, List<String>> newHeaders = headers.entrySet().stream() .filter(e -> !e.getKey().equalsIgnoreCase("Content-Encoding")) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); if (data.length == 0) { return new HttpResponse(status, newHeaders); } else { return new HttpResponse(status, newHeaders, HttpUtils.gunzip(data)); } }
@Test public void decompress() throws IOException { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); headers.put("Content-Encoding", Collections.singletonList("gzip")); byte[] entity = HttpUtils.gzip("foo bar baz foo bar baz".getBytes(StandardCharsets.UTF_8)); HttpResponse res = new HttpResponse(200, headers, entity); Assertions.assertEquals("foo bar baz foo bar baz", res.decompress().entityAsString()); Assertions.assertNull(res.decompress().header("content-encoding")); }
try { response = sendImpl(); int s = response.status(); if (s == 429 || s == 503) {
@Test public void decompressEmpty() throws IOException { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); headers.put("Content-Encoding", Collections.singletonList("gzip")); HttpResponse res = new HttpResponse(200, headers); Assertions.assertEquals("", res.decompress().entityAsString()); Assertions.assertEquals(0, res.decompress().entity().length); } }
@Test public void header() { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); HttpResponse res = new HttpResponse(200, headers); Assertions.assertEquals("application/json", res.header("content-type")); Assertions.assertEquals("application/json", res.header("Content-Type")); }
@Test public void toStringHeaders() { Map<String, List<String>> headers = new HashMap<>(); headers.put("Date", Collections.singletonList("Mon, 27 Jul 2012 17:21:03 GMT")); headers.put("Content-Type", Collections.singletonList("application/json")); byte[] entity = "{}".getBytes(StandardCharsets.UTF_8); HttpResponse res = new HttpResponse(200, headers, entity); String expected = "HTTP/1.1 200\nContent-Type: application/json\nDate: Mon, 27 Jul 2012 17:21:03 GMT\n\n... 2 bytes ...\n"; Assertions.assertEquals(expected, res.toString()); }
@Test public void okWithCompressedBody() throws IOException { HttpResponse res = HttpClient.DEFAULT .post(uri("/echo")) .acceptGzip() .addHeader("X-Status", "200") .withContent("text/plain", "foo") .compress() .send() .decompress(); Assertions.assertEquals(200, res.status()); Assertions.assertEquals("foo", res.entityAsString()); } }
@Test public void dateHeaderNull() { Map<String, List<String>> headers = new HashMap<>(); HttpResponse res = new HttpResponse(200, headers); Assertions.assertNull(res.dateHeader("Date")); }
@Test public void decompressNoChange() throws IOException { Map<String, List<String>> headers = new HashMap<>(); headers.put("Content-Type", Collections.singletonList("application/json")); byte[] entity = HttpUtils.gzip("foo bar baz foo bar baz".getBytes(StandardCharsets.UTF_8)); HttpResponse res = new HttpResponse(200, headers, entity); Assertions.assertSame(res, res.decompress()); }
@Test public void okWithBody() throws IOException { HttpResponse res = HttpClient.DEFAULT .post(uri("/echo")) .addHeader("X-Status", "200") .withContent("text/plain", "foo") .send(); Assertions.assertEquals(200, res.status()); Assertions.assertEquals("foo", res.entityAsString()); }
try { response = sendImpl(); int s = response.status(); if (s == 429 || s == 503) {
@Test public void toStringEmpty() { HttpResponse res = new HttpResponse(200, Collections.emptyMap()); String expected = "HTTP/1.1 200\n\n... 0 bytes ...\n"; Assertions.assertEquals(expected, res.toString()); }
@Test public void dateHeaderGMT() { Map<String, List<String>> headers = new HashMap<>(); headers.put("Date", Collections.singletonList("Fri, 27 Jul 2012 17:21:03 GMT")); HttpResponse res = new HttpResponse(200, headers); Instant expected = Instant.ofEpochMilli(1343409663000L); Assertions.assertEquals(expected, res.dateHeader("Date")); }
private void retryStatus(HttpResponse expectedRes, int expectedAttempts) throws IOException { AtomicInteger attempts = new AtomicInteger(); HttpResponseSupplier supplier = () -> { attempts.incrementAndGet(); return expectedRes; }; HttpResponse res = new TestRequestBuilder(supplier) .withInitialRetryDelay(0L) .withRetries(2) .send(); Assertions.assertEquals(expectedAttempts, attempts.get()); Assertions.assertEquals(expectedRes.status(), res.status()); }
@Test public void toStringContent() { byte[] entity = "content".getBytes(StandardCharsets.UTF_8); HttpResponse res = new HttpResponse(200, Collections.emptyMap(), entity); String expected = "HTTP/1.1 200\n\n... 7 bytes ...\n"; Assertions.assertEquals(expected, res.toString()); }
private HttpResponse unzip() throws IOException { Map<String, List<String>> newHeaders = headers.entrySet().stream() .filter(e -> !e.getKey().equalsIgnoreCase("Content-Encoding")) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); if (data.length == 0) { return new HttpResponse(status, newHeaders); } else { return new HttpResponse(status, newHeaders, HttpUtils.gunzip(data)); } }