/** * Converts the given input stream into a string with the given charset. * * @see #toBytes(InputStream) * @param is * the input stream to read from * @param charset * the charset for the created string * @return the string created from the given input stream */ public static String toString(InputStream is, Charset charset) { return new String(toBytes(is), charset); }
/** * 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 testToBytes() throws IOException { assertArrayEquals("test".getBytes(), IOUtils.toBytes(new ByteArrayInputStream("test".getBytes()))); }
@Test public void testToBytesWithEmptyByteArray() throws IOException { assertArrayEquals(new byte[0], IOUtils.toBytes(new ByteArrayInputStream(new byte[0]))); }
@Test public void testToStringUsesUtf8() { assertEquals(UTF8_SYMBOL, IOUtils.toString(new ByteArrayInputStream(UTF8_SYMBOL.getBytes()))); }
@Test public void testToBytesRethrowsIOExceptionWrapped() throws IOException { InputStream is = mock(InputStream.class); IOException thrownException = new IOException("whatever"); when(is.read(any(), anyInt(), anyInt())).thenThrow(thrownException); try { IOUtils.toBytes(is); fail("expected exception to be thrown"); } catch (RuntimeException re) { assertSame(thrownException, re.getCause()); } }
@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()); }
private void testBase64DecodingFails(String base64InputString) throws WebApplicationException, IOException { InputStream is = fetchWrappedInputStream(base64InputString); try { IOUtils.toBytes(is); } catch (Exception e) { throw new Base64DecodingFailedException(e); } }
@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 testToBytesRethrowsRuntimeExceptionAsIs() throws IOException { InputStream is = mock(InputStream.class); RuntimeException thrownException = new RuntimeException("whatever"); when(is.read(any(), anyInt(), anyInt())).thenThrow(thrownException); try { IOUtils.toBytes(is); fail("expected exception to be thrown"); } catch (RuntimeException re) { assertSame(thrownException, re); } }
@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())); }
private void testBase64Decoding(String base64InputString, byte[] expectedReadBytes) throws IOException { assertArrayEquals(expectedReadBytes, IOUtils.toBytes(fetchWrappedInputStream(base64InputString))); }
@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)); }
@Test public void testNonBinaryZippedWhenZippingRequestedAndNotBinaryCompressionOnly() throws Exception { runJerseyTest(createJerseyTest(false), (jersey) -> { Response response = jersey.target() .path("/non-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("non-binary", new String(IOUtils.toBytes(unzipStream))); }); }
@Test public void createContainerRequest_BodyGiven_ShouldMapToInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody("some body") .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals("some body".getBytes(), IOUtils.toBytes(containerRequest.getEntityStream())); }
@Test public void createContainerRequest_NullBodyGiven_ShouldMapToEmptyInputStream() { WebActionRequest request = minimalRequestBuilder() .setBody(null) .build(); JRestlessContainerRequest containerRequest = handler.createContainerRequest(request); assertArrayEquals(new byte[0], IOUtils.toBytes(containerRequest.getEntityStream())); }