@Test public void shouldHandleMultipleInvocationsOfExecute() throws Exception { Robolectric.addPendingHttpResponse(200, "a happy response body"); Robolectric.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) Robolectric.getSentHttpRequest(0); assertThat(request1.getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(request1.getURI(), equalTo(URI.create("http://example.com"))); HttpUriRequest request2 = (HttpUriRequest) Robolectric.getSentHttpRequest(1); assertThat(request2.getMethod(), equalTo(HttpGet.METHOD_NAME)); assertThat(request2.getURI(), equalTo(URI.create("www.example.com"))); }
@Test public void shouldSupportBasicResponseHandlerHandleResponse() throws Exception { Robolectric.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) Robolectric.getSentHttpRequest(0)).getURI(), equalTo(URI.create("http://www.nowhere.org"))); Assert.assertNotNull(response); String responseStr = new BasicResponseHandler().handleResponse(response); Assert.assertEquals("OK", responseStr); }