1 package org.robolectric.shadows.httpclient; 2 3 import com.google.errorprone.annotations.InlineMe; 4 import java.util.List; 5 import org.apache.http.Header; 6 import org.apache.http.HttpRequest; 7 import org.apache.http.HttpResponse; 8 9 /** Collection of static methods used interact with HTTP requests / responses. */ 10 public class FakeHttp { 11 private static FakeHttpLayer instance = new FakeHttpLayer(); 12 13 /** 14 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 15 * 16 * @param statusCode the status code of the response 17 * @param responseBody the body of the response 18 * @param headers optional headers for the request 19 */ addPendingHttpResponse( int statusCode, String responseBody, Header... headers)20 public static void addPendingHttpResponse( 21 int statusCode, String responseBody, Header... headers) { 22 getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody, headers); 23 } 24 25 /** 26 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 27 * 28 * @param statusCode the status code of the response 29 * @param responseBody the body of the response 30 * @param contentType the contentType of the response 31 * @deprecated use {@link #addPendingHttpResponse(int, String, org.apache.http.Header...)} instead 32 */ 33 @Deprecated 34 @InlineMe( 35 replacement = 36 "FakeHttp.getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody," 37 + " contentType)", 38 imports = "org.robolectric.shadows.httpclient.FakeHttp") addPendingHttpResponseWithContentType( int statusCode, String responseBody, Header contentType)39 public static final void addPendingHttpResponseWithContentType( 40 int statusCode, String responseBody, Header contentType) { 41 getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody, contentType); 42 } 43 44 /** 45 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 46 * 47 * @param httpResponse the response 48 */ addPendingHttpResponse(HttpResponse httpResponse)49 public static void addPendingHttpResponse(HttpResponse httpResponse) { 50 getFakeHttpLayer().addPendingHttpResponse(httpResponse); 51 } 52 53 /** 54 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 55 * 56 * @param httpResponseGenerator an HttpResponseGenerator that will provide responses 57 */ addPendingHttpResponse(HttpResponseGenerator httpResponseGenerator)58 public static void addPendingHttpResponse(HttpResponseGenerator httpResponseGenerator) { 59 getFakeHttpLayer().addPendingHttpResponse(httpResponseGenerator); 60 } 61 62 /** 63 * Accessor to obtain HTTP requests made during the current test in the order in which they were 64 * made. 65 * 66 * @param index index of the request to retrieve. 67 * @return the requested request. 68 */ getSentHttpRequest(int index)69 public static HttpRequest getSentHttpRequest(int index) { 70 return getFakeHttpLayer().getSentHttpRequestInfo(index).getHttpRequest(); 71 } 72 getLatestSentHttpRequest()73 public static HttpRequest getLatestSentHttpRequest() { 74 return ShadowDefaultRequestDirector.getLatestSentHttpRequest(); 75 } 76 77 /** 78 * Accessor to find out if HTTP requests were made during the current test. 79 * 80 * @return whether a request was made. 81 */ httpRequestWasMade()82 public static boolean httpRequestWasMade() { 83 return getFakeHttpLayer().hasRequestInfos(); 84 } 85 httpRequestWasMade(String uri)86 public static boolean httpRequestWasMade(String uri) { 87 return getFakeHttpLayer().hasRequestMatchingRule(new FakeHttpLayer.UriRequestMatcher(uri)); 88 } 89 90 /** 91 * Accessor to obtain metadata for an HTTP request made during the current test in the order in 92 * which they were made. 93 * 94 * @param index index of the request to retrieve. 95 * @return the requested request metadata. 96 */ getSentHttpRequestInfo(int index)97 public static HttpRequestInfo getSentHttpRequestInfo(int index) { 98 return getFakeHttpLayer().getSentHttpRequestInfo(index); 99 } 100 101 /** 102 * Accessor to obtain HTTP requests made during the current test in the order in which they were 103 * made. 104 * 105 * @return the requested request or null if there are none. 106 */ getNextSentHttpRequest()107 public static HttpRequest getNextSentHttpRequest() { 108 HttpRequestInfo httpRequestInfo = getFakeHttpLayer().getNextSentHttpRequestInfo(); 109 return httpRequestInfo == null ? null : httpRequestInfo.getHttpRequest(); 110 } 111 112 /** 113 * Accessor to obtain metadata for an HTTP request made during the current test in the order in 114 * which they were made. 115 * 116 * @return the requested request metadata or null if there are none. 117 */ getNextSentHttpRequestInfo()118 public static HttpRequestInfo getNextSentHttpRequestInfo() { 119 return getFakeHttpLayer().getNextSentHttpRequestInfo(); 120 } 121 122 /** 123 * Adds an HTTP response rule. The response will be returned when the rule is matched. 124 * 125 * @param method method to match. 126 * @param uri uri to match. 127 * @param response response to return when a match is found. 128 */ addHttpResponseRule(String method, String uri, HttpResponse response)129 public static void addHttpResponseRule(String method, String uri, HttpResponse response) { 130 getFakeHttpLayer().addHttpResponseRule(method, uri, response); 131 } 132 133 /** 134 * Adds an HTTP response rule with a default method of GET. The response will be returned when the 135 * rule is matched. 136 * 137 * @param uri uri to match. 138 * @param response response to return when a match is found. 139 */ addHttpResponseRule(String uri, HttpResponse response)140 public static void addHttpResponseRule(String uri, HttpResponse response) { 141 getFakeHttpLayer().addHttpResponseRule(uri, response); 142 } 143 144 /** 145 * Adds an HTTP response rule. The response will be returned when the rule is matched. 146 * 147 * @param uri uri to match. 148 * @param response response to return when a match is found. 149 */ addHttpResponseRule(String uri, String response)150 public static void addHttpResponseRule(String uri, String response) { 151 getFakeHttpLayer().addHttpResponseRule(uri, response); 152 } 153 154 /** 155 * Adds an HTTP response rule. The response will be returned when the rule is matched. 156 * 157 * @param requestMatcher custom {@code RequestMatcher}. 158 * @param response response to return when a match is found. 159 */ addHttpResponseRule(RequestMatcher requestMatcher, HttpResponse response)160 public static void addHttpResponseRule(RequestMatcher requestMatcher, HttpResponse response) { 161 getFakeHttpLayer().addHttpResponseRule(requestMatcher, response); 162 } 163 164 /** 165 * Adds an HTTP response rule. For each time the rule is matched, responses will be shifted off 166 * the list and returned. When all responses have been given and the rule is matched again, an 167 * exception will be thrown. 168 * 169 * @param requestMatcher custom {@code RequestMatcher}. 170 * @param responses responses to return in order when a match is found. 171 */ addHttpResponseRule( RequestMatcher requestMatcher, List<? extends HttpResponse> responses)172 public static void addHttpResponseRule( 173 RequestMatcher requestMatcher, List<? extends HttpResponse> responses) { 174 getFakeHttpLayer().addHttpResponseRule(requestMatcher, responses); 175 } 176 getFakeHttpLayer()177 public static FakeHttpLayer getFakeHttpLayer() { 178 return instance; 179 } 180 setDefaultHttpResponse(int statusCode, String responseBody)181 public static void setDefaultHttpResponse(int statusCode, String responseBody) { 182 getFakeHttpLayer().setDefaultHttpResponse(statusCode, responseBody); 183 } 184 setDefaultHttpResponse(HttpResponse defaultHttpResponse)185 public static void setDefaultHttpResponse(HttpResponse defaultHttpResponse) { 186 getFakeHttpLayer().setDefaultHttpResponse(defaultHttpResponse); 187 } 188 clearHttpResponseRules()189 public static void clearHttpResponseRules() { 190 getFakeHttpLayer().clearHttpResponseRules(); 191 } 192 clearPendingHttpResponses()193 public static void clearPendingHttpResponses() { 194 getFakeHttpLayer().clearPendingHttpResponses(); 195 } 196 reset()197 public static void reset() { 198 instance = new FakeHttpLayer(); 199 } 200 } 201