-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ShadowDefaultRequestDirectorTest.java
444 lines (359 loc) · 17.7 KB
/
ShadowDefaultRequestDirectorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package org.robolectric.shadows.httpclient;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import static org.robolectric.shadows.httpclient.Shadows.shadowOf;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.io.CharStreams;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.DefaultRequestDirector;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests for {@link DefaultRequestDirector} */
@RunWith(AndroidJUnit4.class)
public class ShadowDefaultRequestDirectorTest {
private DefaultRequestDirector requestDirector;
private ConnectionKeepAliveStrategy connectionKeepAliveStrategy;
@Before
public void setUp_EnsureStaticStateIsReset() {
FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer();
assertFalse(fakeHttpLayer.hasPendingResponses());
assertFalse(fakeHttpLayer.hasRequestInfos());
assertFalse(fakeHttpLayer.hasResponseRules());
connectionKeepAliveStrategy = (httpResponse, httpContext) -> 0;
requestDirector =
new DefaultRequestDirector(
null,
null,
null,
connectionKeepAliveStrategy,
null,
null,
null,
null,
null,
null,
null,
null);
}
@After
public void tearDown_EnsureStaticStateIsReset() {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
}
@Test
public void shouldGetHttpResponseFromExecute() throws Exception {
FakeHttp.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a happy response body");
}
@Test
public void shouldPreferPendingResponses() throws Exception {
FakeHttp.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body"));
FakeHttp.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri",
new TestHttpResponse(200, "a cheery response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a happy response body");
}
@Test
public void shouldReturnRequestsByRule() throws Exception {
FakeHttp.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri",
new TestHttpResponse(200, "a cheery response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a cheery response body");
}
@Test
public void shouldReturnRequestsByRule_MatchingMethod() throws Exception {
FakeHttp.setDefaultHttpResponse(404, "no such page");
FakeHttp.addHttpResponseRule(HttpPost.METHOD_NAME, "http://some.uri",
new TestHttpResponse(200, "a cheery response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(404);
}
@Test
public void shouldReturnRequestsByRule_AnyMethod() throws Exception {
FakeHttp.addHttpResponseRule(
"http://some.uri", new TestHttpResponse(200, "a cheery response body"));
HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(getResponse);
assertThat(getResponse.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(getResponse)).isEqualTo("a cheery response body");
HttpResponse postResponse =
requestDirector.execute(null, new HttpPost("http://some.uri"), null);
assertNotNull(postResponse);
assertThat(postResponse.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(postResponse)).isEqualTo("a cheery response body");
}
@Test
public void shouldReturnRequestsByRule_KeepsTrackOfOpenContentStreams() throws Exception {
TestHttpResponse testHttpResponse = new TestHttpResponse(200, "a cheery response body");
FakeHttp.addHttpResponseRule("http://some.uri", testHttpResponse);
assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isTrue();
HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
InputStream getResponseStream = getResponse.getEntity().getContent();
assertThat(CharStreams.toString(new InputStreamReader(getResponseStream, UTF_8)))
.isEqualTo("a cheery response body");
assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse();
HttpResponse postResponse =
requestDirector.execute(null, new HttpPost("http://some.uri"), null);
InputStream postResponseStream = postResponse.getEntity().getContent();
assertThat(CharStreams.toString(new InputStreamReader(postResponseStream, UTF_8)))
.isEqualTo("a cheery response body");
assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse();
getResponseStream.close();
assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isFalse();
postResponseStream.close();
assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed()).isTrue();
}
@Test
public void shouldReturnRequestsByRule_WithTextResponse() throws Exception {
FakeHttp.addHttpResponseRule("http://some.uri", "a cheery response body");
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a cheery response body");
}
@Test
public void clearHttpResponseRules_shouldRemoveAllRules() throws Exception {
FakeHttp.addHttpResponseRule("http://some.uri", "a cheery response body");
FakeHttp.clearHttpResponseRules();
FakeHttp.addHttpResponseRule("http://some.uri", "a gloomy response body");
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a gloomy response body");
}
@Test
public void clearPendingHttpResponses() throws Exception {
FakeHttp.addPendingHttpResponse(200, "earlier");
FakeHttp.clearPendingHttpResponses();
FakeHttp.addPendingHttpResponse(500, "later");
HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(500);
assertThat(getStringContent(response)).isEqualTo("later");
}
@Test
public void shouldReturnRequestsByRule_WithCustomRequestMatcher() throws Exception {
FakeHttp.setDefaultHttpResponse(404, "no such page");
FakeHttp.addHttpResponseRule(
request -> request.getRequestLine().getUri().equals("http://matching.uri"),
new TestHttpResponse(200, "a cheery response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://matching.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a cheery response body");
response = requestDirector.execute(null, new HttpGet("http://non-matching.uri"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(404);
assertThat(getStringContent(response)).isEqualTo("no such page");
}
@Test
public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a happy response body");
}
@Test
public void shouldHandleMultipleInvocations() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
FakeHttp.addPendingHttpResponse(201, "another happy response body");
HttpResponse response1 = requestDirector.execute(null, new HttpGet("http://example.com"), null);
HttpResponse response2 = requestDirector.execute(null, new HttpGet("www.example.com"), null);
assertThat(response1.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response1)).isEqualTo("a happy response body");
assertThat(response2.getStatusLine().getStatusCode()).isEqualTo(201);
assertThat(getStringContent(response2)).isEqualTo("another happy response body");
}
@Test
public void shouldHandleMultipleInvocationsOfExecute() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
FakeHttp.addPendingHttpResponse(201, "another happy response body");
requestDirector.execute(null, new HttpGet("http://example.com"), null);
requestDirector.execute(null, new HttpGet("www.example.com"), null);
HttpUriRequest request1 = (HttpUriRequest) FakeHttp.getSentHttpRequest(0);
assertThat(request1.getMethod()).isEqualTo(HttpGet.METHOD_NAME);
assertThat(request1.getURI()).isEqualTo(URI.create("http://example.com"));
HttpUriRequest request2 = (HttpUriRequest) FakeHttp.getSentHttpRequest(1);
assertThat(request2.getMethod()).isEqualTo(HttpGet.METHOD_NAME);
assertThat(request2.getURI()).isEqualTo(URI.create("www.example.com"));
}
@Test
public void shouldRejectUnexpectedCallsToExecute() throws Exception {
try {
requestDirector.execute(null, new HttpGet("http://example.com"), null);
fail();
} catch (RuntimeException expected) {
assertThat(expected.getMessage())
.isEqualTo(
"Unexpected call to execute, no pending responses are available. "
+ "See Robolectric.addPendingResponse(). Request was: GET http://example.com");
}
}
@Test
public void shouldRecordExtendedRequestData() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
HttpGet httpGet = new HttpGet("http://example.com");
requestDirector.execute(null, httpGet, null);
assertSame(FakeHttp.getSentHttpRequestInfo(0).getHttpRequest(), httpGet);
ConnectionKeepAliveStrategy strategy =
shadowOf((DefaultRequestDirector) FakeHttp.getSentHttpRequestInfo(0).getRequestDirector())
.getConnectionKeepAliveStrategy();
assertSame(strategy, connectionKeepAliveStrategy);
}
@Test
public void getNextSentHttpRequestInfo_shouldRemoveHttpRequestInfos() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
HttpGet httpGet = new HttpGet("http://example.com");
requestDirector.execute(null, httpGet, null);
assertSame(FakeHttp.getNextSentHttpRequestInfo().getHttpRequest(), httpGet);
assertNull(FakeHttp.getNextSentHttpRequestInfo());
}
@Test
public void getNextSentHttpRequest_shouldRemoveHttpRequests() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
HttpGet httpGet = new HttpGet("http://example.com");
requestDirector.execute(null, httpGet, null);
assertSame(FakeHttp.getNextSentHttpRequest(), httpGet);
assertNull(FakeHttp.getNextSentHttpRequest());
}
@Test
public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception {
FakeHttp.addPendingHttpResponse(200, "OK", new BasicHeader("Content-Type", "text/plain"));
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet("http://www.nowhere.org"));
assertThat(((HttpUriRequest) FakeHttp.getSentHttpRequest(0)).getURI())
.isEqualTo(URI.create("http://www.nowhere.org"));
Assert.assertNotNull(response);
String responseStr = new BasicResponseHandler().handleResponse(response);
Assert.assertEquals("OK", responseStr);
}
@Test
public void shouldFindLastRequestMade() throws Exception {
FakeHttp.addPendingHttpResponse(200, "a happy response body");
FakeHttp.addPendingHttpResponse(200, "a happy response body");
FakeHttp.addPendingHttpResponse(200, "a happy response body");
DefaultHttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://www.first.org"));
client.execute(new HttpGet("http://www.second.org"));
client.execute(new HttpGet("http://www.third.org"));
assertThat(((HttpUriRequest) FakeHttp.getLatestSentHttpRequest()).getURI())
.isEqualTo(URI.create("http://www.third.org"));
}
@Test
public void shouldSupportConnectionTimeoutWithExceptions() throws Exception {
FakeHttp.setDefaultHttpResponse(new TestHttpResponse() {
@Override
public HttpParams getParams() {
HttpParams httpParams = super.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, -1);
return httpParams;
}
});
DefaultHttpClient client = new DefaultHttpClient();
try {
client.execute(new HttpGet("http://www.nowhere.org"));
} catch (ConnectTimeoutException x) {
return;
}
fail("Exception should have been thrown");
}
@Test
public void shouldSupportSocketTimeoutWithExceptions() throws Exception {
FakeHttp.setDefaultHttpResponse(new TestHttpResponse() {
@Override
public HttpParams getParams() {
HttpParams httpParams = super.getParams();
HttpConnectionParams.setSoTimeout(httpParams, -1);
return httpParams;
}
});
DefaultHttpClient client = new DefaultHttpClient();
try {
client.execute(new HttpGet("http://www.nowhere.org"));
} catch (ConnectTimeoutException x) {
return;
}
fail("Exception should have been thrown");
}
@Test
public void shouldSupportRealHttpRequests() {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
DefaultHttpClient client = new DefaultHttpClient();
assertThrows(
IOException.class,
() ->
client.execute(new HttpGet("http://www.this-host-should-not-exist-123456790.org:999")));
}
@Test
public void shouldSupportRealHttpRequestsAddingRequestInfo() throws Exception {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
DefaultHttpClient client = new DefaultHttpClient();
// it's really bad to depend on an external server in order to get a test pass,
// but this test is about making sure that we can intercept calls to external servers
// so, I think that in this specific case, it's appropriate...
client.execute(new HttpGet("http://google.com"));
assertNotNull(FakeHttp.getFakeHttpLayer().getLastSentHttpRequestInfo());
assertNotNull(FakeHttp.getFakeHttpLayer().getLastHttpResponse());
}
@Test
public void realHttpRequestsShouldMakeContentDataAvailable() throws Exception {
FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
FakeHttp.getFakeHttpLayer().interceptResponseContent(true);
DefaultHttpClient client = new DefaultHttpClient();
client.execute(new HttpGet("http://google.com"));
byte[] cachedContent = FakeHttp.getFakeHttpLayer().getHttpResposeContentList().get(0);
assertThat(cachedContent.length).isNotEqualTo(0);
InputStream content =
FakeHttp.getFakeHttpLayer().getLastHttpResponse().getEntity().getContent();
BufferedReader contentReader = new BufferedReader(new InputStreamReader(content, UTF_8));
String firstLineOfContent = contentReader.readLine();
assertThat(firstLineOfContent).contains("Google");
BufferedReader cacheReader =
new BufferedReader(new InputStreamReader(new ByteArrayInputStream(cachedContent), UTF_8));
String firstLineOfCachedContent = cacheReader.readLine();
assertThat(firstLineOfCachedContent).isEqualTo(firstLineOfContent);
}
@Test
public void shouldReturnResponseFromHttpResponseGenerator() throws Exception {
FakeHttp.addPendingHttpResponse(request -> new TestHttpResponse(200, "a happy response body"));
HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);
assertNotNull(response);
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
assertThat(getStringContent(response)).isEqualTo("a happy response body");
}
private static String getStringContent(HttpResponse response) throws IOException {
return CharStreams.toString(new InputStreamReader(response.getEntity().getContent(), UTF_8));
}
}