@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 shouldFindLastRequestMade() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    Robolectric.addPendingHttpResponse(200, "a happy response body");

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(new HttpGet("http://www.first.org"));
    client.execute(new HttpGet("http://www.second.org"));
    client.execute(new HttpGet("http://www.third.org"));

    assertThat(
        ((HttpUriRequest) Robolectric.getLatestSentHttpRequest()).getURI(),
        equalTo(URI.create("http://www.third.org")));
  }
  @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 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 getNextSentHttpRequest_shouldRemoveHttpRequests() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    HttpGet httpGet = new HttpGet("http://example.com");
    requestDirector.execute(null, httpGet, null);

    assertSame(Robolectric.getNextSentHttpRequest(), httpGet);
    assertNull(Robolectric.getNextSentHttpRequest());
  }
  @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 shouldRecordExtendedRequestData() throws Exception {
    Robolectric.addPendingHttpResponse(200, "a happy response body");
    HttpGet httpGet = new HttpGet("http://example.com");
    requestDirector.execute(null, httpGet, null);

    assertSame(Robolectric.getSentHttpRequestInfo(0).getHttpRequest(), httpGet);
    ConnectionKeepAliveStrategy strategy =
        shadowOf(
                (DefaultRequestDirector) Robolectric.getSentHttpRequestInfo(0).getRequestDirector())
            .getConnectionKeepAliveStrategy();
    assertSame(strategy, connectionKeepAliveStrategy);
  }
  @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);
  }
  @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"));
  }
  @Test
  public void shouldReturnResponseFromHttpResponseGenerator() throws Exception {
    Robolectric.addPendingHttpResponse(
        new HttpResponseGenerator() {
          @Override
          public HttpResponse getResponse(HttpRequest request) {
            return new TestHttpResponse(200, "a happy response body");
          }
        });
    HttpResponse response = requestDirector.execute(null, new HttpGet("http://example.com"), null);

    assertNotNull(response);
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    assertThat(
        Strings.fromStream(response.getEntity().getContent()), equalTo("a happy response body"));
  }
Exemplo n.º 11
0
  @Test
  public void testAsyncTasks() {
    Robolectric.getBackgroundScheduler().pause();

    TestRequest request = new TestRequest();
    api.makeCall(request, responseCallbacks);

    // setting pending http response
    Robolectric.addPendingHttpResponse(200, "Test Response");

    // executing task
    Robolectric.getBackgroundScheduler().runOneTask();

    HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);
    HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest();

    // Testing URL
    assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.disney.com"));

    // Testing Headers
    assertThat(sentHttpRequest.getHeaders("mickey")[0].getValue(), equalTo("mouse"));
  }
 @After
 public void tearDown_EnsureStaticStateIsReset() throws Exception {
   Robolectric.addPendingHttpResponse(200, "a happy response body");
 }