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