Skip to content

Commit 58770e0

Browse files
committed
Merge branch '1.5.x'
2 parents 92db3b4 + f0b235c commit 58770e0

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.http.HttpHeaders;
6262
import org.springframework.http.MediaType;
6363
import org.springframework.http.converter.HttpMessageConverter;
64+
import org.springframework.util.StringUtils;
6465
import org.springframework.validation.DefaultMessageCodesResolver;
6566
import org.springframework.validation.MessageCodesResolver;
6667
import org.springframework.web.accept.ContentNegotiationManager;
@@ -516,15 +517,20 @@ private WelcomePageHandlerMapping(Resource welcomePage) {
516517

517518
@Override
518519
public Object getHandlerInternal(HttpServletRequest request) throws Exception {
519-
for (MediaType mediaType : MediaType
520-
.parseMediaTypes(request.getHeader(HttpHeaders.ACCEPT))) {
520+
for (MediaType mediaType : getAcceptedMediaTypes(request)) {
521521
if (mediaType.includes(MediaType.TEXT_HTML)) {
522522
return super.getHandlerInternal(request);
523523
}
524524
}
525525
return null;
526526
}
527527

528+
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) {
529+
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
530+
return MediaType.parseMediaTypes(
531+
StringUtils.hasText(acceptHeader) ? acceptHeader : "*/*");
532+
}
533+
528534
}
529535

530536
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ public void welcomePageMappingHandlesRequestsThatAcceptTextHtml() throws Excepti
577577
}
578578

579579
@Test
580-
public void welcomePageMappingOnlyHandlesRequestsThatAcceptTextHtml()
580+
public void welcomePageMappingDoesNotHandleRequestThatDoNotAcceptTextHtml()
581581
throws Exception {
582582
load("spring.resources.static-locations:classpath:/welcome-page/");
583583
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
@@ -587,6 +587,27 @@ public void welcomePageMappingOnlyHandlesRequestsThatAcceptTextHtml()
587587
.andExpect(status().isNotFound());
588588
}
589589

590+
@Test
591+
public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() throws Exception {
592+
load("spring.resources.static-locations:classpath:/welcome-page/");
593+
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
594+
.hasSize(1);
595+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
596+
mockMvc.perform(get("/")).andExpect(status().isOk())
597+
.andExpect(forwardedUrl("index.html"));
598+
}
599+
600+
@Test
601+
public void welcomePageMappingHandlesRequestsWithEmptyAcceptHeader()
602+
throws Exception {
603+
load("spring.resources.static-locations:classpath:/welcome-page/");
604+
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
605+
.hasSize(1);
606+
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
607+
mockMvc.perform(get("/").header(HttpHeaders.ACCEPT, ""))
608+
.andExpect(status().isOk()).andExpect(forwardedUrl("index.html"));
609+
}
610+
590611
@Test
591612
public void welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation()
592613
throws Exception {

0 commit comments

Comments
 (0)