Skip to content

Commit e01ad5a

Browse files
committed
Polishing in ServletServerHttpRequest
See gh-34675
1 parent b8158df commit e01ad5a

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

Diff for: spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -273,22 +273,21 @@ private InputStream getBodyFromServletRequestParameters(HttpServletRequest reque
273273
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
274274

275275
Map<String, String[]> form = request.getParameterMap();
276-
for (Iterator<Map.Entry<String, String[]>> entryIterator = form.entrySet().iterator(); entryIterator.hasNext();) {
277-
Map.Entry<String, String[]> entry = entryIterator.next();
278-
String name = entry.getKey();
276+
for (Iterator<Map.Entry<String, String[]>> entryItr = form.entrySet().iterator(); entryItr.hasNext();) {
277+
Map.Entry<String, String[]> entry = entryItr.next();
279278
List<String> values = Arrays.asList(entry.getValue());
280-
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext();) {
281-
String value = valueIterator.next();
282-
writer.write(URLEncoder.encode(name, FORM_CHARSET));
279+
for (Iterator<String> valueItr = values.iterator(); valueItr.hasNext();) {
280+
String value = valueItr.next();
281+
writer.write(URLEncoder.encode(entry.getKey(), FORM_CHARSET));
283282
if (value != null) {
284283
writer.write('=');
285284
writer.write(URLEncoder.encode(value, FORM_CHARSET));
286-
if (valueIterator.hasNext()) {
285+
if (valueItr.hasNext()) {
287286
writer.write('&');
288287
}
289288
}
290289
}
291-
if (entryIterator.hasNext()) {
290+
if (entryItr.hasNext()) {
292291
writer.append('&');
293292
}
294293
}

Diff for: spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -182,19 +182,15 @@ void getFormBody() throws IOException {
182182
mockRequest.addParameter("name 2", "value 2+1", "value 2+2");
183183
mockRequest.addParameter("name 3", (String) null);
184184

185-
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
186-
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8);
187-
assertThat(result).as("Invalid content returned").isEqualTo(content);
185+
assertFormContent("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3");
188186
}
189187

190188
@Test
191189
void getEmptyFormBody() throws IOException {
192190
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
193191
mockRequest.setMethod("POST");
194192

195-
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
196-
byte[] content = "".getBytes(StandardCharsets.UTF_8);
197-
assertThat(result).as("Invalid content returned").isEqualTo(content);
193+
assertFormContent("");
198194
}
199195

200196
@Test // gh-31327
@@ -206,9 +202,7 @@ void getFormBodyWhenQueryParamsAlsoPresent() throws IOException {
206202
mockRequest.setContent("foo=bar".getBytes(StandardCharsets.UTF_8));
207203
mockRequest.addHeader("Content-Length", 7);
208204

209-
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
210-
byte[] content = "foo=bar".getBytes(StandardCharsets.UTF_8);
211-
assertThat(result).as("Invalid content returned").isEqualTo(content);
205+
assertFormContent("foo=bar");
212206
}
213207

214208
@Test // gh-32471
@@ -219,9 +213,15 @@ void getFormBodyWhenNotEncodedCharactersPresent() throws IOException {
219213
mockRequest.addParameter("lastName", "Test@er");
220214
mockRequest.addHeader("Content-Length", 26);
221215

216+
int contentLength = assertFormContent("name=Test&lastName=Test%40er");
217+
assertThat(request.getHeaders().getContentLength()).isEqualTo(contentLength);
218+
}
219+
220+
private int assertFormContent(String expected) throws IOException {
222221
byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
223-
assertThat(result).isEqualTo("name=Test&lastName=Test%40er".getBytes(StandardCharsets.UTF_8));
224-
assertThat(request.getHeaders().getContentLength()).isEqualTo(result.length);
222+
byte[] content = expected.getBytes(StandardCharsets.UTF_8);
223+
assertThat(result).as("Invalid content returned").isEqualTo(content);
224+
return result.length;
225225
}
226226

227227
@Test

0 commit comments

Comments
 (0)