Skip to content

Commit 187b029

Browse files
committed
Include Part headers support in MockMultipartHttpServletRequest
See gh-25829
1 parent 87b1b61 commit 187b029

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,16 @@
1616

1717
package org.springframework.mock.web;
1818

19+
import java.io.IOException;
1920
import java.util.Collections;
2021
import java.util.Enumeration;
2122
import java.util.Iterator;
2223
import java.util.List;
2324
import java.util.Map;
2425

2526
import javax.servlet.ServletContext;
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.Part;
2629

2730
import org.springframework.http.HttpHeaders;
2831
import org.springframework.http.HttpMethod;
@@ -121,9 +124,17 @@ public String getMultipartContentType(String paramOrFileName) {
121124
if (file != null) {
122125
return file.getContentType();
123126
}
124-
else {
125-
return null;
127+
try {
128+
Part part = getPart(paramOrFileName);
129+
if (part != null) {
130+
return part.getContentType();
131+
}
132+
}
133+
catch (ServletException | IOException ex) {
134+
// Should never happen (we're not actually parsing)
135+
throw new IllegalStateException(ex);
126136
}
137+
return null;
127138
}
128139

129140
@Override
@@ -147,7 +158,7 @@ public HttpHeaders getMultipartHeaders(String paramOrFileName) {
147158
String contentType = getMultipartContentType(paramOrFileName);
148159
if (contentType != null) {
149160
HttpHeaders headers = new HttpHeaders();
150-
headers.add("Content-Type", contentType);
161+
headers.add(HttpHeaders.CONTENT_TYPE, contentType);
151162
return headers;
152163
}
153164
else {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,16 @@
1616

1717
package org.springframework.mock.web.test;
1818

19+
import java.io.IOException;
1920
import java.util.Collections;
2021
import java.util.Enumeration;
2122
import java.util.Iterator;
2223
import java.util.List;
2324
import java.util.Map;
2425

2526
import javax.servlet.ServletContext;
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.Part;
2629

2730
import org.springframework.http.HttpHeaders;
2831
import org.springframework.http.HttpMethod;
@@ -121,9 +124,17 @@ public String getMultipartContentType(String paramOrFileName) {
121124
if (file != null) {
122125
return file.getContentType();
123126
}
124-
else {
125-
return null;
127+
try {
128+
Part part = getPart(paramOrFileName);
129+
if (part != null) {
130+
return part.getContentType();
131+
}
132+
}
133+
catch (ServletException | IOException ex) {
134+
// Should never happen (we're not actually parsing)
135+
throw new IllegalStateException(ex);
126136
}
137+
return null;
127138
}
128139

129140
@Override
@@ -147,7 +158,7 @@ public HttpHeaders getMultipartHeaders(String paramOrFileName) {
147158
String contentType = getMultipartContentType(paramOrFileName);
148159
if (contentType != null) {
149160
HttpHeaders headers = new HttpHeaders();
150-
headers.add("Content-Type", contentType);
161+
headers.add(HttpHeaders.CONTENT_TYPE, contentType);
151162
return headers;
152163
}
153164
else {

spring-web/src/test/java/org/springframework/web/multipart/support/RequestPartServletServerHttpRequestTests.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,13 @@ public HttpHeaders getMultipartHeaders(String paramOrFileName) {
139139
assertArrayEquals(bytes, result);
140140
}
141141

142-
@Test
142+
@Test // gh-25829
143143
public void getBodyViaRequestPart() throws Exception {
144-
MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest() {
145-
@Override
146-
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
147-
HttpHeaders headers = new HttpHeaders();
148-
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
149-
return headers;
150-
}
151-
};
152-
153144
byte[] bytes = "content".getBytes("UTF-8");
154145
MockPart mockPart = new MockPart("part", bytes);
155146
mockPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
156-
mockRequest.addPart(mockPart);
157-
mockRequest.addPart(mockPart);
158-
ServerHttpRequest request = new RequestPartServletServerHttpRequest(mockRequest, "part");
147+
this.mockRequest.addPart(mockPart);
148+
ServerHttpRequest request = new RequestPartServletServerHttpRequest(this.mockRequest, "part");
159149

160150
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
161151
assertArrayEquals(bytes, result);

0 commit comments

Comments
 (0)