16
16
17
17
package org .springframework .web .util ;
18
18
19
- import java .io .UnsupportedEncodingException ;
20
- import java .nio .charset .StandardCharsets ;
21
-
22
19
import org .junit .jupiter .api .Test ;
23
20
24
21
import org .springframework .http .HttpMethod ;
25
22
import org .springframework .http .MediaType ;
26
23
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
27
24
25
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
28
26
import static org .assertj .core .api .Assertions .assertThat ;
29
27
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
30
28
@@ -38,68 +36,59 @@ class ContentCachingRequestWrapperTests {
38
36
39
37
protected static final String FORM_CONTENT_TYPE = MediaType .APPLICATION_FORM_URLENCODED_VALUE ;
40
38
41
- protected static final String CHARSET = StandardCharsets .UTF_8 .name ();
42
-
43
- protected static final String GET = HttpMethod .GET .name ();
44
-
45
- protected static final String POST = HttpMethod .POST .name ();
46
-
47
- protected static final int CONTENT_CACHE_LIMIT = 3 ;
48
-
49
39
50
40
@ Test
51
- void cachedContentToByteArrayWithNoRead () throws Exception {
52
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello" ) );
41
+ void cachedContentToByteArrayWithNoRead () {
42
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello" , - 1 );
53
43
assertThat (wrapper .getContentAsByteArray ()).isEmpty ();
54
44
}
55
45
56
46
@ Test
57
- void cachedContentToStringWithNoRead () throws Exception {
58
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello" ) );
47
+ void cachedContentToStringWithNoRead () {
48
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello" , - 1 );
59
49
assertThat (wrapper .getContentAsString ()).isEqualTo ("" );
60
50
}
61
51
62
52
@ Test
63
53
void cachedContentToByteArray () throws Exception {
64
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello World" ) );
54
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello World" , - 1 );
65
55
byte [] response = wrapper .getInputStream ().readAllBytes ();
66
56
assertThat (wrapper .getContentAsByteArray ()).isEqualTo (response );
67
57
}
68
58
69
59
@ Test
70
60
void cachedContentToString () throws Exception {
71
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello World" ) );
61
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello World" , - 1 );
72
62
byte [] response = wrapper .getInputStream ().readAllBytes ();
73
- assertThat (wrapper .getContentAsString ()).isEqualTo (new String (response , CHARSET ));
63
+ assertThat (wrapper .getContentAsString ()).isEqualTo (new String (response , UTF_8 ));
74
64
}
75
65
76
66
@ Test
77
67
void cachedContentToByteArrayWithLimit () throws Exception {
78
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello World" ), CONTENT_CACHE_LIMIT );
68
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello World" , 3 );
79
69
byte [] response = wrapper .getInputStream ().readAllBytes ();
80
- assertThat (response ).isEqualTo ("Hello World" .getBytes (CHARSET ));
81
- assertThat (wrapper .getContentAsByteArray ()).isEqualTo ("Hel" .getBytes (CHARSET ));
70
+ assertThat (response ).isEqualTo ("Hello World" .getBytes (UTF_8 ));
71
+ assertThat (wrapper .getContentAsByteArray ()).isEqualTo ("Hel" .getBytes (UTF_8 ));
82
72
}
83
73
84
74
@ Test
85
75
void cachedContentToStringWithLimit () throws Exception {
86
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello World" ), CONTENT_CACHE_LIMIT );
76
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello World" , 3 );
87
77
byte [] response = wrapper .getInputStream ().readAllBytes ();
88
- assertThat (response ).isEqualTo ("Hello World" .getBytes (CHARSET ));
89
- assertThat (wrapper .getContentAsString ()).isEqualTo (new String ("Hel" .getBytes (CHARSET ), CHARSET ));
78
+ assertThat (response ).isEqualTo ("Hello World" .getBytes (UTF_8 ));
79
+ assertThat (wrapper .getContentAsString ()).isEqualTo (new String ("Hel" .getBytes (UTF_8 ), UTF_8 ));
90
80
}
91
81
92
82
@ Test
93
- void shouldNotAllocateMoreThanCacheLimit () throws Exception {
94
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper ( createGetRequest ("Hello World" ), CONTENT_CACHE_LIMIT );
95
- assertThat (wrapper ).extracting ("cachedContent.initialBlockSize" ).isEqualTo (CONTENT_CACHE_LIMIT );
83
+ void shouldNotAllocateMoreThanCacheLimit () {
84
+ ContentCachingRequestWrapper wrapper = createGetRequest ("Hello World" , 3 );
85
+ assertThat (wrapper ).extracting ("cachedContent.initialBlockSize" ).isEqualTo (3 );
96
86
}
97
87
98
88
99
89
@ Test
100
- void cachedContentWithOverflow () throws Exception {
101
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (
102
- createGetRequest ("Hello World" ), CONTENT_CACHE_LIMIT ) {
90
+ void cachedContentWithOverflow () {
91
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (createGetRequest ("Hello World" ), 3 ) {
103
92
@ Override
104
93
protected void handleContentOverflow (int contentCacheLimit ) {
105
94
throw new IllegalStateException (String .valueOf (contentCacheLimit ));
@@ -117,7 +106,7 @@ void requestParams() throws Exception {
117
106
request .setParameter ("first" , "value" );
118
107
request .setParameter ("second" , "foo" , "bar" );
119
108
120
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request );
109
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request , - 1 );
121
110
// getting request parameters will consume the request body
122
111
assertThat (wrapper .getParameterMap ()).isNotEmpty ();
123
112
assertThat (new String (wrapper .getContentAsByteArray ())).isEqualTo ("first=value&second=foo&second=bar" );
@@ -131,25 +120,30 @@ void inputStreamFormPostRequest() throws Exception {
131
120
request .setParameter ("first" , "value" );
132
121
request .setParameter ("second" , "foo" , "bar" );
133
122
134
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request );
123
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request , - 1 );
135
124
136
125
byte [] response = wrapper .getInputStream ().readAllBytes ();
137
126
assertThat (wrapper .getContentAsByteArray ()).isEqualTo (response );
138
127
}
139
128
140
- private MockHttpServletRequest createGetRequest (String content ) throws UnsupportedEncodingException {
129
+ private ContentCachingRequestWrapper createGetRequest (String content , int cacheLimit ) {
130
+ return new ContentCachingRequestWrapper (createGetRequest (content ), cacheLimit );
131
+ }
132
+
133
+
134
+ private MockHttpServletRequest createGetRequest (String content ) {
141
135
MockHttpServletRequest request = new MockHttpServletRequest ();
142
- request .setMethod (GET );
143
- request .setCharacterEncoding (CHARSET );
144
- request .setContent (content .getBytes (CHARSET ));
136
+ request .setMethod (HttpMethod . GET . name () );
137
+ request .setCharacterEncoding (UTF_8 );
138
+ request .setContent (content .getBytes (UTF_8 ));
145
139
return request ;
146
140
}
147
141
148
142
private MockHttpServletRequest createPostRequest () {
149
143
MockHttpServletRequest request = new MockHttpServletRequest ();
150
- request .setMethod (POST );
144
+ request .setMethod (HttpMethod . POST . name () );
151
145
request .setContentType (FORM_CONTENT_TYPE );
152
- request .setCharacterEncoding (CHARSET );
146
+ request .setCharacterEncoding (UTF_8 . name () );
153
147
return request ;
154
148
}
155
149
0 commit comments