|
39 | 39 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
40 | 40 | import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
41 | 41 | import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter;
|
| 42 | +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration.WelcomePageHandlerMapping; |
42 | 43 | import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
43 | 44 | import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
44 | 45 | import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
|
52 | 53 | import org.springframework.core.io.Resource;
|
53 | 54 | import org.springframework.format.support.FormattingConversionService;
|
54 | 55 | import org.springframework.http.HttpHeaders;
|
| 56 | +import org.springframework.http.MediaType; |
55 | 57 | import org.springframework.mock.web.MockHttpServletRequest;
|
56 | 58 | import org.springframework.test.util.ReflectionTestUtils;
|
| 59 | +import org.springframework.test.web.servlet.MockMvc; |
| 60 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
57 | 61 | import org.springframework.util.ReflectionUtils;
|
58 | 62 | import org.springframework.util.StringUtils;
|
59 | 63 | import org.springframework.web.accept.ContentNegotiationManager;
|
|
90 | 94 | import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
91 | 95 |
|
92 | 96 | import static org.assertj.core.api.Assertions.assertThat;
|
| 97 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 98 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; |
| 99 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
93 | 100 |
|
94 | 101 | /**
|
95 | 102 | * Tests for {@link WebMvcAutoConfiguration}.
|
@@ -132,7 +139,7 @@ public void handlerAdaptersCreated() throws Exception {
|
132 | 139 | public void handlerMappingsCreated() throws Exception {
|
133 | 140 | load();
|
134 | 141 | assertThat(this.context.getBeanNamesForType(HandlerMapping.class).length)
|
135 |
| - .isEqualTo(6); |
| 142 | + .isEqualTo(7); |
136 | 143 | }
|
137 | 144 |
|
138 | 145 | @Test
|
@@ -540,6 +547,51 @@ public void customLogResolvedException() {
|
540 | 547 | testLogResolvedExceptionCustomization(true);
|
541 | 548 | }
|
542 | 549 |
|
| 550 | + @Test |
| 551 | + public void welcomePageMappingProducesNotFoundResponseWhenThereIsNoWelcomePage() |
| 552 | + throws Exception { |
| 553 | + load("spring.resources.static-locations:classpath:/no-welcome-page/"); |
| 554 | + assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)) |
| 555 | + .hasSize(1); |
| 556 | + MockMvcBuilders.webAppContextSetup(this.context).build() |
| 557 | + .perform(get("/").accept(MediaType.TEXT_HTML)) |
| 558 | + .andExpect(status().isNotFound()); |
| 559 | + } |
| 560 | + |
| 561 | + @Test |
| 562 | + public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Exception { |
| 563 | + load("spring.resources.static-locations:classpath:/welcome-page/"); |
| 564 | + assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)) |
| 565 | + .hasSize(1); |
| 566 | + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
| 567 | + mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) |
| 568 | + .andExpect(forwardedUrl("index.html")); |
| 569 | + mockMvc.perform(get("/").accept("*/*")).andExpect(status().isOk()) |
| 570 | + .andExpect(forwardedUrl("index.html")); |
| 571 | + } |
| 572 | + |
| 573 | + @Test |
| 574 | + public void welcomePageMappingOnlyHandlesRequestsThatAcceptTextHtml() |
| 575 | + throws Exception { |
| 576 | + load("spring.resources.static-locations:classpath:/welcome-page/"); |
| 577 | + assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)) |
| 578 | + .hasSize(1); |
| 579 | + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
| 580 | + mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) |
| 581 | + .andExpect(status().isNotFound()); |
| 582 | + } |
| 583 | + |
| 584 | + @Test |
| 585 | + public void welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation() |
| 586 | + throws Exception { |
| 587 | + load("spring.resources.static-locations:classpath:/welcome-page"); |
| 588 | + assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class)) |
| 589 | + .hasSize(1); |
| 590 | + MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build(); |
| 591 | + mockMvc.perform(get("/").accept(MediaType.TEXT_HTML)).andExpect(status().isOk()) |
| 592 | + .andExpect(forwardedUrl("index.html")); |
| 593 | + } |
| 594 | + |
543 | 595 | private void testLogResolvedExceptionCustomization(final boolean expected) {
|
544 | 596 | HandlerExceptionResolver exceptionResolver = this.context
|
545 | 597 | .getBean(HandlerExceptionResolver.class);
|
|
0 commit comments