Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
03334169
Commit
03334169
authored
Oct 11, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update WelcomePageHandlerMapping to handle reqs with no Accept header
Closes gh-7138
parent
b1d2552e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
3 deletions
+30
-3
WebMvcAutoConfiguration.java
...ework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
+8
-2
WebMvcAutoConfigurationTests.java
.../boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
+22
-1
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java
View file @
03334169
...
...
@@ -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 {
@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 {
return
null
;
}
private
List
<
MediaType
>
getAcceptedMediaTypes
(
HttpServletRequest
request
)
{
String
acceptHeader
=
request
.
getHeader
(
HttpHeaders
.
ACCEPT
);
return
MediaType
.
parseMediaTypes
(
StringUtils
.
hasText
(
acceptHeader
)
?
acceptHeader
:
"*/*"
);
}
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java
View file @
03334169
...
...
@@ -577,7 +577,7 @@ public class WebMvcAutoConfigurationTests {
}
@Test
public
void
welcomePageMapping
OnlyHandlesRequestsTha
tAcceptTextHtml
()
public
void
welcomePageMapping
DoesNotHandleRequestThatDoNo
tAcceptTextHtml
()
throws
Exception
{
load
(
"spring.resources.static-locations:classpath:/welcome-page/"
);
assertThat
(
this
.
context
.
getBeansOfType
(
WelcomePageHandlerMapping
.
class
))
...
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment