Skip to content

Commit 8e4e0f3

Browse files
committed
Use null in MockServletContext for unknown mime types
MockServletContext.getMimeTypes now returns null if the Java Activation Framework returns "application/octet-stream", which is the default media type it returns if the mime type is unknown. This enforces the contract for ServletContext.getMimeTypes (return null for uknown mime types) but does mean "application/octet-stream" cannot be returned. Issue: SPR-10334
1 parent eefd1c4 commit 8e4e0f3

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

spring-test/src/main/java/org/springframework/mock/web/MockServletContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,16 @@ public int getEffectiveMinorVersion() {
242242
return this.effectiveMinorVersion;
243243
}
244244

245+
/**
246+
* This method uses the Java Activation framework, which returns
247+
* "application/octet-stream" when the mime type is unknown (i.e. it never returns
248+
* {@code null}). In order to maintain the {@link ServletContext#getMimeType(String)
249+
* contract, as of version 3.2.2, this method returns null if the mimeType is
250+
* "application/octet-stream".
251+
*/
245252
public String getMimeType(String filePath) {
246-
return MimeTypeResolver.getMimeType(filePath);
253+
String mimeType = MimeTypeResolver.getMimeType(filePath);
254+
return ("application/octet-stream".equals(mimeType)) ? null : mimeType;
247255
}
248256

249257
public Set<String> getResourcePaths(String path) {

spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545

4646
import org.apache.commons.logging.Log;
4747
import org.apache.commons.logging.LogFactory;
48-
4948
import org.springframework.core.io.DefaultResourceLoader;
5049
import org.springframework.core.io.Resource;
5150
import org.springframework.core.io.ResourceLoader;
@@ -258,9 +257,17 @@ public int getEffectiveMinorVersion() {
258257
return this.effectiveMinorVersion;
259258
}
260259

260+
/**
261+
* This method uses the Java Activation framework, which returns
262+
* "application/octet-stream" when the mime type is unknown (i.e. it never returns
263+
* {@code null}). In order to maintain the {@link ServletContext#getMimeType(String)
264+
* contract, as of version 3.2.2, this method returns null if the mimeType is
265+
* "application/octet-stream".
266+
*/
261267
@Override
262268
public String getMimeType(String filePath) {
263-
return MimeTypeResolver.getMimeType(filePath);
269+
String mimeType = MimeTypeResolver.getMimeType(filePath);
270+
return ("application/octet-stream".equals(mimeType)) ? null : mimeType;
264271
}
265272

266273
@Override

spring-web/src/test/java/org/springframework/web/accept/PathExtensionContentNegotiationStrategyTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ public void resolveMediaTypesFromJaf() {
7474
assertEquals(Arrays.asList(new MediaType("application", "vnd.ms-excel")), mediaTypes);
7575
}
7676

77+
// SPR-10334
78+
7779
@Test
7880
public void getMediaTypeFromFilenameNoJaf() {
7981

80-
this.servletRequest.setRequestURI("test.xls");
82+
this.servletRequest.setRequestURI("test.json");
8183

8284
ServletContext servletContext = this.servletRequest.getServletContext();
8385
PathExtensionContentNegotiationStrategy strategy =
@@ -86,7 +88,7 @@ public void getMediaTypeFromFilenameNoJaf() {
8688

8789
List<MediaType> mediaTypes = strategy.resolveMediaTypes(this.webRequest);
8890

89-
assertEquals(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM), mediaTypes);
91+
assertEquals(Collections.emptyList(), mediaTypes);
9092
}
9193

9294
// SPR-8678

0 commit comments

Comments
 (0)