Fix MediaType lookup for ResourceHttpRequestHandler
As of 4.3 ResourceHttpRequestHandler delegates to the configured ContentNegotiationManager, or one created internally, to look up the media type for are resource. This commit ensures the internally created ContentNegotiationManager is correctly injected with the ServletContext through which it can perform lookups as before. Also the ServletPathContentNegotiationStrategy now checks the ServletContext first and then delegates to its parent the PathContentNegotiationStrategy and not vice versa. This is consistent with how handleNoMatch (also in the same class) works and also matches how ResourceHttpRequestHandler worked before 4.3. Issue: SPR-14368
This commit is contained in:
@@ -281,6 +281,34 @@ public class ResourceHttpRequestHandlerTests {
|
||||
assertEquals("h1 { color:red; }", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test // SPR-14368
|
||||
public void getResourceWithMediaTypeResolvedThroughServletContext() throws Exception {
|
||||
MockServletContext servletContext = new MockServletContext() {
|
||||
|
||||
@Override
|
||||
public String getMimeType(String filePath) {
|
||||
return "foo/bar";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVirtualServerName() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
|
||||
this.handler = new ResourceHttpRequestHandler();
|
||||
this.handler.setServletContext(servletContext);
|
||||
this.handler.setLocations(paths);
|
||||
this.handler.afterPropertiesSet();
|
||||
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
this.handler.handleRequest(this.request, this.response);
|
||||
|
||||
assertEquals("foo/bar", this.response.getContentType());
|
||||
assertEquals("h1 { color:red; }", this.response.getContentAsString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidPath() throws Exception {
|
||||
for (HttpMethod method : HttpMethod.values()) {
|
||||
|
||||
Reference in New Issue
Block a user