|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.net.URISyntaxException; |
21 | 22 | import java.net.URL;
|
22 | 23 | import java.nio.charset.Charset;
|
23 | 24 | import java.nio.charset.StandardCharsets;
|
|
26 | 27 | import java.util.Locale;
|
27 | 28 | import java.util.Map;
|
28 | 29 | import java.util.Set;
|
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
29 | 31 |
|
30 | 32 | import javax.naming.InitialContext;
|
31 | 33 | import javax.naming.NamingException;
|
| 34 | +import javax.servlet.MultipartConfigElement; |
| 35 | +import javax.servlet.ServletContext; |
32 | 36 | import javax.servlet.ServletException;
|
| 37 | +import javax.servlet.ServletRegistration.Dynamic; |
33 | 38 | import javax.servlet.http.HttpServlet;
|
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
34 | 41 |
|
35 | 42 | import org.apache.catalina.Container;
|
36 | 43 | import org.apache.catalina.Context;
|
|
58 | 65 |
|
59 | 66 | import org.springframework.boot.testsupport.rule.OutputCapture;
|
60 | 67 | import org.springframework.boot.web.server.WebServerException;
|
| 68 | +import org.springframework.boot.web.servlet.ServletContextInitializer; |
61 | 69 | import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
|
62 | 70 | import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
|
| 71 | +import org.springframework.core.io.ByteArrayResource; |
| 72 | +import org.springframework.http.HttpEntity; |
| 73 | +import org.springframework.http.HttpHeaders; |
| 74 | +import org.springframework.http.HttpStatus; |
| 75 | +import org.springframework.http.MediaType; |
| 76 | +import org.springframework.http.ResponseEntity; |
63 | 77 | import org.springframework.test.util.ReflectionTestUtils;
|
| 78 | +import org.springframework.util.FileSystemUtils; |
| 79 | +import org.springframework.util.LinkedMultiValueMap; |
| 80 | +import org.springframework.util.MultiValueMap; |
| 81 | +import org.springframework.web.client.RestTemplate; |
64 | 82 |
|
65 | 83 | import static org.assertj.core.api.Assertions.assertThat;
|
66 | 84 | import static org.junit.Assert.fail;
|
@@ -443,6 +461,48 @@ public void exceptionThrownOnLoadFailureWhenFailCtxIfServletStartFailsIsTrue() {
|
443 | 461 | this.webServer.start();
|
444 | 462 | }
|
445 | 463 |
|
| 464 | + @Test |
| 465 | + public void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() |
| 466 | + throws IOException, URISyntaxException { |
| 467 | + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0); |
| 468 | + AtomicReference<ServletContext> servletContextReference = new AtomicReference<>(); |
| 469 | + factory.addInitializers(new ServletContextInitializer() { |
| 470 | + |
| 471 | + @Override |
| 472 | + public void onStartup(ServletContext servletContext) throws ServletException { |
| 473 | + servletContextReference.set(servletContext); |
| 474 | + Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() { |
| 475 | + |
| 476 | + @Override |
| 477 | + protected void doPost(HttpServletRequest req, |
| 478 | + HttpServletResponse resp) |
| 479 | + throws ServletException, IOException { |
| 480 | + req.getParts(); |
| 481 | + } |
| 482 | + |
| 483 | + }); |
| 484 | + servlet.addMapping("/upload"); |
| 485 | + servlet.setMultipartConfig(new MultipartConfigElement((String) null)); |
| 486 | + } |
| 487 | + |
| 488 | + }); |
| 489 | + this.webServer = factory.getWebServer(); |
| 490 | + this.webServer.start(); |
| 491 | + File temp = (File) servletContextReference.get() |
| 492 | + .getAttribute(ServletContext.TEMPDIR); |
| 493 | + FileSystemUtils.deleteRecursively(temp); |
| 494 | + RestTemplate restTemplate = new RestTemplate(); |
| 495 | + HttpHeaders headers = new HttpHeaders(); |
| 496 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
| 497 | + body.add("file", new ByteArrayResource(new byte[1024 * 1024])); |
| 498 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 499 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, |
| 500 | + headers); |
| 501 | + ResponseEntity<String> response = restTemplate |
| 502 | + .postForEntity(getLocalUrl("/upload"), requestEntity, String.class); |
| 503 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 504 | + } |
| 505 | + |
446 | 506 | @Override
|
447 | 507 | protected JspServlet getJspServlet() throws ServletException {
|
448 | 508 | Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
|
|
0 commit comments