/** * Converts the given input stream into a string with UTF8 as charset. * * @see #toString(InputStream, Charset) * @param is * the input stream to read from * @return the string created from the given input stream */ public static String toString(InputStream is) { return toString(is, StandardCharsets.UTF_8); }
@Test public void testToStringUsesUtf8() { assertEquals(UTF8_SYMBOL, IOUtils.toString(new ByteArrayInputStream(UTF8_SYMBOL.getBytes()))); }
@Test public void testGetters() throws IOException { JRestlessContainerRequest request = new DefaultJRestlessContainerRequest(URI.create("/123"), URI.create("/456"), "DELETE", new ByteArrayInputStream("123".getBytes()), ImmutableMap.of("a", ImmutableList.of("a0", "a1"))); assertEquals(URI.create("/123"), request.getBaseUri()); assertEquals(URI.create("/456"), request.getRequestUri()); assertEquals("123", IOUtils.toString(request.getEntityStream())); assertEquals(ImmutableMap.of("a", ImmutableList.of("a0", "a1")), request.getHeaders()); }
@Test public void execute_ResponseBodyGiven_ShouldRespondWithBody() throws IOException { FeignLambdaClientImpl client = new FeignLambdaClientImpl(serviceResponse); when(serviceResponse.getBody()).thenReturn("some body"); feign.Response response = client.execute(feignRequest, null); assertEquals("some body", IOUtils.toString(response.body().asInputStream())); }
@Test public void testBinaryZippedWhenZippingRequested() throws Exception { runJerseyTest(createJerseyTest(true), (jersey) -> { Response response = jersey.target() .path("/binary") .request() .header(HttpHeaders.ACCEPT_ENCODING, "gzip") .get(); assertEquals(200, response.getStatus()); assertNotNull(response.getHeaderString(HttpHeaders.CONTENT_ENCODING)); assertNotNull(response.getHeaderString(GatewayBinaryResponseFilter.HEADER_BINARY_RESPONSE)); InputStream unzipStream = new GZIPInputStream(response.readEntity(InputStream.class)); assertEquals("binary", IOUtils.toString(unzipStream)); }); }
@Test public void testBinaryBase64EncodingWithContentEncoding() throws IOException { DefaultGatewayRequest request = new DefaultGatewayRequestBuilder() .httpMethod("GET") .resource("/byte-array") .headers(ImmutableMap.of(HttpHeaders.ACCEPT_ENCODING, "gzip")) .build(); GatewayResponse response = handler.handleRequest(request, context); assertTrue(response.isIsBase64Encoded()); byte[] bytes = Base64.getDecoder().decode(response.getBody()); InputStream unzipStream = new GZIPInputStream(new ByteArrayInputStream(bytes)); assertEquals("test", IOUtils.toString(unzipStream)); assertNotNull(response.getHeaders().get(HttpHeaders.CONTENT_ENCODING)); }