Java 类org.robolectric.tester.org.apache.http.HttpRequestInfo 实例源码

项目:FullRobolectricTestSample    文件:ShadowDefaultRequestDirector.java   
@Implementation
public HttpResponse execute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws HttpException, IOException {
  if (Robolectric.getFakeHttpLayer().isInterceptingHttpRequests()) {
    return Robolectric.getFakeHttpLayer().emulateRequest(httpHost, httpRequest, httpContext, realObject);
  } else {
    Robolectric.getFakeHttpLayer().addRequestInfo(new HttpRequestInfo(httpRequest, httpHost, httpContext, redirector));
    HttpResponse response = redirector.execute(httpHost, httpRequest, httpContext);

    if (Robolectric.getFakeHttpLayer().isInterceptingResponseContent()) {
      interceptResponseContent(response);
    }

    Robolectric.getFakeHttpLayer().addHttpResponse(response);
    return response;
  }
}
项目:robolectric-ant-sample    文件:ApiGatewayTest.java   
@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"));
}
项目:Inside_Android_Testing    文件:ApiGatewayTest.java   
@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"));
}
项目:robolectric-ant-sample    文件:ApiGatewayTest.java   
@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"));
}
项目:robolectric-ant-sample    文件:HttpTest.java   
@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"));
}
项目:Inside_Android_Testing    文件:ApiGatewayTest.java   
@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"));
}
项目:Inside_Android_Testing    文件:HttpTest.java   
@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"));
}
项目:FullRobolectricTestSample    文件:ShadowDefaultRequestDirector.java   
public static HttpRequestInfo getLatestSentHttpRequestInfo() {
  int requestCount = Robolectric.getFakeHttpLayer().getSentHttpRequestInfos().size();
  return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(requestCount - 1);
}
项目:FullRobolectricTestSample    文件:ShadowDefaultRequestDirector.java   
/**
 * @deprecated Use {@link Robolectric#getSentHttpRequest(int)} instead.)
 */
public static HttpRequestInfo getSentHttpRequestInfo(int index) {
  return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(index);
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
/**
 * Accessor to obtain metadata for an HTTP request made during the current test in the order in which they were made.
 *
 * @param index index of the request to retrieve.
 * @return the requested request metadata.
 */
public static HttpRequestInfo getSentHttpRequestInfo(int index) {
  return getFakeHttpLayer().getSentHttpRequestInfo(index);
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
/**
 * Accessor to obtain HTTP requests made during the current test in the order in which they were made.
 *
 * @return the requested request or null if there are none.
 */
public static HttpRequest getNextSentHttpRequest() {
  HttpRequestInfo httpRequestInfo = getFakeHttpLayer().getNextSentHttpRequestInfo();
  return httpRequestInfo == null ? null : httpRequestInfo.getHttpRequest();
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
/**
 * Accessor to obtain metadata for an HTTP request made during the current test in the order in which they were made.
 *
 * @return the requested request metadata or null if there are none.
 */
public static HttpRequestInfo getNextSentHttpRequestInfo() {
  return getFakeHttpLayer().getNextSentHttpRequestInfo();
}