@Test public void shouldReturnRequestsByRule_MatchingMethod() throws Exception { Robolectric.setDefaultHttpResponse(404, "no such page"); Robolectric.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(), equalTo(404)); }
@Test public void shouldSupportSocketTimeoutWithExceptions() throws Exception { Robolectric.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 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")); }