new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build();
/** * 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(); }
.setReasonPhrase(reasonPhrase); MockHttpTransport otherTransport = new MockHttpTransport.Builder() .setLowLevelHttpResponse(otherServiceUnavaiableLowLevelResponse) .build(); HttpRequest otherRequest = otherTransport
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. } }
@Test public void testFileSizeWhenFileNotFoundNonBatch() throws Exception { MockLowLevelHttpResponse notFoundResponse = new MockLowLevelHttpResponse(); notFoundResponse.setContent(""); notFoundResponse.setStatusCode(HttpStatusCodes.STATUS_CODE_NOT_FOUND); MockHttpTransport mockTransport = new MockHttpTransport.Builder().setLowLevelHttpResponse(notFoundResponse).build(); GcsOptions pipelineOptions = gcsOptionsWithTestCredential(); GcsUtil gcsUtil = pipelineOptions.getGcsUtil(); gcsUtil.setStorageClient(new Storage(mockTransport, Transport.getJsonFactory(), null)); thrown.expect(FileNotFoundException.class); gcsUtil.fileSize(GcsPath.fromComponents("testbucket", "testobject")); }