/** * Create a new MockMultipartFile with the given content. * @param name the name of the file * @param contentStream the content of the file as stream * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); }
/** * Copy the contents of the given input File into a new byte array. * @param in the file to copy from * @return the new byte array that has been copied to * @throws IOException in case of I/O errors */ public static byte[] copyToByteArray(File in) throws IOException { Assert.notNull(in, "No input File specified"); return copyToByteArray(Files.newInputStream(in.toPath())); }
/** * Create a new MockMultipartFile with the given content. * @param name the name of the file * @param contentStream the content of the file as stream * @throws IOException if reading from the stream failed */ public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); }
@Override public String getResourceVersion(Resource resource) { try { byte[] content = FileCopyUtils.copyToByteArray(resource.getInputStream()); return DigestUtils.md5DigestAsHex(content); } catch (IOException ex) { throw new IllegalStateException("Failed to calculate hash for " + resource, ex); } }
@Override public byte[] getAttachmentAsByteArray(String cid) { try { DataHandler dataHandler = getAttachmentAsDataHandler(cid); return FileCopyUtils.copyToByteArray(dataHandler.getInputStream()); } catch (IOException ex) { throw new UnmarshallingFailureException("Couldn't read attachment", ex); } }
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { ClientHttpResponse response = execution.execute(request, body); byte[] expected = FileCopyUtils.copyToByteArray(this.resource.getInputStream()); byte[] actual = FileCopyUtils.copyToByteArray(response.getBody()); assertEquals(new String(expected), new String(actual)); return response; } }
@Test public void copyToByteArray() throws IOException { byte[] content = "content".getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(content); byte[] result = FileCopyUtils.copyToByteArray(in); assertTrue(Arrays.equals(content, result)); }
@Override public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException { ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream()); assertArrayEquals(requestBody, buf); } };
@Test public void getResourceVersion() throws Exception { Resource expected = new ClassPathResource("test/bar.css", getClass()); String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream())); assertEquals(hash, this.strategy.getResourceVersion(expected).block()); }
@Test public void getResourceVersion() throws IOException { Resource expected = new ClassPathResource("test/bar.css", getClass()); String hash = DigestUtils.md5DigestAsHex(FileCopyUtils.copyToByteArray(expected.getInputStream())); assertEquals(hash, this.versionStrategy.getResourceVersion(expected)); }
@Test public void read() throws IOException { Resource logo = new ClassPathResource("logo.jpg", BufferedImageHttpMessageConverterTests.class); byte[] body = FileCopyUtils.copyToByteArray(logo.getInputStream()); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(new MediaType("image", "jpeg")); BufferedImage result = converter.read(BufferedImage.class, inputMessage); assertEquals("Invalid height", 500, result.getHeight()); assertEquals("Invalid width", 750, result.getWidth()); }
@Test public void shouldReadImageResource() throws IOException { byte[] body = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("logo.jpg")); MockHttpInputMessage inputMessage = new MockHttpInputMessage(body); inputMessage.getHeaders().setContentType(MediaType.IMAGE_JPEG); inputMessage.getHeaders().setContentDisposition( ContentDisposition.builder("attachment").filename("yourlogo.jpg").build()); Resource actualResource = converter.read(Resource.class, inputMessage); assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body)); assertEquals("yourlogo.jpg", actualResource.getFilename()); }
@Override public void doFilter(ServletRequest filterRequest, ServletResponse filterResponse) throws IOException, ServletException { ((HttpServletResponse) filterResponse).setStatus(HttpServletResponse.SC_OK); byte[] buf = FileCopyUtils.copyToByteArray(filterRequest.getInputStream()); assertArrayEquals(requestBody, buf); ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(filterRequest, ContentCachingRequestWrapper.class); assertArrayEquals("Hel".getBytes("UTF-8"), wrapper.getContentAsByteArray()); } };
@Test public void getBody() throws IOException { byte[] content = "Hello World".getBytes("UTF-8"); mockRequest.setContent(content); byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); assertArrayEquals("Invalid content returned", content, result); }
@Test public void getInputStream() throws IOException { PathResource resource = new PathResource(TEST_FILE); byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream()); assertThat(bytes.length, greaterThan(0)); }
@Test public void body() throws IOException { byte[] body = "Hello World".getBytes("UTF-8"); this.builder.content(body); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); byte[] result = FileCopyUtils.copyToByteArray(request.getInputStream()); assertArrayEquals(body, result); }
@Test public void cachedContentWithLimit() throws Exception { this.request.setMethod("GET"); this.request.setCharacterEncoding(CHARSET); this.request.setContent("Hello World".getBytes(CHARSET)); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, 3); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals("Hello World".getBytes(CHARSET), response); assertArrayEquals("Hel".getBytes(CHARSET), wrapper.getContentAsByteArray()); }
@Test // SPR-13317 public void getBodyWithWrappedRequest() throws Exception { byte[] bytes = "content".getBytes("UTF-8"); MultipartFile part = new MockMultipartFile("part", "", "application/json", bytes); this.mockRequest.addFile(part); HttpServletRequest wrapped = new HttpServletRequestWrapper(this.mockRequest); ServerHttpRequest request = new RequestPartServletServerHttpRequest(wrapped, "part"); byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); assertArrayEquals(bytes, result); }
@Test public void getFormBody() throws IOException { // Charset (SPR-8676) mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); mockRequest.setMethod("POST"); mockRequest.addParameter("name 1", "value 1"); mockRequest.addParameter("name 2", new String[] {"value 2+1", "value 2+2"}); mockRequest.addParameter("name 3", (String) null); byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes("UTF-8"); assertArrayEquals("Invalid content returned", content, result); }
@Test // SPR-12810 public void inputStreamFormPostRequest() throws Exception { this.request.setMethod("POST"); this.request.setContentType(FORM_CONTENT_TYPE); this.request.setCharacterEncoding(CHARSET); this.request.setParameter("first", "value"); this.request.setParameter("second", "foo", "bar"); ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request); byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream()); assertArrayEquals(response, wrapper.getContentAsByteArray()); }