File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed
spring-boot-autoconfigure/src
main/java/org/springframework/boot/autoconfigure/web
test/java/org/springframework/boot/autoconfigure/web Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change 6161import org .springframework .http .HttpHeaders ;
6262import org .springframework .http .MediaType ;
6363import org .springframework .http .converter .HttpMessageConverter ;
64+ import org .springframework .util .StringUtils ;
6465import org .springframework .validation .DefaultMessageCodesResolver ;
6566import org .springframework .validation .MessageCodesResolver ;
6667import 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}
Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments