return request; return super.buildRequest(method, url);
@Test void setupAndTest() throws SetupFailedException { final HttpTransport transport = new MockHttpTransport(); final CredentialProvider credentialProvider = CredentialProvider.mock(true); final Feature feature = new GoogleCredentialsFeature("someone@somewhere.cz", transport, credentialProvider); feature.setup(); // this will fail and is too hard to mock away; doesn't matter much, this is all tested in Stonky module assertThatThrownBy(feature::test).isInstanceOf(TestFailedException.class); }
@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); }
@Test void credentialRetrieval() { final HttpTransport transport = new MockHttpTransport(); final CredentialProvider credentialProvider = CredentialProvider.mock(true); final String username = "someone@somewhere.cz"; final GoogleCredentialsFeature feature = new GoogleCredentialsFeature(username, transport, credentialProvider); final SessionInfo sessionInfo = new SessionInfo(username); assertThat(feature.runGoogleCredentialCheckForDrive(sessionInfo)).isNotNull(); assertThat(feature.runGoogleCredentialCheckForSheets(sessionInfo)).isNotNull(); } }
.build(); HttpRequest otherRequest = otherTransport .createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); otherRequest.setThrowExceptionOnExecuteError(false); HttpResponse otherServiceUnavailableResponse = otherRequest.execute();
@Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { if (url.equals(tokenServerUrl)) { return buildTokenRequest(url); } else if (url.equals(LEGACY_TOKEN_SERVER_URL)) { LOGGER.warning("Your configured token_uri is using a legacy endpoint. You may want to " + "redownload your credentials."); return buildTokenRequest(url); } return super.buildRequest(method, url); }
MockJsonFactory mockJsonFactory = new MockJsonFactory(); HttpTransport transport = new MockHttpTransport() { @Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { return new MockLowLevelHttpRequest() { @Override public LowLevelHttpResponse execute() throws IOException { MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); response.setStatusCode(408); response.setContent("{\"error\":\"Timeout\"}"); return response; } }; } }; HttpRequest httpRequest = transport.createRequestFactory().buildGetRequest(HttpTesting.SIMPLE_GENERIC_URL); httpRequest.setThrowExceptionOnExecuteError(false); HttpResponse httpResponse = httpRequest.execute(); GoogleJsonResponseException googleJsonResponseException =
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. } }
@Override public LowLevelHttpRequest buildRequest(final String method, final String url) throws IOException { return responseHandlers.stream() .map(h -> h.apply(method, url)) .flatMap(o -> o.map(Stream::of).orElse(Stream.empty())) .findFirst() .map(MultiRequestMockHttpTransport::getRequest) .orElse(super.buildRequest(method, url)); } }
.setRequest(catMessage) .setApiMethodDescriptor(methodDescriptor) .setHttpTransport(new MockHttpTransport()) .setJsonFactory(new JacksonFactory()) .setResponseFuture(SettableApiFuture.<Void>create())
/** * 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(); }
@Override public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { String formattedUrl = String.format(IAM_ENDPOINT, this.targetPrincipal); if (url.equals(formattedUrl)) { return new MockLowLevelHttpRequest(url) { @Override public LowLevelHttpResponse execute() throws IOException { if (tokenResponseErrorCode != null) { return new MockLowLevelHttpResponse() .setStatusCode(tokenResponseErrorCode) .setContentType(Json.MEDIA_TYPE) .setContent(tokenResponseErrorContent); } // Create the JSON response GenericJson refreshContents = new GenericJson(); refreshContents.setFactory(OAuth2Utils.JSON_FACTORY); refreshContents.put("accessToken", accessToken); refreshContents.put("expireTime", expireTime); String refreshText = refreshContents.toPrettyString(); return new MockLowLevelHttpResponse() .setContentType(Json.MEDIA_TYPE) .setContent(refreshText); } }; } return super.buildRequest(method, url); }
.setRequest(insertFrogRequest) .setApiMethodDescriptor(apiMethodDescriptor) .setHttpTransport(new MockHttpTransport()) .setJsonFactory(new JacksonFactory()) .setResponseFuture(SettableApiFuture.<Void>create())
return super.buildRequest(method, url);
return super.buildRequest(method, url);