Browse Source

Merge branch '1.4.x' into 1.5.x

pull/7164/head
Andy Wilkinson 9 years ago
parent
commit
f0b235cbc5
  1. 10
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
  2. 23
      spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java

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

@ -61,6 +61,7 @@ import org.springframework.format.datetime.DateFormatter; @@ -61,6 +61,7 @@ import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StringUtils;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.web.accept.ContentNegotiationManager;
@ -516,8 +517,7 @@ public class WebMvcAutoConfiguration { @@ -516,8 +517,7 @@ public class WebMvcAutoConfiguration {
@Override
public Object getHandlerInternal(HttpServletRequest request) throws Exception {
for (MediaType mediaType : MediaType
.parseMediaTypes(request.getHeader(HttpHeaders.ACCEPT))) {
for (MediaType mediaType : getAcceptedMediaTypes(request)) {
if (mediaType.includes(MediaType.TEXT_HTML)) {
return super.getHandlerInternal(request);
}
@ -525,6 +525,12 @@ public class WebMvcAutoConfiguration { @@ -525,6 +525,12 @@ public class WebMvcAutoConfiguration {
return null;
}
private List<MediaType> getAcceptedMediaTypes(HttpServletRequest request) {
String acceptHeader = request.getHeader(HttpHeaders.ACCEPT);
return MediaType.parseMediaTypes(
StringUtils.hasText(acceptHeader) ? acceptHeader : "*/*");
}
}
}

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

@ -577,7 +577,7 @@ public class WebMvcAutoConfigurationTests { @@ -577,7 +577,7 @@ public class WebMvcAutoConfigurationTests {
}
@Test
public void welcomePageMappingOnlyHandlesRequestsThatAcceptTextHtml()
public void welcomePageMappingDoesNotHandleRequestThatDoNotAcceptTextHtml()
throws Exception {
load("spring.resources.static-locations:classpath:/welcome-page/");
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
@ -587,6 +587,27 @@ public class WebMvcAutoConfigurationTests { @@ -587,6 +587,27 @@ public class WebMvcAutoConfigurationTests {
.andExpect(status().isNotFound());
}
@Test
public void welcomePageMappingHandlesRequestsWithNoAcceptHeader() throws Exception {
load("spring.resources.static-locations:classpath:/welcome-page/");
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
mockMvc.perform(get("/")).andExpect(status().isOk())
.andExpect(forwardedUrl("index.html"));
}
@Test
public void welcomePageMappingHandlesRequestsWithEmptyAcceptHeader()
throws Exception {
load("spring.resources.static-locations:classpath:/welcome-page/");
assertThat(this.context.getBeansOfType(WelcomePageHandlerMapping.class))
.hasSize(1);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
mockMvc.perform(get("/").header(HttpHeaders.ACCEPT, ""))
.andExpect(status().isOk()).andExpect(forwardedUrl("index.html"));
}
@Test
public void welcomePageMappingWorksWithNoTrailingSlashOnResourceLocation()
throws Exception {

Loading…
Cancel
Save