Java Code Examples for org.robolectric.Robolectric#addPendingHttpResponse()

The following examples show how to use org.robolectric.Robolectric#addPendingHttpResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiGatewayTest.java    From Inside_Android_Testing with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldMakeRemoteGetCalls() {
    Robolectric.getBackgroundScheduler().pause();

    TestGetRequest apiRequest = new TestGetRequest();
    apiGateway.makeRequest(apiRequest, responseCallbacks);

    Robolectric.addPendingHttpResponse(200, GENERIC_XML);

    Robolectric.getBackgroundScheduler().runOneTask();

    HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);
    HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest();
    assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com"));
    assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpGet.METHOD_NAME));

    assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar"));

    CredentialsProvider credentialsProvider =
            (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER);
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants"));
}
 
Example 2
Source File: ApiGatewayTest.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Test
public void dispatch_shouldCallOnFailureWhenXmlParseErrorOccurs() throws Exception {
    Robolectric.addPendingHttpResponse(200, "Invalid x-shmail");
    apiGateway.makeRequest(new TestGetRequest(), responseCallbacks);
    assertThat(responseCallbacks.successResponse, nullValue());
    assertThat(responseCallbacks.failureResponse, not(nullValue()));
    assertThat(responseCallbacks.onCompleteWasCalled, equalTo(true));
}
 
Example 3
Source File: ApiGatewayTest.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldMakeRemotePostCalls() throws Exception {
    Robolectric.getBackgroundScheduler().pause();

    TestPostRequest apiRequest = new TestPostRequest();
    apiGateway.makeRequest(apiRequest, responseCallbacks);

    Robolectric.addPendingHttpResponse(200, GENERIC_XML);

    Robolectric.getBackgroundScheduler().runOneTask();

    HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);
    HttpRequest sentHttpRequest = sentHttpRequestData.getHttpRequest();
    assertThat(sentHttpRequest.getRequestLine().getUri(), equalTo("www.example.com"));
    assertThat(sentHttpRequest.getRequestLine().getMethod(), equalTo(HttpPost.METHOD_NAME));

    assertThat(sentHttpRequest.getHeaders("foo")[0].getValue(), equalTo("bar"));

    InputStream contentStream = ((HttpPost) sentHttpRequest).getEntity().getContent();

    assertThat(Strings.fromStream(contentStream), CoreMatchers.equalTo("post body content"));

    CredentialsProvider credentialsProvider =
            (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER);
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), CoreMatchers.equalTo("spongebob"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), CoreMatchers.equalTo("squarepants"));
}
 
Example 4
Source File: HttpTest.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet_FormsCorrectRequest_noBasicAuth() throws Exception {
    Robolectric.addPendingHttpResponse(200, "OK");

    http.get("www.example.com", new HashMap<String, String>(), null, null);

    assertThat(((HttpUriRequest) Robolectric.getSentHttpRequest(0)).getURI(), equalTo(URI.create("www.example.com")));
}
 
Example 5
Source File: HttpTest.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet_FormsCorrectRequest_withBasicAuth() throws Exception {
    Robolectric.addPendingHttpResponse(200, "OK");
    http.get("www.example.com", new HashMap<String, String>(), "username", "password");
    HttpRequestInfo sentHttpRequestData = Robolectric.getSentHttpRequestInfo(0);

    CredentialsProvider credentialsProvider =
            (CredentialsProvider) sentHttpRequestData.getHttpContext().getAttribute(ClientContext.CREDS_PROVIDER);
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getUserPrincipal().getName(), equalTo("username"));
    assertThat(credentialsProvider.getCredentials(AuthScope.ANY).getPassword(), equalTo("password"));
}
 
Example 6
Source File: HttpTest.java    From Inside_Android_Testing with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReturnCorrectResponse() throws Exception {
    Robolectric.addPendingHttpResponse(666, "it's all cool");

    Http.Response response = http.get("www.example.com", new HashMap<String, String>(), null, null);

    assertThat(fromStream(response.getResponseBody()), equalTo("it's all cool"));
    assertThat(response.getStatusCode(), equalTo(666));
}
 
Example 7
Source File: ApiGatewayTest.java    From Inside_Android_Testing with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldDispatchUponReceivingResponse() throws Exception {
    Robolectric.getBackgroundScheduler().pause();
    Robolectric.getUiThreadScheduler().pause();

    Robolectric.addPendingHttpResponse(200, GENERIC_XML);

    apiGateway.makeRequest(new TestGetRequest(), responseCallbacks);
    Robolectric.getBackgroundScheduler().runOneTask();

    assertThat(responseCallbacks.successResponse, nullValue());

    Robolectric.getUiThreadScheduler().runOneTask();

    assertThat(asString(responseCallbacks.successResponse.getResponseDocument()), equalTo(GENERIC_XML));
}