private StreamSource readStreamSource(InputStream body) throws IOException { byte[] bytes = StreamUtils.copyToByteArray(body); return new StreamSource(new ByteArrayInputStream(bytes)); }
@Override public InputStream getBody() throws IOException { if (this.body == null) { this.body = StreamUtils.copyToByteArray(this.response.getBody()); } return new ByteArrayInputStream(this.body); }
private StreamSource readStreamSource(InputStream body) throws IOException { byte[] bytes = StreamUtils.copyToByteArray(body); return new StreamSource(new ByteArrayInputStream(bytes)); }
@Override public byte[] asBytes() { if (data == null) { data = UnCheck.unCheck(() -> StreamUtils.copyToByteArray(inputStream)); } return data; }
@SuppressWarnings("deprecation") // on JDK 9 private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); if (!isProcessExternalEntities()) { xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); } byte[] bytes = StreamUtils.copyToByteArray(body); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); } catch (SAXException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@Override public InputStream getBody() throws IOException { if (this.body == null) { this.body = StreamUtils.copyToByteArray(this.response.getBody()); } return new ByteArrayInputStream(this.body); }
@SuppressWarnings("deprecation") // on JDK 9 private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); if (!isProcessExternalEntities()) { xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); } byte[] bytes = StreamUtils.copyToByteArray(body); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); } catch (SAXException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@Override protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if (this.supportsReadStreaming && InputStreamResource.class == clazz) { return new InputStreamResource(inputMessage.getBody()) { @Override public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) { byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); return new ByteArrayResource(body) { @Override @Nullable public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else { throw new HttpMessageNotReadableException("Unsupported resource class: " + clazz, inputMessage); } }
@Test public void copyToByteArray() throws Exception { InputStream inputStream = spy(new ByteArrayInputStream(bytes)); byte[] actual = StreamUtils.copyToByteArray(inputStream); assertThat(actual, equalTo(bytes)); verify(inputStream, never()).close(); }
@Override public void decodeToMono() throws Exception { Flux<DataBuffer> input = Flux.concat( dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); testDecodeToMonoAll(input, Resource.class, step -> step .consumeNextWith(resource -> { try { byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream()); assertEquals("foobar", new String(bytes)); } catch (IOException e) { fail(e.getMessage()); } }) .expectComplete() .verify()); }
@Override @Test public void decode() { Flux<DataBuffer> input = Flux.concat( dataBuffer(this.fooBytes), dataBuffer(this.barBytes)); testDecodeAll(input, Resource.class, step -> step .consumeNextWith(resource -> { try { byte[] bytes = StreamUtils.copyToByteArray(resource.getInputStream()); assertEquals("foobar", new String(bytes)); } catch (IOException e) { fail(e.getMessage()); } }) .expectComplete() .verify()); }
@Test public void successWithContent() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", MediaType.TEXT_PLAIN); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(MediaType.TEXT_PLAIN, response.getHeaders().getContentType()); assertArrayEquals("foo".getBytes(), StreamUtils.copyToByteArray(response.getBody())); }
@Test public void created() throws Exception { URI location = new URI("/foo"); DefaultResponseCreator responseCreator = MockRestResponseCreators.withCreatedEntity(location); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.CREATED, response.getStatusCode()); assertEquals(location, response.getHeaders().getLocation()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void successWithContentWithoutContentType() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withSuccess("foo", null); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.OK, response.getStatusCode()); assertNull(response.getHeaders().getContentType()); assertArrayEquals("foo".getBytes(), StreamUtils.copyToByteArray(response.getBody())); }
@Test public void withStatus() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withStatus(HttpStatus.FORBIDDEN); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void success() throws Exception { MockClientHttpResponse response = (MockClientHttpResponse) MockRestResponseCreators.withSuccess().createResponse(null); assertEquals(HttpStatus.OK, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void unauthorized() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withUnauthorizedRequest(); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void noContent() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withNoContent(); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void badRequest() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withBadRequest(); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }
@Test public void serverError() throws Exception { DefaultResponseCreator responseCreator = MockRestResponseCreators.withServerError(); MockClientHttpResponse response = (MockClientHttpResponse) responseCreator.createResponse(null); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); assertTrue(response.getHeaders().isEmpty()); assertEquals(0, StreamUtils.copyToByteArray(response.getBody()).length); }