Class FakeHttp

java.lang.Object
org.robolectric.shadows.httpclient.FakeHttp

public class FakeHttp extends Object
Collection of static methods used interact with HTTP requests / responses.
  • Constructor Details

    • FakeHttp

      public FakeHttp()
  • Method Details

    • addPendingHttpResponse

      public static void addPendingHttpResponse(int statusCode, String responseBody, org.apache.http.Header... headers)
      Sets up an HTTP response to be returned by calls to Apache's HttpClient implementers.
      Parameters:
      statusCode - the status code of the response
      responseBody - the body of the response
      headers - optional headers for the request
    • addPendingHttpResponseWithContentType

      @Deprecated @InlineMe(replacement="FakeHttp.getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody, contentType)", imports="org.robolectric.shadows.httpclient.FakeHttp") public static final void addPendingHttpResponseWithContentType(int statusCode, String responseBody, org.apache.http.Header contentType)
      Sets up an HTTP response to be returned by calls to Apache's HttpClient implementers.
      Parameters:
      statusCode - the status code of the response
      responseBody - the body of the response
      contentType - the contentType of the response
    • addPendingHttpResponse

      public static void addPendingHttpResponse(org.apache.http.HttpResponse httpResponse)
      Sets up an HTTP response to be returned by calls to Apache's HttpClient implementers.
      Parameters:
      httpResponse - the response
    • addPendingHttpResponse

      public static void addPendingHttpResponse(HttpResponseGenerator httpResponseGenerator)
      Sets up an HTTP response to be returned by calls to Apache's HttpClient implementers.
      Parameters:
      httpResponseGenerator - an HttpResponseGenerator that will provide responses
    • getSentHttpRequest

      public static org.apache.http.HttpRequest getSentHttpRequest(int index)
      Accessor to obtain HTTP requests made during the current test in the order in which they were made.
      Parameters:
      index - index of the request to retrieve.
      Returns:
      the requested request.
    • getLatestSentHttpRequest

      public static org.apache.http.HttpRequest getLatestSentHttpRequest()
    • httpRequestWasMade

      public static boolean httpRequestWasMade()
      Accessor to find out if HTTP requests were made during the current test.
      Returns:
      whether a request was made.
    • httpRequestWasMade

      public static boolean httpRequestWasMade(String uri)
    • getSentHttpRequestInfo

      public static HttpRequestInfo getSentHttpRequestInfo(int index)
      Accessor to obtain metadata for an HTTP request made during the current test in the order in which they were made.
      Parameters:
      index - index of the request to retrieve.
      Returns:
      the requested request metadata.
    • getNextSentHttpRequest

      public static org.apache.http.HttpRequest getNextSentHttpRequest()
      Accessor to obtain HTTP requests made during the current test in the order in which they were made.
      Returns:
      the requested request or null if there are none.
    • getNextSentHttpRequestInfo

      public static HttpRequestInfo getNextSentHttpRequestInfo()
      Accessor to obtain metadata for an HTTP request made during the current test in the order in which they were made.
      Returns:
      the requested request metadata or null if there are none.
    • addHttpResponseRule

      public static void addHttpResponseRule(String method, String uri, org.apache.http.HttpResponse response)
      Adds an HTTP response rule. The response will be returned when the rule is matched.
      Parameters:
      method - method to match.
      uri - uri to match.
      response - response to return when a match is found.
    • addHttpResponseRule

      public static void addHttpResponseRule(String uri, org.apache.http.HttpResponse response)
      Adds an HTTP response rule with a default method of GET. The response will be returned when the rule is matched.
      Parameters:
      uri - uri to match.
      response - response to return when a match is found.
    • addHttpResponseRule

      public static void addHttpResponseRule(String uri, String response)
      Adds an HTTP response rule. The response will be returned when the rule is matched.
      Parameters:
      uri - uri to match.
      response - response to return when a match is found.
    • addHttpResponseRule

      public static void addHttpResponseRule(RequestMatcher requestMatcher, org.apache.http.HttpResponse response)
      Adds an HTTP response rule. The response will be returned when the rule is matched.
      Parameters:
      requestMatcher - custom RequestMatcher.
      response - response to return when a match is found.
    • addHttpResponseRule

      public static void addHttpResponseRule(RequestMatcher requestMatcher, List<? extends org.apache.http.HttpResponse> responses)
      Adds an HTTP response rule. For each time the rule is matched, responses will be shifted off the list and returned. When all responses have been given and the rule is matched again, an exception will be thrown.
      Parameters:
      requestMatcher - custom RequestMatcher.
      responses - responses to return in order when a match is found.
    • getFakeHttpLayer

      public static FakeHttpLayer getFakeHttpLayer()
    • setDefaultHttpResponse

      public static void setDefaultHttpResponse(int statusCode, String responseBody)
    • setDefaultHttpResponse

      public static void setDefaultHttpResponse(org.apache.http.HttpResponse defaultHttpResponse)
    • clearHttpResponseRules

      public static void clearHttpResponseRules()
    • clearPendingHttpResponses

      public static void clearPendingHttpResponses()
    • reset

      public static void reset()