Skip to content

Commit 4695bd3

Browse files
committed
Polish "Get content as String for ContentCachingRequestWrapper"
See gh-30709
1 parent d9b8826 commit 4695bd3

File tree

2 files changed

+74
-39
lines changed

2 files changed

+74
-39
lines changed

spring-web/src/main/java/org/springframework/web/util/ContentCachingRequestWrapper.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -198,8 +198,14 @@ public byte[] getContentAsByteArray() {
198198
}
199199

200200
/**
201-
* Return the cached request content as a String. The Charset used to decode
202-
* the cached content is the same as returned by getCharacterEncoding.
201+
* Return the cached request content as a String, using the configured
202+
* {@link Charset}.
203+
* <p><strong>Note:</strong> The String returned from this method
204+
* reflects the amount of content that has been read at the time when it
205+
* is called. If the application does not read the content, this method
206+
* returns an empty String.
207+
* @since 6.1
208+
* @see #getContentAsByteArray()
203209
*/
204210
public String getContentAsString() {
205211
return this.cachedContent.toString(Charset.forName(getCharacterEncoding()));

spring-web/src/test/java/org/springframework/web/util/ContentCachingRequestWrapperTests.java

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616

1717
package org.springframework.web.util;
1818

19+
import java.io.UnsupportedEncodingException;
1920
import java.nio.charset.StandardCharsets;
2021

2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.http.HttpMethod;
2425
import org.springframework.http.MediaType;
25-
import org.springframework.util.FileCopyUtils;
2626
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
2929
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
3030

3131
/**
32+
* Tests for {@link ContentCachingRequestWrapper}.
33+
*
3234
* @author Brian Clozel
35+
* @author Stephane Nicoll
3336
*/
3437
public class ContentCachingRequestWrapperTests {
3538

@@ -43,78 +46,104 @@ public class ContentCachingRequestWrapperTests {
4346

4447
protected static final int CONTENT_CACHE_LIMIT = 3;
4548

46-
private final MockHttpServletRequest request = new MockHttpServletRequest();
4749

50+
@Test
51+
void cachedContentToByteArrayWithNoRead() throws Exception {
52+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello"));
53+
assertThat(wrapper.getContentAsByteArray()).isEmpty();
54+
}
4855

4956
@Test
50-
void cachedContent() throws Exception {
51-
this.request.setMethod(GET);
52-
this.request.setCharacterEncoding(CHARSET);
53-
this.request.setContent("Hello World".getBytes(CHARSET));
57+
void cachedContentToStringWithNoRead() throws Exception {
58+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello"));
59+
assertThat(wrapper.getContentAsString()).isEqualTo("");
60+
}
5461

55-
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request);
56-
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
62+
@Test
63+
void cachedContentToByteArray() throws Exception {
64+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"));
65+
byte[] response = wrapper.getInputStream().readAllBytes();
5766
assertThat(wrapper.getContentAsByteArray()).isEqualTo(response);
5867
}
5968

6069
@Test
61-
void cachedContentWithLimit() throws Exception {
62-
this.request.setMethod(GET);
63-
this.request.setCharacterEncoding(CHARSET);
64-
this.request.setContent("Hello World".getBytes(CHARSET));
70+
void cachedContentToString() throws Exception {
71+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"));
72+
byte[] response = wrapper.getInputStream().readAllBytes();
73+
assertThat(wrapper.getContentAsString()).isEqualTo(new String(response, CHARSET));
74+
}
6575

66-
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, CONTENT_CACHE_LIMIT);
67-
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
76+
@Test
77+
void cachedContentToByteArrayWithLimit() throws Exception {
78+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), CONTENT_CACHE_LIMIT);
79+
byte[] response = wrapper.getInputStream().readAllBytes();
6880
assertThat(response).isEqualTo("Hello World".getBytes(CHARSET));
6981
assertThat(wrapper.getContentAsByteArray()).isEqualTo("Hel".getBytes(CHARSET));
7082
}
7183

7284
@Test
73-
void cachedContentWithOverflow() throws Exception {
74-
this.request.setMethod(GET);
75-
this.request.setCharacterEncoding(CHARSET);
76-
this.request.setContent("Hello World".getBytes(CHARSET));
85+
void cachedContentToStringWithLimit() throws Exception {
86+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(createGetRequest("Hello World"), CONTENT_CACHE_LIMIT);
87+
byte[] response = wrapper.getInputStream().readAllBytes();
88+
assertThat(response).isEqualTo("Hello World".getBytes(CHARSET));
89+
assertThat(wrapper.getContentAsString()).isEqualTo(new String("Hel".getBytes(CHARSET), CHARSET));
90+
}
7791

78-
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request, CONTENT_CACHE_LIMIT) {
92+
@Test
93+
void cachedContentWithOverflow() throws Exception {
94+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(
95+
createGetRequest("Hello World"), CONTENT_CACHE_LIMIT) {
7996
@Override
8097
protected void handleContentOverflow(int contentCacheLimit) {
8198
throw new IllegalStateException(String.valueOf(contentCacheLimit));
8299
}
83100
};
84101

85102
assertThatIllegalStateException().isThrownBy(() ->
86-
FileCopyUtils.copyToByteArray(wrapper.getInputStream()))
87-
.withMessage("3");
103+
wrapper.getInputStream().readAllBytes())
104+
.withMessage("3");
88105
}
89106

90107
@Test
91108
void requestParams() throws Exception {
92-
this.request.setMethod(POST);
93-
this.request.setContentType(FORM_CONTENT_TYPE);
94-
this.request.setCharacterEncoding(CHARSET);
95-
this.request.setParameter("first", "value");
96-
this.request.setParameter("second", "foo", "bar");
109+
MockHttpServletRequest request = createPostRequest();
110+
request.setParameter("first", "value");
111+
request.setParameter("second", "foo", "bar");
97112

98-
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request);
113+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(request);
99114
// getting request parameters will consume the request body
100115
assertThat(wrapper.getParameterMap().isEmpty()).isFalse();
101116
assertThat(new String(wrapper.getContentAsByteArray())).isEqualTo("first=value&second=foo&second=bar");
102117
// SPR-12810 : inputstream body should be consumed
103-
assertThat(new String(FileCopyUtils.copyToByteArray(wrapper.getInputStream()))).isEmpty();
118+
assertThat(new String(wrapper.getInputStream().readAllBytes())).isEmpty();
104119
}
105120

106-
@Test // SPR-12810
121+
@Test // SPR-12810
107122
void inputStreamFormPostRequest() throws Exception {
108-
this.request.setMethod(POST);
109-
this.request.setContentType(FORM_CONTENT_TYPE);
110-
this.request.setCharacterEncoding(CHARSET);
111-
this.request.setParameter("first", "value");
112-
this.request.setParameter("second", "foo", "bar");
123+
MockHttpServletRequest request = createPostRequest();
124+
request.setParameter("first", "value");
125+
request.setParameter("second", "foo", "bar");
113126

114-
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(this.request);
127+
ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper(request);
115128

116-
byte[] response = FileCopyUtils.copyToByteArray(wrapper.getInputStream());
129+
byte[] response = wrapper.getInputStream().readAllBytes();
117130
assertThat(wrapper.getContentAsByteArray()).isEqualTo(response);
118131
}
119132

133+
private MockHttpServletRequest createGetRequest(String content) throws UnsupportedEncodingException {
134+
MockHttpServletRequest request = new MockHttpServletRequest();
135+
request.setMethod(GET);
136+
request.setCharacterEncoding(CHARSET);
137+
request.setContent(content.getBytes(CHARSET));
138+
return request;
139+
}
140+
141+
private MockHttpServletRequest createPostRequest() {
142+
MockHttpServletRequest request = new MockHttpServletRequest();
143+
request.setMethod(POST);
144+
request.setContentType(FORM_CONTENT_TYPE);
145+
request.setCharacterEncoding(CHARSET);
146+
return request;
147+
}
148+
120149
}

0 commit comments

Comments
 (0)