|
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.HashMap;
|
27 | 28 | import java.util.Locale;
|
28 | 29 | import java.util.Map;
|
| 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; |
| 38 | +import javax.servlet.http.HttpServlet; |
| 39 | +import javax.servlet.http.HttpServletRequest; |
| 40 | +import javax.servlet.http.HttpServletResponse; |
33 | 41 |
|
34 | 42 | import org.apache.catalina.Container;
|
35 | 43 | import org.apache.catalina.Context;
|
|
58 | 66 |
|
59 | 67 | import org.springframework.boot.testsupport.rule.OutputCapture;
|
60 | 68 | import org.springframework.boot.web.server.WebServerException;
|
| 69 | +import org.springframework.boot.web.servlet.ServletContextInitializer; |
61 | 70 | import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory;
|
62 | 71 | import org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests;
|
| 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; |
63 | 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; |
| 82 | +import org.springframework.web.client.RestTemplate; |
64 | 83 |
|
65 | 84 | import static org.assertj.core.api.Assertions.assertThat;
|
66 | 85 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -457,6 +476,48 @@ public void referenceClearingIsDisabled() {
|
457 | 476 | assertThat(context.getClearReferencesThreadLocals()).isFalse();
|
458 | 477 | }
|
459 | 478 |
|
| 479 | + @Test |
| 480 | + public void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() |
| 481 | + throws IOException, URISyntaxException { |
| 482 | + TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0); |
| 483 | + AtomicReference<ServletContext> servletContextReference = new AtomicReference<>(); |
| 484 | + factory.addInitializers(new ServletContextInitializer() { |
| 485 | + |
| 486 | + @Override |
| 487 | + public void onStartup(ServletContext servletContext) throws ServletException { |
| 488 | + servletContextReference.set(servletContext); |
| 489 | + Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() { |
| 490 | + |
| 491 | + @Override |
| 492 | + protected void doPost(HttpServletRequest req, |
| 493 | + HttpServletResponse resp) |
| 494 | + throws ServletException, IOException { |
| 495 | + req.getParts(); |
| 496 | + } |
| 497 | + |
| 498 | + }); |
| 499 | + servlet.addMapping("/upload"); |
| 500 | + servlet.setMultipartConfig(new MultipartConfigElement((String) null)); |
| 501 | + } |
| 502 | + |
| 503 | + }); |
| 504 | + this.webServer = factory.getWebServer(); |
| 505 | + this.webServer.start(); |
| 506 | + File temp = (File) servletContextReference.get() |
| 507 | + .getAttribute(ServletContext.TEMPDIR); |
| 508 | + FileSystemUtils.deleteRecursively(temp); |
| 509 | + RestTemplate restTemplate = new RestTemplate(); |
| 510 | + HttpHeaders headers = new HttpHeaders(); |
| 511 | + MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
| 512 | + body.add("file", new ByteArrayResource(new byte[1024 * 1024])); |
| 513 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 514 | + HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, |
| 515 | + headers); |
| 516 | + ResponseEntity<String> response = restTemplate |
| 517 | + .postForEntity(getLocalUrl("/upload"), requestEntity, String.class); |
| 518 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 519 | + } |
| 520 | + |
460 | 521 | @Override
|
461 | 522 | protected JspServlet getJspServlet() throws ServletException {
|
462 | 523 | Tomcat tomcat = ((TomcatWebServer) this.webServer).getTomcat();
|
|
0 commit comments