|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.net.URISyntaxException; |
21 | 22 | import java.nio.charset.Charset;
|
22 | 23 | import java.util.Arrays;
|
23 | 24 | import java.util.Locale;
|
|
30 | 31 | import javax.servlet.Filter;
|
31 | 32 | import javax.servlet.FilterChain;
|
32 | 33 | import javax.servlet.FilterConfig;
|
| 34 | +import javax.servlet.MultipartConfigElement; |
33 | 35 | import javax.servlet.ServletContext;
|
34 | 36 | import javax.servlet.ServletException;
|
| 37 | +import javax.servlet.ServletRegistration.Dynamic; |
35 | 38 | import javax.servlet.ServletRequest;
|
36 | 39 | import javax.servlet.ServletResponse;
|
| 40 | +import javax.servlet.http.HttpServlet; |
| 41 | +import javax.servlet.http.HttpServletRequest; |
| 42 | +import javax.servlet.http.HttpServletResponse; |
37 | 43 |
|
38 | 44 | import org.apache.catalina.Container;
|
39 | 45 | import org.apache.catalina.Context;
|
|
63 | 69 | import org.springframework.boot.context.embedded.Ssl;
|
64 | 70 | import org.springframework.boot.testutil.InternalOutputCapture;
|
65 | 71 | import org.springframework.boot.web.servlet.ServletContextInitializer;
|
| 72 | +import org.springframework.core.io.ByteArrayResource; |
| 73 | +import org.springframework.http.HttpEntity; |
| 74 | +import org.springframework.http.HttpHeaders; |
| 75 | +import org.springframework.http.HttpStatus; |
| 76 | +import org.springframework.http.MediaType; |
| 77 | +import org.springframework.http.ResponseEntity; |
66 | 78 | import org.springframework.test.util.ReflectionTestUtils;
|
| 79 | +import org.springframework.util.FileSystemUtils; |
| 80 | +import org.springframework.util.LinkedMultiValueMap; |
| 81 | +import org.springframework.util.MultiValueMap; |
67 | 82 | import org.springframework.util.SocketUtils;
|
| 83 | +import org.springframework.web.client.RestTemplate; |
68 | 84 |
|
69 | 85 | import static org.assertj.core.api.Assertions.assertThat;
|
70 | 86 | import static org.junit.Assert.fail;
|
@@ -539,6 +555,49 @@ public void destroy() {
|
539 | 555 | }
|
540 | 556 | }
|
541 | 557 |
|
| 558 | + @Test |
| 559 | + public void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() |
| 560 | + throws IOException, URISyntaxException { |
| 561 | + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory( |
| 562 | + 0); |
| 563 | + AtomicReference<ServletContext> servletContextReference = new AtomicReference<>(); |
| 564 | + factory.addInitializers(new ServletContextInitializer() { |
| 565 | + |
| 566 | + @Override |
| 567 | + public void onStartup(ServletContext servletContext) throws ServletException { |
| 568 | + servletContextReference.set(servletContext); |
| 569 | + Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() { |
| 570 | + |
| 571 | + @Override |
| 572 | + protected void doPost(HttpServletRequest req, |
| 573 | + HttpServletResponse resp) |
| 574 | + throws ServletException, IOException { |
| 575 | + req.getParts(); |
| 576 | + } |
| 577 | + |
| 578 | + }); |
| 579 | + servlet.addMapping("/upload"); |
| 580 | + servlet.setMultipartConfig(new MultipartConfigElement((String) null)); |
| 581 | + } |
| 582 | + |
| 583 | + }); |
| 584 | + this.container = factory.getEmbeddedServletContainer(); |
| 585 | + this.container.start(); |
| 586 | + File temp = (File) servletContextReference.get() |
| 587 | + .getAttribute(ServletContext.TEMPDIR); |
| 588 | + FileSystemUtils.deleteRecursively(temp); |
| 589 | + RestTemplate restTemplate = new RestTemplate(); |
| 590 | + HttpHeaders headers = new HttpHeaders(); |
| 591 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
| 592 | + body.add("file", new ByteArrayResource(new byte[1024 * 1024])); |
| 593 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 594 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, |
| 595 | + headers); |
| 596 | + ResponseEntity<String> response = restTemplate |
| 597 | + .postForEntity(getLocalUrl("/upload"), requestEntity, String.class); |
| 598 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 599 | + } |
| 600 | + |
542 | 601 | @Override
|
543 | 602 | protected JspServlet getJspServlet() throws ServletException {
|
544 | 603 | Container context = ((TomcatEmbeddedServletContainer) this.container).getTomcat()
|
|
0 commit comments