1
+ /*
2
+ * Copyright 2002-2016 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
1
17
package org .springframework .web .client ;
2
18
3
19
import java .io .EOFException ;
15
31
16
32
import org .springframework .http .MediaType ;
17
33
18
- import static org .junit .Assert .assertEquals ;
19
- import static org .junit .Assert .assertNotNull ;
20
- import static org .junit .Assert .assertThat ;
21
- import static org .junit .Assert .assertTrue ;
34
+ import static org .junit .Assert .*;
22
35
23
36
/**
24
37
* @author Brian Clozel
25
38
*/
26
39
public class AbstractMockWebServerTestCase {
27
40
28
- protected static final String helloWorld = "H \u00e9 llo W \u00f6 rld" ;
41
+ private static final Charset UTF_8 = Charset . forName ( "UTF-8" ) ;
29
42
30
- private static final Charset UTF_8 = Charset .forName ("UTF-8 " );
43
+ private static final Charset ISO_8859_1 = Charset .forName ("ISO-8859-1 " );
31
44
32
- private static final Charset ISO_8859_1 = Charset . forName ( "ISO-8859-1" ) ;
45
+ protected static final String helloWorld = "H \u00e9 llo W \u00f6 rld" ;
33
46
34
47
protected static final MediaType textContentType =
35
48
new MediaType ("text" , "plain" , Collections .singletonMap ("charset" , "UTF-8" ));
@@ -40,6 +53,7 @@ public class AbstractMockWebServerTestCase {
40
53
41
54
protected String baseUrl ;
42
55
56
+
43
57
@ Before
44
58
public void setUp () throws Exception {
45
59
this .server = new MockWebServer ();
@@ -54,68 +68,9 @@ public void tearDown() throws Exception {
54
68
this .server .shutdown ();
55
69
}
56
70
57
- protected class TestDispatcher extends Dispatcher {
58
- @ Override
59
- public MockResponse dispatch (RecordedRequest request ) throws InterruptedException {
60
- try {
61
- byte [] helloWorldBytes = helloWorld .getBytes (UTF_8 );
62
-
63
- if (request .getPath ().equals ("/get" )) {
64
- return getRequest (request , helloWorldBytes , textContentType .toString ());
65
- }
66
- else if (request .getPath ().equals ("/get/nothing" )) {
67
- return getRequest (request , new byte [0 ], textContentType .toString ());
68
- }
69
- else if (request .getPath ().equals ("/get/nocontenttype" )) {
70
- return getRequest (request , helloWorldBytes , null );
71
- }
72
- else if (request .getPath ().equals ("/post" )) {
73
- return postRequest (request , helloWorld , "/post/1" , textContentType .toString (), helloWorldBytes );
74
- }
75
- else if (request .getPath ().equals ("/jsonpost" )) {
76
- return jsonPostRequest (request , "/jsonpost/1" , "application/json; charset=utf-8" );
77
- }
78
- else if (request .getPath ().equals ("/status/nocontent" )) {
79
- return new MockResponse ().setResponseCode (204 );
80
- }
81
- else if (request .getPath ().equals ("/status/notmodified" )) {
82
- return new MockResponse ().setResponseCode (304 );
83
- }
84
- else if (request .getPath ().equals ("/status/notfound" )) {
85
- return new MockResponse ().setResponseCode (404 );
86
- }
87
- else if (request .getPath ().equals ("/status/server" )) {
88
- return new MockResponse ().setResponseCode (500 );
89
- }
90
- else if (request .getPath ().contains ("/uri/" )) {
91
- return new MockResponse ().setBody (request .getPath ()).setHeader ("Content-Type" , "text/plain" );
92
- }
93
- else if (request .getPath ().equals ("/multipart" )) {
94
- return multipartRequest (request );
95
- }
96
- else if (request .getPath ().equals ("/form" )) {
97
- return formRequest (request );
98
- }
99
- else if (request .getPath ().equals ("/delete" )) {
100
- return new MockResponse ().setResponseCode (200 );
101
- }
102
- else if (request .getPath ().equals ("/patch" )) {
103
- return patchRequest (request , helloWorld , textContentType .toString (), helloWorldBytes );
104
- }
105
- else if (request .getPath ().equals ("/put" )) {
106
- return putRequest (request , helloWorld );
107
- }
108
- return new MockResponse ().setResponseCode (404 );
109
- }
110
- catch (Throwable exc ) {
111
- return new MockResponse ().setResponseCode (500 ).setBody (exc .toString ());
112
- }
113
- }
114
- }
115
-
116
71
117
72
private MockResponse getRequest (RecordedRequest request , byte [] body , String contentType ) {
118
- if (request .getMethod ().equals ("OPTIONS" )) {
73
+ if (request .getMethod ().equals ("OPTIONS" )) {
119
74
return new MockResponse ().setResponseCode (200 ).setHeader ("Allow" , "GET, OPTIONS, HEAD, TRACE" );
120
75
}
121
76
Buffer buf = new Buffer ();
@@ -138,7 +93,7 @@ private MockResponse postRequest(RecordedRequest request, String expectedRequest
138
93
String requestContentType = request .getHeader ("Content-Type" );
139
94
assertNotNull ("No content-type" , requestContentType );
140
95
Charset charset = ISO_8859_1 ;
141
- if (requestContentType .indexOf ("charset=" ) > - 1 ) {
96
+ if (requestContentType .contains ("charset=" )) {
142
97
String charsetName = requestContentType .split ("charset=" )[1 ];
143
98
charset = Charset .forName (charsetName );
144
99
}
@@ -154,10 +109,11 @@ private MockResponse postRequest(RecordedRequest request, String expectedRequest
154
109
}
155
110
156
111
private MockResponse jsonPostRequest (RecordedRequest request , String location , String contentType ) {
157
-
158
- assertTrue ("Invalid request content-length" ,
159
- Integer .parseInt (request .getHeader ("Content-Length" )) > 0 );
160
- assertNotNull ("No content-type" , request .getHeader ("Content-Type" ));
112
+ if (request .getBodySize () > 0 ) {
113
+ assertTrue ("Invalid request content-length" ,
114
+ Integer .parseInt (request .getHeader ("Content-Length" )) > 0 );
115
+ assertNotNull ("No content-type" , request .getHeader ("Content-Type" ));
116
+ }
161
117
return new MockResponse ()
162
118
.setHeader ("Location" , baseUrl + location )
163
119
.setHeader ("Content-Type" , contentType )
@@ -177,8 +133,8 @@ private MockResponse multipartRequest(RecordedRequest request) {
177
133
assertPart (body , "form-data" , boundary , "name 2" , "text/plain" , "value 2+2" );
178
134
assertFilePart (body , "form-data" , boundary , "logo" , "logo.jpg" , "image/jpeg" );
179
135
}
180
- catch (EOFException e ) {
181
- throw new RuntimeException ( e );
136
+ catch (EOFException ex ) {
137
+ throw new IllegalStateException ( ex );
182
138
}
183
139
return new MockResponse ().setResponseCode (200 );
184
140
}
@@ -221,13 +177,14 @@ private MockResponse formRequest(RecordedRequest request) {
221
177
222
178
private MockResponse patchRequest (RecordedRequest request , String expectedRequestContent ,
223
179
String contentType , byte [] responseBody ) {
180
+
224
181
assertEquals ("PATCH" , request .getMethod ());
225
182
assertTrue ("Invalid request content-length" ,
226
183
Integer .parseInt (request .getHeader ("Content-Length" )) > 0 );
227
184
String requestContentType = request .getHeader ("Content-Type" );
228
185
assertNotNull ("No content-type" , requestContentType );
229
186
Charset charset = ISO_8859_1 ;
230
- if (requestContentType .indexOf ("charset=" ) > - 1 ) {
187
+ if (requestContentType .contains ("charset=" )) {
231
188
String charsetName = requestContentType .split ("charset=" )[1 ];
232
189
charset = Charset .forName (charsetName );
233
190
}
@@ -246,12 +203,73 @@ private MockResponse putRequest(RecordedRequest request, String expectedRequestC
246
203
String requestContentType = request .getHeader ("Content-Type" );
247
204
assertNotNull ("No content-type" , requestContentType );
248
205
Charset charset = ISO_8859_1 ;
249
- if (requestContentType .indexOf ("charset=" ) > - 1 ) {
206
+ if (requestContentType .contains ("charset=" )) {
250
207
String charsetName = requestContentType .split ("charset=" )[1 ];
251
208
charset = Charset .forName (charsetName );
252
209
}
253
210
assertEquals ("Invalid request body" , expectedRequestContent , request .getBody ().readString (charset ));
254
211
return new MockResponse ().setResponseCode (202 );
255
212
}
256
213
214
+
215
+ protected class TestDispatcher extends Dispatcher {
216
+
217
+ @ Override
218
+ public MockResponse dispatch (RecordedRequest request ) throws InterruptedException {
219
+ try {
220
+ byte [] helloWorldBytes = helloWorld .getBytes (UTF_8 );
221
+
222
+ if (request .getPath ().equals ("/get" )) {
223
+ return getRequest (request , helloWorldBytes , textContentType .toString ());
224
+ }
225
+ else if (request .getPath ().equals ("/get/nothing" )) {
226
+ return getRequest (request , new byte [0 ], textContentType .toString ());
227
+ }
228
+ else if (request .getPath ().equals ("/get/nocontenttype" )) {
229
+ return getRequest (request , helloWorldBytes , null );
230
+ }
231
+ else if (request .getPath ().equals ("/post" )) {
232
+ return postRequest (request , helloWorld , "/post/1" , textContentType .toString (), helloWorldBytes );
233
+ }
234
+ else if (request .getPath ().equals ("/jsonpost" )) {
235
+ return jsonPostRequest (request , "/jsonpost/1" , "application/json; charset=utf-8" );
236
+ }
237
+ else if (request .getPath ().equals ("/status/nocontent" )) {
238
+ return new MockResponse ().setResponseCode (204 );
239
+ }
240
+ else if (request .getPath ().equals ("/status/notmodified" )) {
241
+ return new MockResponse ().setResponseCode (304 );
242
+ }
243
+ else if (request .getPath ().equals ("/status/notfound" )) {
244
+ return new MockResponse ().setResponseCode (404 );
245
+ }
246
+ else if (request .getPath ().equals ("/status/server" )) {
247
+ return new MockResponse ().setResponseCode (500 );
248
+ }
249
+ else if (request .getPath ().contains ("/uri/" )) {
250
+ return new MockResponse ().setBody (request .getPath ()).setHeader ("Content-Type" , "text/plain" );
251
+ }
252
+ else if (request .getPath ().equals ("/multipart" )) {
253
+ return multipartRequest (request );
254
+ }
255
+ else if (request .getPath ().equals ("/form" )) {
256
+ return formRequest (request );
257
+ }
258
+ else if (request .getPath ().equals ("/delete" )) {
259
+ return new MockResponse ().setResponseCode (200 );
260
+ }
261
+ else if (request .getPath ().equals ("/patch" )) {
262
+ return patchRequest (request , helloWorld , textContentType .toString (), helloWorldBytes );
263
+ }
264
+ else if (request .getPath ().equals ("/put" )) {
265
+ return putRequest (request , helloWorld );
266
+ }
267
+ return new MockResponse ().setResponseCode (404 );
268
+ }
269
+ catch (Throwable ex ) {
270
+ return new MockResponse ().setResponseCode (500 ).setBody (ex .toString ());
271
+ }
272
+ }
273
+ }
274
+
257
275
}
0 commit comments