10dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinpackage com.xtremelabs.robolectric.shadows; 20dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin 3cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultzimport com.xtremelabs.robolectric.Robolectric; 40dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinimport com.xtremelabs.robolectric.WithTestDefaultsRunner; 52cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwinimport com.xtremelabs.robolectric.tester.org.apache.http.FakeHttpLayer; 62cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwinimport com.xtremelabs.robolectric.tester.org.apache.http.RequestMatcher; 72cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwinimport com.xtremelabs.robolectric.tester.org.apache.http.TestHttpResponse; 8c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultzimport com.xtremelabs.robolectric.util.Strings; 96f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sancheimport junit.framework.Assert; 10bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwinimport org.apache.http.HttpRequest; 110dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinimport org.apache.http.HttpResponse; 120dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinimport org.apache.http.client.methods.HttpGet; 137cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwinimport org.apache.http.client.methods.HttpPost; 145e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultzimport org.apache.http.client.methods.HttpUriRequest; 1588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.conn.ConnectTimeoutException; 16b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultzimport org.apache.http.conn.ConnectionKeepAliveStrategy; 1739a416145e85c7008805ef493b2d61f66d669158Tyler Schultzimport org.apache.http.impl.client.BasicResponseHandler; 1839a416145e85c7008805ef493b2d61f66d669158Tyler Schultzimport org.apache.http.impl.client.DefaultHttpClient; 19cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultzimport org.apache.http.impl.client.DefaultRequestDirector; 2039a416145e85c7008805ef493b2d61f66d669158Tyler Schultzimport org.apache.http.message.BasicHeader; 2188748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.params.HttpConnectionParams; 2288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuwimport org.apache.http.params.HttpParams; 23b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultzimport org.apache.http.protocol.HttpContext; 24c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultzimport org.junit.After; 25c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultzimport org.junit.Before; 260dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinimport org.junit.Test; 270dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwinimport org.junit.runner.RunWith; 280dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin 29ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkelimport java.io.IOException; 309088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richardimport java.io.InputStream; 315e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultzimport java.net.URI; 325e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz 33b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf; 34c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultzimport static org.hamcrest.core.IsEqual.equalTo; 35c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultzimport static org.junit.Assert.*; 360dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin 370dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin@RunWith(WithTestDefaultsRunner.class) 38cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultzpublic class DefaultRequestDirectorTest { 39cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz private DefaultRequestDirector requestDirector; 40b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz private ConnectionKeepAliveStrategy connectionKeepAliveStrategy; 41c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 42c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz @Before 43c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz public void setUp_EnsureStaticStateIsReset() { 44f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin FakeHttpLayer fakeHttpLayer = Robolectric.getFakeHttpLayer(); 452cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin assertFalse(fakeHttpLayer.hasPendingResponses()); 462cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin assertFalse(fakeHttpLayer.hasRequestInfos()); 472cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin assertFalse(fakeHttpLayer.hasResponseRules()); 482cdde11355e7c249e83a8b7aaabae5977bbc3e49Phil Goodwin assertNull(fakeHttpLayer.getDefaultResponse()); 49f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin 50b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() { 51b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz @Override public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) { 52b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz return 0; 53b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz } 54b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz }; 55b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz requestDirector = new DefaultRequestDirector(null, null, null, connectionKeepAliveStrategy, null, null, null, null, null, null, null, null); 56c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 57c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 58c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz @After 59c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz public void tearDown_EnsureStaticStateIsReset() throws Exception { 60b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(200, "a happy response body"); 61c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 62c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 630dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin @Test 640dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin public void shouldGetHttpResponseFromExecute() throws Exception { 65b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body")); 66cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); 67c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 68cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz assertNotNull(response); 69c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 70c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); 71c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 72c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 73c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz @Test 74bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin public void shouldPreferPendingResponses() throws Exception { 75bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin Robolectric.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body")); 76bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 77f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri", 78bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin new TestHttpResponse(200, "a cheery response body")); 79bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 80bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 81bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 82bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 83bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 84bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); 85bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 86bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 87bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Test 88bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin public void shouldReturnRequestsByRule() throws Exception { 89f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin Robolectric.addHttpResponseRule(HttpGet.METHOD_NAME, "http://some.uri", 90bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin new TestHttpResponse(200, "a cheery response body")); 91bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 92bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 93bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 94bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 95bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 96bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a cheery response body")); 97bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 98bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 99bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Test 1007cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin public void shouldReturnRequestsByRule_MatchingMethod() throws Exception { 101d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin Robolectric.setDefaultHttpResponse(404, "no such page"); 102f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin Robolectric.addHttpResponseRule(HttpPost.METHOD_NAME, "http://some.uri", 103bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin new TestHttpResponse(200, "a cheery response body")); 104bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 105bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 106bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 107bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 1087cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); 1097cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin } 1107cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin 1117cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin @Test 1127cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin public void shouldReturnRequestsByRule_AnyMethod() throws Exception { 113f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin Robolectric.addHttpResponseRule("http://some.uri", new TestHttpResponse(200, "a cheery response body")); 1147cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin 1157cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 1167cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertNotNull(getResponse); 1177cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertThat(getResponse.getStatusLine().getStatusCode(), equalTo(200)); 1187cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertThat(Strings.fromStream(getResponse.getEntity().getContent()), equalTo("a cheery response body")); 1197cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin 1207cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin HttpResponse postResponse = requestDirector.execute(null, new HttpPost("http://some.uri"), null); 1217cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertNotNull(postResponse); 1227cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertThat(postResponse.getStatusLine().getStatusCode(), equalTo(200)); 1237cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertThat(Strings.fromStream(postResponse.getEntity().getContent()), equalTo("a cheery response body")); 124bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 125bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 126bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Test 1279088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard public void shouldReturnRequestsByRule_KeepsTrackOfOpenContentStreams() throws Exception { 1289088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard TestHttpResponse testHttpResponse = new TestHttpResponse(200, "a cheery response body"); 1299088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard Robolectric.addHttpResponseRule("http://some.uri", testHttpResponse); 1309088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1319088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(true)); 1329088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1339088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 1349088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard InputStream getResponseStream = getResponse.getEntity().getContent(); 1359088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(Strings.fromStream(getResponseStream), equalTo("a cheery response body")); 1369088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); 1379088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1389088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard HttpResponse postResponse = requestDirector.execute(null, new HttpPost("http://some.uri"), null); 1399088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard InputStream postResponseStream = postResponse.getEntity().getContent(); 1409088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(Strings.fromStream(postResponseStream), equalTo("a cheery response body")); 1419088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); 1429088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1439088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard getResponseStream.close(); 1449088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); 1459088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1469088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard postResponseStream.close(); 1479088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(true)); 1489088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard } 1499088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard 1509088d97a59b3864cc5f0fbe87dc72568619e27ebRyan Richard @Test 151bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin public void shouldReturnRequestsByRule_WithTextResponse() throws Exception { 152f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin Robolectric.addHttpResponseRule("http://some.uri", "a cheery response body"); 153bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 154bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 155bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 156bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 157bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 158bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a cheery response body")); 159bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 160bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 161bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Test 162b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson public void clearHttpResponseRules_shouldRemoveAllRules() throws Exception { 163b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson Robolectric.addHttpResponseRule("http://some.uri", "a cheery response body"); 164b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson Robolectric.clearHttpResponseRules(); 165b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson Robolectric.addHttpResponseRule("http://some.uri", "a gloomy response body"); 166b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson 167b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 168b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson 169b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson assertNotNull(response); 170b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 171b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a gloomy response body")); 172b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson } 173b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson 174b539795cdd8f6be1b0b01527905a31faea03242cRob Dickerson @Test 17554c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala public void clearPendingHttpResponses() throws Exception { 17654c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala Robolectric.addPendingHttpResponse(200, "earlier"); 17754c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala Robolectric.clearPendingHttpResponses(); 17854c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala Robolectric.addPendingHttpResponse(500, "later"); 17954c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala 18054c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); 18154c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala 18254c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala assertNotNull(response); 18354c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala assertThat(response.getStatusLine().getStatusCode(), equalTo(500)); 18454c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("later")); 18554c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala } 186ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel 18754c8f286408ad7e791f9468dcdc2e18cff1681e4Glenn Jahnke & Rick Kawala @Test 188bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin public void shouldReturnRequestsByRule_WithCustomRequestMatcher() throws Exception { 189d76cb3959b31f5def82d0476cc114cc5da06a7a1Phil Goodwin Robolectric.setDefaultHttpResponse(404, "no such page"); 190bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 1914105051dc6b4e1d67766d1ad886bd3b191f8a68fChristian Williams Robolectric.addHttpResponseRule(new RequestMatcher() { 192bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Override public boolean matches(HttpRequest request) { 193bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin return request.getRequestLine().getUri().equals("http://matching.uri"); 194bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 195bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin }, new TestHttpResponse(200, "a cheery response body")); 196bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 197bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin HttpResponse response = requestDirector.execute(null, new HttpGet("http://matching.uri"), null); 198bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 199bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 200bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a cheery response body")); 201bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 202bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin response = requestDirector.execute(null, new HttpGet("http://non-matching.uri"), null); 203bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertNotNull(response); 204bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); 205bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("no such page")); 206bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin } 207bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin 208bd9537bd36434865d4d25ef332bd388d39a51e34Christian Williams & Phil Goodwin @Test 209c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception { 210b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(200, "a happy response body"); 211cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); 212c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 213c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 214c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); 215c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 216cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz 217c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz @Test 218c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz public void shouldHandleMultipleInvocations() throws Exception { 219b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(200, "a happy response body"); 220b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(201, "another happy response body"); 221cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz 222cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz HttpResponse response1 = requestDirector.execute(null, new HttpGet("http://example.com"), null); 223cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz HttpResponse response2 = requestDirector.execute(null, new HttpGet("www.example.com"), null); 224cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz 225c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(response1.getStatusLine().getStatusCode(), equalTo(200)); 226c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(Strings.fromStream(response1.getEntity().getContent()), equalTo("a happy response body")); 227c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 228c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(response2.getStatusLine().getStatusCode(), equalTo(201)); 229c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz assertThat(Strings.fromStream(response2.getEntity().getContent()), equalTo("another happy response body")); 230c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 231c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz 232c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz @Test 2335e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz public void shouldHandleMultipleInvocationsOfExecute() throws Exception { 234b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(200, "a happy response body"); 235b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(201, "another happy response body"); 2365e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz 237cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz requestDirector.execute(null, new HttpGet("http://example.com"), null); 238cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz requestDirector.execute(null, new HttpGet("www.example.com"), null); 2395e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz 240b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz HttpUriRequest request1 = (HttpUriRequest) Robolectric.getSentHttpRequest(0); 2415e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz assertThat(request1.getMethod(), equalTo(HttpGet.METHOD_NAME)); 2425e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz assertThat(request1.getURI(), equalTo(URI.create("http://example.com"))); 2435e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz 244b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz HttpUriRequest request2 = (HttpUriRequest) Robolectric.getSentHttpRequest(1); 2455e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz assertThat(request2.getMethod(), equalTo(HttpGet.METHOD_NAME)); 2465e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz assertThat(request2.getURI(), equalTo(URI.create("www.example.com"))); 2475e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz } 2485e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz 2495e8d6561f4ca9bff6940c87d8569e08be76348ecPhil Goodwin & Tyler Schultz @Test 250c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz public void shouldRejectUnexpectedCallsToExecute() throws Exception { 251c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz try { 252cf8438f6160ad20fcd8afc57485b35166bec4a2bPhil Goodwin & Tyler Schultz requestDirector.execute(null, new HttpGet("http://example.com"), null); 253c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz fail(); 254c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } catch (RuntimeException expected) { 255b188febae0b24463565d9b6157bd43e0ff22a269David Farber & Ryan Richard assertThat(expected.getMessage(), equalTo("Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse(). Request was: GET http://example.com")); 256c95377d49257364984344908f301574ebe8d4d9cPhil Goodwin & Tyler Schultz } 2570dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin } 258b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz 259b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz @Test 260b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz public void shouldRecordExtendedRequestData() throws Exception { 261b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz Robolectric.addPendingHttpResponse(200, "a happy response body"); 262b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz HttpGet httpGet = new HttpGet("http://example.com"); 263b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz requestDirector.execute(null, httpGet, null); 264b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz 2657cf93e04b5b478b64f757a4b86ed75e28ae612c1Christian Williams & Phil Goodwin assertSame(Robolectric.getSentHttpRequestInfo(0).getHttpRequest(), httpGet); 266f91a118112ae13d39251dd3c09f1d85d33283ba1Christian Williams & Phil Goodwin ConnectionKeepAliveStrategy strategy = shadowOf((DefaultRequestDirector) Robolectric.getSentHttpRequestInfo(0).getRequestDirector()).getConnectionKeepAliveStrategy(); 267b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz assertSame(strategy, connectionKeepAliveStrategy); 268b2fea8dc6ecf427c1e620148946b6fd83a0bd210Phil Goodwin & Tyler Schultz } 269ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel 2706f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche @Test 2716f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception { 27250462c4dff2d5f6aa1f5f9683935f5f0270c8a2dJan Berkel Robolectric.addPendingHttpResponse(200, "OK", new BasicHeader("Content-Type", "text/plain")); 2736f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche 2746f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche DefaultHttpClient client = new DefaultHttpClient(); 2756f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche HttpResponse response = client.execute(new HttpGet("http://www.nowhere.org")); 2766f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche 2776f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche assertThat(((HttpUriRequest) Robolectric.getSentHttpRequest(0)).getURI(), 27839a416145e85c7008805ef493b2d61f66d669158Tyler Schultz equalTo(URI.create("http://www.nowhere.org"))); 2796f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche 2806f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche Assert.assertNotNull(response); 2816f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche String responseStr = new BasicResponseHandler().handleResponse(response); 2826f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche Assert.assertEquals("OK", responseStr); 2836f34840c49a26f54f62ead83509e3f64bed7e0cfNeal Sanche } 28488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 28588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw @Test 286f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala public void shouldFindLastRequestMade() throws Exception { 287f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala Robolectric.addPendingHttpResponse(200, "a happy response body"); 288f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala Robolectric.addPendingHttpResponse(200, "a happy response body"); 289f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala Robolectric.addPendingHttpResponse(200, "a happy response body"); 290f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala 291f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala DefaultHttpClient client = new DefaultHttpClient(); 292f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala client.execute(new HttpGet("http://www.first.org")); 293f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala client.execute(new HttpGet("http://www.second.org")); 294f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala client.execute(new HttpGet("http://www.third.org")); 295f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala 296f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala assertThat(((HttpUriRequest) Robolectric.getLatestSentHttpRequest()).getURI(), 297f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala equalTo(URI.create("http://www.third.org"))); 298f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala } 299f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala 300f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala 301f7591628cda8719684f21aa809a5e4ddc634ec56Phil Goodwin & Rick Kawala @Test 30288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw public void shouldSupportConnectionTimeoutWithExceptions() throws Exception { 30388748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw Robolectric.setDefaultHttpResponse(new TestHttpResponse() { 30488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw @Override 30588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw public HttpParams getParams() { 30688748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw HttpParams httpParams = super.getParams(); 30788748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw HttpConnectionParams.setConnectionTimeout(httpParams, -1); 30888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw return httpParams; 30988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 31088748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw }); 31188748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 31288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw DefaultHttpClient client = new DefaultHttpClient(); 31388748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw try { 31488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw client.execute(new HttpGet("http://www.nowhere.org")); 31588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } catch (ConnectTimeoutException x) { 31688748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw return; 31788748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 31888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 31988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw fail("Exception should have been thrown"); 32088748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 32188748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 32288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw @Test 32388748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw public void shouldSupportSocketTimeoutWithExceptions() throws Exception { 32488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw Robolectric.setDefaultHttpResponse(new TestHttpResponse() { 32588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw @Override 32688748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw public HttpParams getParams() { 32788748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw HttpParams httpParams = super.getParams(); 32888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw HttpConnectionParams.setSoTimeout(httpParams, -1); 32988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw return httpParams; 33088748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 33188748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw }); 33288748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 33388748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw DefaultHttpClient client = new DefaultHttpClient(); 33488748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw try { 33588748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw client.execute(new HttpGet("http://www.nowhere.org")); 33688748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } catch (ConnectTimeoutException x) { 33788748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw return; 33888748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 33988748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw 34088748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw fail("Exception should have been thrown"); 34188748bc70223e557208926eca76033a38dcd044bLenny Turetsky & Tim Labeeuw } 342ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel 343ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel @Test(expected = IOException.class) 344ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel public void shouldSupportRealHttpRequests() throws Exception { 345ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel Robolectric.getFakeHttpLayer().interceptHttpRequests(false); 346ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel DefaultHttpClient client = new DefaultHttpClient(); 3470a362c553c20d1213806246a5dd5488218444eb1Aaron VonderHaar client.execute(new HttpGet("http://www.this-host-should-not-exist-123456790.org:999")); 348ef24e85f87c30d8d1cbe6f7ca0c29a11bd7e4be4Jan Berkel } 349a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur 350a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur @Test 351a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur public void shouldReturnResponseFromHttpResponseGenerator() throws Exception { 352a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur Robolectric.addPendingHttpResponse(new HttpResponseGenerator() { 353a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur @Override 354a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur public HttpResponse getResponse(HttpRequest request) { 355a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur return new TestHttpResponse(200, "a happy response body"); 356a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur } 357a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur }); 358a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); 359a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur 360a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur assertNotNull(response); 361a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); 362a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); 363a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur } 364a34c6ada3c3b41e5b04ab09db7ddc346ac0b3ab9Aaron VonderHaar & Amrit Thakur 3650dde37339060dd5d0681552c3e98f22b22988e15Phil Goodwin} 366