16
16
17
17
package org .springframework .web .util ;
18
18
19
+ import java .io .UnsupportedEncodingException ;
19
20
import java .nio .charset .StandardCharsets ;
20
21
21
22
import org .junit .jupiter .api .Test ;
22
23
23
24
import org .springframework .http .HttpMethod ;
24
25
import org .springframework .http .MediaType ;
25
- import org .springframework .util .FileCopyUtils ;
26
26
import org .springframework .web .testfixture .servlet .MockHttpServletRequest ;
27
27
28
28
import static org .assertj .core .api .Assertions .assertThat ;
29
29
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
30
30
31
31
/**
32
+ * Tests for {@link ContentCachingRequestWrapper}.
33
+ *
32
34
* @author Brian Clozel
35
+ * @author Stephane Nicoll
33
36
*/
34
37
public class ContentCachingRequestWrapperTests {
35
38
@@ -43,78 +46,104 @@ public class ContentCachingRequestWrapperTests {
43
46
44
47
protected static final int CONTENT_CACHE_LIMIT = 3 ;
45
48
46
- private final MockHttpServletRequest request = new MockHttpServletRequest ();
47
49
50
+ @ Test
51
+ void cachedContentToByteArrayWithNoRead () throws Exception {
52
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (createGetRequest ("Hello" ));
53
+ assertThat (wrapper .getContentAsByteArray ()).isEmpty ();
54
+ }
48
55
49
56
@ 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
+ }
54
61
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 ();
57
66
assertThat (wrapper .getContentAsByteArray ()).isEqualTo (response );
58
67
}
59
68
60
69
@ 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
+ }
65
75
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 ();
68
80
assertThat (response ).isEqualTo ("Hello World" .getBytes (CHARSET ));
69
81
assertThat (wrapper .getContentAsByteArray ()).isEqualTo ("Hel" .getBytes (CHARSET ));
70
82
}
71
83
72
84
@ 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
+ }
77
91
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 ) {
79
96
@ Override
80
97
protected void handleContentOverflow (int contentCacheLimit ) {
81
98
throw new IllegalStateException (String .valueOf (contentCacheLimit ));
82
99
}
83
100
};
84
101
85
102
assertThatIllegalStateException ().isThrownBy (() ->
86
- FileCopyUtils . copyToByteArray ( wrapper .getInputStream ()))
87
- .withMessage ("3" );
103
+ wrapper .getInputStream (). readAllBytes ( ))
104
+ .withMessage ("3" );
88
105
}
89
106
90
107
@ Test
91
108
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" );
97
112
98
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (this . request );
113
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request );
99
114
// getting request parameters will consume the request body
100
115
assertThat (wrapper .getParameterMap ().isEmpty ()).isFalse ();
101
116
assertThat (new String (wrapper .getContentAsByteArray ())).isEqualTo ("first=value&second=foo&second=bar" );
102
117
// SPR-12810 : inputstream body should be consumed
103
- assertThat (new String (FileCopyUtils . copyToByteArray ( wrapper .getInputStream ()))).isEmpty ();
118
+ assertThat (new String (wrapper .getInputStream (). readAllBytes ( ))).isEmpty ();
104
119
}
105
120
106
- @ Test // SPR-12810
121
+ @ Test // SPR-12810
107
122
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" );
113
126
114
- ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (this . request );
127
+ ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper (request );
115
128
116
- byte [] response = FileCopyUtils . copyToByteArray ( wrapper .getInputStream ());
129
+ byte [] response = wrapper .getInputStream (). readAllBytes ( );
117
130
assertThat (wrapper .getContentAsByteArray ()).isEqualTo (response );
118
131
}
119
132
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
+
120
149
}
0 commit comments