|
20 | 20 |
|
21 | 21 | import org.junit.jupiter.api.Test; |
22 | 22 |
|
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
23 | 24 | import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; |
24 | | -import org.springframework.boot.builder.SpringApplicationBuilder; |
25 | 25 | import org.springframework.boot.http.converter.autoconfigure.HttpMessageConvertersAutoConfiguration; |
26 | | -import org.springframework.boot.test.context.SpringBootTest; |
27 | | -import org.springframework.boot.thymeleaf.autoconfigure.ThymeleafAutoConfiguration; |
| 26 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 27 | +import org.springframework.boot.testsupport.classpath.resources.WithResource; |
28 | 28 | import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; |
29 | | -import org.springframework.boot.web.server.test.LocalServerPort; |
| 29 | +import org.springframework.boot.web.server.context.WebServerApplicationContext; |
| 30 | +import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext; |
30 | 31 | import org.springframework.boot.web.server.test.client.TestRestTemplate; |
31 | | -import org.springframework.context.annotation.Configuration; |
32 | | -import org.springframework.context.annotation.Import; |
33 | 32 | import org.springframework.http.HttpStatus; |
34 | 33 | import org.springframework.http.MediaType; |
35 | 34 | import org.springframework.http.RequestEntity; |
|
41 | 40 | * Integration tests for the welcome page. |
42 | 41 | * |
43 | 42 | * @author Madhura Bhave |
| 43 | + * @author Andy Wilkinson |
44 | 44 | */ |
45 | | -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { |
46 | | - "spring.web.resources.chain.strategy.content.enabled=true", |
47 | | - "spring.thymeleaf.prefix=classpath:/org/springframework/boot/webmvc/autoconfigure/", |
48 | | - "spring.web.resources.static-locations=classpath:/org/springframework/boot/webmvc/autoconfigure/static" }) |
| 45 | +@WithResource(name = "static/index.html", content = "custom welcome page") |
49 | 46 | class WelcomePageIntegrationTests { |
50 | 47 |
|
51 | | - @LocalServerPort |
52 | | - private int port; |
| 48 | + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( |
| 49 | + AnnotationConfigServletWebServerApplicationContext::new) |
| 50 | + .withPropertyValues("spring.web.resources.chain.strategy.content.enabled=true", "server.port=0") |
| 51 | + .withConfiguration(AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class, |
| 52 | + WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, |
| 53 | + TomcatServletWebServerAutoConfiguration.class, DispatcherServletAutoConfiguration.class)); |
53 | 54 |
|
54 | 55 | private final TestRestTemplate template = new TestRestTemplate(); |
55 | 56 |
|
56 | 57 | @Test |
57 | | - void contentStrategyWithWelcomePage() throws Exception { |
58 | | - RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + this.port + "/")) |
59 | | - .header("Accept", MediaType.ALL.toString()) |
60 | | - .build(); |
61 | | - ResponseEntity<String> content = this.template.exchange(entity, String.class); |
62 | | - assertThat(content.getBody()).contains("/custom-"); |
63 | | - assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 58 | + void contentStrategyWithWelcomePage() { |
| 59 | + this.contextRunner.run((context) -> { |
| 60 | + int port = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer().getPort(); |
| 61 | + RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + port + "/")) |
| 62 | + .header("Accept", MediaType.ALL.toString()) |
| 63 | + .build(); |
| 64 | + ResponseEntity<String> content = this.template.exchange(entity, String.class); |
| 65 | + assertThat(content.getBody()).contains("custom welcome page"); |
| 66 | + assertThat(content.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 67 | + }); |
64 | 68 | } |
65 | 69 |
|
66 | 70 | @Test |
67 | | - void notAcceptableWelcomePage() throws Exception { |
68 | | - RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + this.port + "/")) |
69 | | - .header("Accept", "spring/boot") |
70 | | - .build(); |
71 | | - ResponseEntity<String> content = this.template.exchange(entity, String.class); |
72 | | - assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE); |
73 | | - } |
74 | | - |
75 | | - @Configuration |
76 | | - @Import({ PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class, |
77 | | - HttpMessageConvertersAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class, |
78 | | - DispatcherServletAutoConfiguration.class, ThymeleafAutoConfiguration.class }) |
79 | | - static class TestConfiguration { |
80 | | - |
81 | | - static void main(String[] args) { |
82 | | - new SpringApplicationBuilder(TestConfiguration.class).run(args); |
83 | | - } |
84 | | - |
| 71 | + void notAcceptableWelcomePage() { |
| 72 | + this.contextRunner.run((context) -> { |
| 73 | + int port = ((WebServerApplicationContext) context.getSourceApplicationContext()).getWebServer().getPort(); |
| 74 | + RequestEntity<?> entity = RequestEntity.get(new URI("http://localhost:" + port + "/")) |
| 75 | + .header("Accept", "spring/boot") |
| 76 | + .build(); |
| 77 | + ResponseEntity<String> content = this.template.exchange(entity, String.class); |
| 78 | + assertThat(content.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE); |
| 79 | + }); |
85 | 80 | } |
86 | 81 |
|
87 | 82 | } |
0 commit comments