From 395c1e415cb7c494cde4be3d8a29c16a4d488094 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 29 Nov 2019 11:48:33 +0000 Subject: [PATCH] Polishing contribution See gh-24074 --- .../web/MockMultipartHttpServletRequest.java | 10 +++++----- .../test/MockMultipartHttpServletRequest.java | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java index e118497c19c7..b438a4fe7d43 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -124,16 +124,16 @@ public String getMultipartContentType(String paramOrFileName) { if (file != null) { return file.getContentType(); } - try { Part part = getPart(paramOrFileName); if (part != null) { return part.getContentType(); } - } catch (ServletException | IOException e) { - throw new IllegalStateException("Cannot extract content type from multipart request.", e); } - + catch (ServletException | IOException ex) { + // Should never happen (we're not actually parsing) + throw new IllegalStateException(ex); + } return null; } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java index 7aeab6f98bfa..996f1f562522 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartHttpServletRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.mock.web.test; +import java.io.IOException; import java.util.Collections; import java.util.Enumeration; import java.util.Iterator; @@ -23,6 +24,8 @@ import java.util.Map; import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.Part; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -121,9 +124,17 @@ public String getMultipartContentType(String paramOrFileName) { if (file != null) { return file.getContentType(); } - else { - return null; + try { + Part part = getPart(paramOrFileName); + if (part != null) { + return part.getContentType(); + } + } + catch (ServletException | IOException ex) { + // Should never happen (we're not actually parsing) + throw new IllegalStateException(ex); } + return null; } @Override