Browse Source

Polishing in ServletServerHttpRequest

See gh-34675
pull/34732/head
rstoyanchev 9 months ago
parent
commit
e01ad5a08d
  1. 17
      spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java
  2. 24
      spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

17
spring-web/src/main/java/org/springframework/http/server/ServletServerHttpRequest.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -273,22 +273,21 @@ public class ServletServerHttpRequest implements ServerHttpRequest {
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET); Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);
Map<String, String[]> form = request.getParameterMap(); Map<String, String[]> form = request.getParameterMap();
for (Iterator<Map.Entry<String, String[]>> entryIterator = form.entrySet().iterator(); entryIterator.hasNext();) { for (Iterator<Map.Entry<String, String[]>> entryItr = form.entrySet().iterator(); entryItr.hasNext();) {
Map.Entry<String, String[]> entry = entryIterator.next(); Map.Entry<String, String[]> entry = entryItr.next();
String name = entry.getKey();
List<String> values = Arrays.asList(entry.getValue()); List<String> values = Arrays.asList(entry.getValue());
for (Iterator<String> valueIterator = values.iterator(); valueIterator.hasNext();) { for (Iterator<String> valueItr = values.iterator(); valueItr.hasNext();) {
String value = valueIterator.next(); String value = valueItr.next();
writer.write(URLEncoder.encode(name, FORM_CHARSET)); writer.write(URLEncoder.encode(entry.getKey(), FORM_CHARSET));
if (value != null) { if (value != null) {
writer.write('='); writer.write('=');
writer.write(URLEncoder.encode(value, FORM_CHARSET)); writer.write(URLEncoder.encode(value, FORM_CHARSET));
if (valueIterator.hasNext()) { if (valueItr.hasNext()) {
writer.write('&'); writer.write('&');
} }
} }
} }
if (entryIterator.hasNext()) { if (entryItr.hasNext()) {
writer.append('&'); writer.append('&');
} }
} }

24
spring-web/src/test/java/org/springframework/http/server/ServletServerHttpRequestTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -182,9 +182,7 @@ class ServletServerHttpRequestTests {
mockRequest.addParameter("name 2", "value 2+1", "value 2+2"); mockRequest.addParameter("name 2", "value 2+1", "value 2+2");
mockRequest.addParameter("name 3", (String) null); mockRequest.addParameter("name 3", (String) null);
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); assertFormContent("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3");
byte[] content = "name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8);
assertThat(result).as("Invalid content returned").isEqualTo(content);
} }
@Test @Test
@ -192,9 +190,7 @@ class ServletServerHttpRequestTests {
mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); mockRequest.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
mockRequest.setMethod("POST"); mockRequest.setMethod("POST");
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); assertFormContent("");
byte[] content = "".getBytes(StandardCharsets.UTF_8);
assertThat(result).as("Invalid content returned").isEqualTo(content);
} }
@Test // gh-31327 @Test // gh-31327
@ -206,9 +202,7 @@ class ServletServerHttpRequestTests {
mockRequest.setContent("foo=bar".getBytes(StandardCharsets.UTF_8)); mockRequest.setContent("foo=bar".getBytes(StandardCharsets.UTF_8));
mockRequest.addHeader("Content-Length", 7); mockRequest.addHeader("Content-Length", 7);
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); assertFormContent("foo=bar");
byte[] content = "foo=bar".getBytes(StandardCharsets.UTF_8);
assertThat(result).as("Invalid content returned").isEqualTo(content);
} }
@Test // gh-32471 @Test // gh-32471
@ -219,9 +213,15 @@ class ServletServerHttpRequestTests {
mockRequest.addParameter("lastName", "Test@er"); mockRequest.addParameter("lastName", "Test@er");
mockRequest.addHeader("Content-Length", 26); mockRequest.addHeader("Content-Length", 26);
int contentLength = assertFormContent("name=Test&lastName=Test%40er");
assertThat(request.getHeaders().getContentLength()).isEqualTo(contentLength);
}
private int assertFormContent(String expected) throws IOException {
byte[] result = FileCopyUtils.copyToByteArray(request.getBody()); byte[] result = FileCopyUtils.copyToByteArray(request.getBody());
assertThat(result).isEqualTo("name=Test&lastName=Test%40er".getBytes(StandardCharsets.UTF_8)); byte[] content = expected.getBytes(StandardCharsets.UTF_8);
assertThat(request.getHeaders().getContentLength()).isEqualTo(result.length); assertThat(result).as("Invalid content returned").isEqualTo(content);
return result.length;
} }
@Test @Test

Loading…
Cancel
Save