@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; } }
@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")); }
@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")); }
@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")); }
public static HttpRequestInfo getLatestSentHttpRequestInfo() { int requestCount = Robolectric.getFakeHttpLayer().getSentHttpRequestInfos().size(); return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(requestCount - 1); }
/** * @deprecated Use {@link Robolectric#getSentHttpRequest(int)} instead.) */ public static HttpRequestInfo getSentHttpRequestInfo(int index) { return Robolectric.getFakeHttpLayer().getSentHttpRequestInfo(index); }
/** * 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); }
/** * 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(); }
/** * 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(); }