@Test
  public void clearHttpResponseRules_shouldRemoveAllRules() throws Exception {
    Robolectric.addHttpResponseRule("http://some.uri", "a cheery response body");
    Robolectric.clearHttpResponseRules();
    Robolectric.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(), equalTo(200));
    assertThat(
        Strings.fromStream(response.getEntity().getContent()), equalTo("a gloomy response body"));
  }
  @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 shouldReturnRequestsByRule() throws Exception {
    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 cheery response body"));
  }
  @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));
  }