@Before public void setUp() { MockitoAnnotations.initMocks(this); fakeTransport = new MockHttpTransport() { // Only override the method for retrieving a LowLevelHttpRequest. @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { // TODO(user): Also record and test the number of calls to this and the method/url. return mockLowLevelRequest; } }; initializer = new RetryHttpInitializer(mockCredential, "foo-user-agent"); initializer.setSleeperOverride(mockSleeper); requestFactory = fakeTransport.createRequestFactory(initializer); }
.build(); HttpRequest otherRequest = otherTransport .createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); otherRequest.setThrowExceptionOnExecuteError(false); HttpResponse otherServiceUnavailableResponse = otherRequest.execute();
HttpResponseException makeHttpException(int status) throws IOException { MockHttpTransport.Builder builder = new MockHttpTransport.Builder(); MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse(); resp.setStatusCode(status); builder.setLowLevelHttpResponse(resp); try { HttpResponse res = builder.build() .createRequestFactory() .buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL) .execute(); return new HttpResponseException(res); } catch (HttpResponseException exception) { return exception; // Throws the exception we want anyway, so just return it. } }
/** * Builds a HttpResponse with the given string response. * * @param header header value to provide or null if none. * @param uploadId upload id to provide in the url upload id param or null if none. * @param uploadType upload type to provide in url upload type param or null if none. * @return HttpResponse with the given parameters * @throws IOException */ private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType) throws IOException { MockHttpTransport.Builder builder = new MockHttpTransport.Builder(); MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse(); builder.setLowLevelHttpResponse(resp); resp.setStatusCode(200); GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL); if (header != null) { resp.addHeader("X-GUploader-UploadID", header); } if (uploadId != null) { url.put("upload_id", uploadId); } if (uploadType != null) { url.put("uploadType", uploadType); } return builder.build().createRequestFactory().buildGetRequest(url).execute(); }