Skip to content

Commit 7d6be2e

Browse files
committed
Polishing
1 parent 14afdb2 commit 7d6be2e

File tree

2 files changed

+34
-33
lines changed

2 files changed

+34
-33
lines changed

spring-web/src/main/java/org/springframework/http/converter/FormHttpMessageConverter.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151
*
5252
* <p>In other words, this converter can read and write the
5353
* {@code "application/x-www-form-urlencoded"} media type as
54-
* {@link MultiValueMap MultiValueMap&lt;String, String&gt;} and it can also
54+
* {@link MultiValueMap MultiValueMap&lt;String, String&gt;}, and it can also
5555
* write (but not read) the {@code "multipart/form-data"} media type as
5656
* {@link MultiValueMap MultiValueMap&lt;String, Object&gt;}.
5757
*
5858
* <p>When writing multipart data, this converter uses other
5959
* {@link HttpMessageConverter HttpMessageConverters} to write the respective
60-
* MIME parts. By default, basic converters are registered (for {@code Strings}
61-
* and {@code Resources}). These can be overridden through the
60+
* MIME parts. By default, basic converters are registered (e.g., for {@code String}
61+
* and {@code Resource}). These can be overridden through the
6262
* {@link #setPartConverters partConverters} property.
6363
*
6464
* <p>For example, the following snippet shows how to submit an HTML form:
@@ -157,14 +157,15 @@ public void addPartConverter(HttpMessageConverter<?> partConverter) {
157157

158158
/**
159159
* Set the default character set to use for reading and writing form data when
160-
* the request or response Content-Type header does not explicitly specify it.
160+
* the request or response {@code Content-Type} header does not explicitly
161+
* specify it.
161162
* <p>As of 4.3, this is also used as the default charset for the conversion
162163
* of text bodies in a multipart request.
163-
* <p>As of 5.0 this is also used for part headers including
164-
* "Content-Disposition" (and its filename parameter) unless (the mutually
165-
* exclusive) {@link #setMultipartCharset} is also set, in which case part
166-
* headers are encoded as ASCII and <i>filename</i> is encoded with the
167-
* "encoded-word" syntax from RFC 2047.
164+
* <p>As of 5.0, this is also used for part headers including
165+
* {@code Content-Disposition} (and its filename parameter) unless (the mutually
166+
* exclusive) {@link #setMultipartCharset multipartCharset} is also set, in
167+
* which case part headers are encoded as ASCII and <i>filename</i> is encoded
168+
* with the {@code encoded-word} syntax from RFC 2047.
168169
* <p>By default this is set to "UTF-8".
169170
*/
170171
public void setCharset(@Nullable Charset charset) {
@@ -191,10 +192,10 @@ private void applyDefaultCharset() {
191192

192193
/**
193194
* Set the character set to use when writing multipart data to encode file
194-
* names. Encoding is based on the "encoded-word" syntax defined in RFC 2047
195-
* and relies on {@code MimeUtility} from "javax.mail".
196-
* <p>As of 5.0 by default part headers, including Content-Disposition (and
197-
* its filename parameter) will be encoded based on the setting of
195+
* names. Encoding is based on the {@code encoded-word} syntax defined in
196+
* RFC 2047 and relies on {@code MimeUtility} from {@code javax.mail}.
197+
* <p>As of 5.0 by default part headers, including {@code Content-Disposition}
198+
* (and its filename parameter) will be encoded based on the setting of
198199
* {@link #setCharset(Charset)} or {@code UTF-8} by default.
199200
* @since 4.1.1
200201
* @see <a href="https://en.wikipedia.org/wiki/MIME#Encoded-Word">Encoded-Word</a>
@@ -374,8 +375,8 @@ private void writeMultipart(final MultiValueMap<String, Object> parts, HttpOutpu
374375

375376
/**
376377
* When {@link #setMultipartCharset(Charset)} is configured (i.e. RFC 2047,
377-
* "encoded-word" syntax) we need to use ASCII for part headers or otherwise
378-
* we encode directly using the configured {@link #setCharset(Charset)}.
378+
* {@code encoded-word} syntax) we need to use ASCII for part headers, or
379+
* otherwise we encode directly using the configured {@link #setCharset(Charset)}.
379380
*/
380381
private boolean isFilenameCharsetSet() {
381382
return (this.multipartCharset != null);

spring-web/src/test/java/org/springframework/web/client/AbstractMockWebServerTestCase.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private MockResponse postRequest(RecordedRequest request, String expectedRequest
8585
String location, String contentType, byte[] responseBody) {
8686

8787
assertThat(request.getHeaders().values("Content-Length").size()).isEqualTo(1);
88-
assertThat(Integer.parseInt(request.getHeader("Content-Length")) > 0).as("Invalid request content-length").isTrue();
88+
assertThat(Integer.parseInt(request.getHeader("Content-Length"))).as("Invalid request content-length").isGreaterThan(0);
8989
String requestContentType = request.getHeader("Content-Type");
9090
assertThat(requestContentType).as("No content-type").isNotNull();
9191
Charset charset = StandardCharsets.ISO_8859_1;
@@ -106,7 +106,7 @@ private MockResponse postRequest(RecordedRequest request, String expectedRequest
106106

107107
private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) {
108108
if (request.getBodySize() > 0) {
109-
assertThat(Integer.parseInt(request.getHeader("Content-Length")) > 0).as("Invalid request content-length").isTrue();
109+
assertThat(Integer.parseInt(request.getHeader("Content-Length"))).as("Invalid request content-length").isGreaterThan(0);
110110
assertThat(request.getHeader("Content-Type")).as("No content-type").isNotNull();
111111
}
112112
return new MockResponse()
@@ -137,27 +137,27 @@ private MockResponse multipartRequest(RecordedRequest request) {
137137
private void assertPart(Buffer buffer, String disposition, String boundary, String name,
138138
String contentType, String value) throws EOFException {
139139

140-
assertThat(buffer.readUtf8Line().contains("--" + boundary)).isTrue();
140+
assertThat(buffer.readUtf8Line()).contains("--" + boundary);
141141
String line = buffer.readUtf8Line();
142-
assertThat(line.contains("Content-Disposition: "+ disposition)).isTrue();
143-
assertThat(line.contains("name=\""+ name + "\"")).isTrue();
144-
assertThat(buffer.readUtf8Line().startsWith("Content-Type: "+contentType)).isTrue();
145-
assertThat(buffer.readUtf8Line().equals("Content-Length: " + value.length())).isTrue();
146-
assertThat(buffer.readUtf8Line().equals("")).isTrue();
147-
assertThat(buffer.readUtf8Line().equals(value)).isTrue();
142+
assertThat(line).contains("Content-Disposition: "+ disposition);
143+
assertThat(line).contains("name=\""+ name + "\"");
144+
assertThat(buffer.readUtf8Line()).startsWith("Content-Type: "+contentType);
145+
assertThat(buffer.readUtf8Line()).isEqualTo("Content-Length: " + value.length());
146+
assertThat(buffer.readUtf8Line()).isEqualTo("");
147+
assertThat(buffer.readUtf8Line()).isEqualTo(value);
148148
}
149149

150150
private void assertFilePart(Buffer buffer, String disposition, String boundary, String name,
151151
String filename, String contentType) throws EOFException {
152152

153-
assertThat(buffer.readUtf8Line().contains("--" + boundary)).isTrue();
153+
assertThat(buffer.readUtf8Line()).contains("--" + boundary);
154154
String line = buffer.readUtf8Line();
155-
assertThat(line.contains("Content-Disposition: "+ disposition)).isTrue();
156-
assertThat(line.contains("name=\""+ name + "\"")).isTrue();
157-
assertThat(line.contains("filename=\""+ filename + "\"")).isTrue();
158-
assertThat(buffer.readUtf8Line().startsWith("Content-Type: "+contentType)).isTrue();
159-
assertThat(buffer.readUtf8Line().startsWith("Content-Length: ")).isTrue();
160-
assertThat(buffer.readUtf8Line().equals("")).isTrue();
155+
assertThat(line).contains("Content-Disposition: " + disposition);
156+
assertThat(line).contains("name=\"" + name + "\"");
157+
assertThat(line).contains("filename=\"" + filename + "\"");
158+
assertThat(buffer.readUtf8Line()).startsWith("Content-Type: " + contentType);
159+
assertThat(buffer.readUtf8Line()).startsWith("Content-Length: ");
160+
assertThat(buffer.readUtf8Line()).isEqualTo("");
161161
assertThat(buffer.readUtf8Line()).isNotNull();
162162
}
163163

@@ -174,7 +174,7 @@ private MockResponse patchRequest(RecordedRequest request, String expectedReques
174174
String contentType, byte[] responseBody) {
175175

176176
assertThat(request.getMethod()).isEqualTo("PATCH");
177-
assertThat(Integer.parseInt(request.getHeader("Content-Length")) > 0).as("Invalid request content-length").isTrue();
177+
assertThat(Integer.parseInt(request.getHeader("Content-Length"))).as("Invalid request content-length").isGreaterThan(0);
178178
String requestContentType = request.getHeader("Content-Type");
179179
assertThat(requestContentType).as("No content-type").isNotNull();
180180
Charset charset = StandardCharsets.ISO_8859_1;
@@ -192,7 +192,7 @@ private MockResponse patchRequest(RecordedRequest request, String expectedReques
192192
}
193193

194194
private MockResponse putRequest(RecordedRequest request, String expectedRequestContent) {
195-
assertThat(Integer.parseInt(request.getHeader("Content-Length")) > 0).as("Invalid request content-length").isTrue();
195+
assertThat(Integer.parseInt(request.getHeader("Content-Length"))).as("Invalid request content-length").isGreaterThan(0);
196196
String requestContentType = request.getHeader("Content-Type");
197197
assertThat(requestContentType).as("No content-type").isNotNull();
198198
Charset charset = StandardCharsets.ISO_8859_1;

0 commit comments

Comments
 (0)