@Test public void shouldHandleMultipleInvocations() throws Exception { Robolectric.addPendingHttpResponse(200, "a happy response body"); Robolectric.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(), equalTo(200)); assertThat( Strings.fromStream(response1.getEntity().getContent()), equalTo("a happy response body")); assertThat(response2.getStatusLine().getStatusCode(), equalTo(201)); assertThat( Strings.fromStream(response2.getEntity().getContent()), equalTo("another happy response body")); }
@Test public void shouldGetHttpResponseFromExecuteSimpleApi() throws Exception { Robolectric.addPendingHttpResponse(200, "a happy response body"); HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null); assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); assertThat( Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); }
@Test public void shouldReturnRequestsByRule_WithTextResponse() throws Exception { Robolectric.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(), equalTo(200)); assertThat( Strings.fromStream(response.getEntity().getContent()), equalTo("a cheery response body")); }
@Test public void clearPendingHttpResponses() throws Exception { Robolectric.addPendingHttpResponse(200, "earlier"); Robolectric.clearPendingHttpResponses(); Robolectric.addPendingHttpResponse(500, "later"); HttpResponse response = requestDirector.execute(null, new HttpGet("http://some.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode(), equalTo(500)); assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("later")); }
@Test public void shouldReturnRequestsByRule_WithCustomRequestMatcher() throws Exception { Robolectric.setDefaultHttpResponse(404, "no such page"); Robolectric.addHttpResponseRule( new RequestMatcher() { @Override public boolean matches(HttpRequest request) { return 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(), equalTo(200)); assertThat( Strings.fromStream(response.getEntity().getContent()), equalTo("a cheery response body")); response = requestDirector.execute(null, new HttpGet("http://non-matching.uri"), null); assertNotNull(response); assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); assertThat(Strings.fromStream(response.getEntity().getContent()), equalTo("no such page")); }
@Test public void shouldReturnRequestsByRule_KeepsTrackOfOpenContentStreams() throws Exception { TestHttpResponse testHttpResponse = new TestHttpResponse(200, "a cheery response body"); Robolectric.addHttpResponseRule("http://some.uri", testHttpResponse); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(true)); HttpResponse getResponse = requestDirector.execute(null, new HttpGet("http://some.uri"), null); InputStream getResponseStream = getResponse.getEntity().getContent(); assertThat(Strings.fromStream(getResponseStream), equalTo("a cheery response body")); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); HttpResponse postResponse = requestDirector.execute(null, new HttpPost("http://some.uri"), null); InputStream postResponseStream = postResponse.getEntity().getContent(); assertThat(Strings.fromStream(postResponseStream), equalTo("a cheery response body")); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); getResponseStream.close(); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(false)); postResponseStream.close(); assertThat(testHttpResponse.entityContentStreamsHaveBeenClosed(), equalTo(true)); }
@Test public void shouldPreferPendingResponses() throws Exception { Robolectric.addPendingHttpResponse(new TestHttpResponse(200, "a happy response body")); Robolectric.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(), equalTo(200)); assertThat( Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body")); }