private void setAuthInterceptor(String uriHost, String uriPath, String uriProtocol, int uriPort) { if (iamApiKey != null) { URI baseURI = getBaseUri(uriHost, uriPath, uriProtocol, uriPort); IamCookieInterceptor ici = new IamCookieInterceptor(iamApiKey, baseURI.toString()); requestInterceptors.add(ici); responseInterceptors.add(ici); } else if (this.username != null && this.password != null) { URI baseURI = getBaseUri(uriHost, uriPath, uriProtocol, uriPort); CookieInterceptor ci = new CookieInterceptor(this.username, this.password, baseURI .toString()); requestInterceptors.add(ci); responseInterceptors.add(ci); } }
private CookieInterceptor buildCookieInterceptor() { if (username != null && password != null) { String path = rootUri.getRawPath() == null ? "" : rootUri.getRawPath(); if (path.length() > 0) { int index = path.lastIndexOf("/"); if (index == path.length() - 1) { // we need to go back one path = path.substring(0, index); index = path.lastIndexOf("/"); } path = path.substring(0, index); } URI baseURI; try { baseURI = new URI(rootUri.getScheme(), null, rootUri.getHost(), rootUri.getPort() , path, null, null); Logger.getLogger(this.getClass().getCanonicalName()).info(String.format(Locale.ENGLISH, "Cookie info: %s", baseURI)); } catch (URISyntaxException e) { throw new RuntimeException(e); } return new CookieInterceptor(username, password, baseURI.toString()); } return null; }
CookieInterceptor cookieInterceptor = new CookieInterceptor(username, password, this.url.toString());
@TestTemplate @RequiresCloudant public void testCookieAuthWithoutRetry() throws IOException { CookieInterceptor interceptor = new CookieInterceptor(CloudantClientHelper.COUCH_USERNAME, CloudantClientHelper.COUCH_PASSWORD, clientResource.get().getBaseUri().toString()); HttpConnection conn = new HttpConnection("POST", dbResource.get().getDBUri().toURL(), "application/json"); conn.responseInterceptors.add(interceptor); conn.requestInterceptors.add(interceptor); ByteArrayInputStream bis = new ByteArrayInputStream(data.getBytes()); // nothing read from stream assertEquals(data.getBytes().length, bis.available()); conn.setRequestBody(bis); HttpConnection responseConn = conn.execute(); // stream was read to end assertEquals(0, bis.available()); assertEquals(2, responseConn.getConnection().getResponseCode() / 100); //check the json Gson gson = new Gson(); InputStream is = responseConn.responseAsInputStream(); try { JsonObject response = gson.fromJson(new InputStreamReader(is), JsonObject.class); assertTrue(response.has("ok")); assertTrue(response.get("ok").getAsBoolean()); assertTrue(response.has("id")); assertTrue(response.has("rev")); } finally { is.close(); } }
CookieInterceptor interceptor = new CookieInterceptor(TestOptions.COUCH_USERNAME, TestOptions.COUCH_PASSWORD, getCouchConfig("httptest" + System
@BeforeEach public void setupSessionInterceptor(boolean okUsable, String sessionPath) { this.mockWebServer = mockWebServerExt.get(); this.mockIamServer = mockIamServerExt.get(); String baseUrl = mockWebServer.url("").toString(); if (sessionPath.equals("/_session")) { CookieInterceptor ci = new CookieInterceptor("user", "pass", baseUrl); rqInterceptor = ci; rpInterceptor = ci; } else if (sessionPath.equals("/_iam_session")) { // Set the endpoint value before each test iamSystemPropertyMock.setMockIamTokenEndpointUrl(mockIamServer.url("/identity/token") .toString()); IamCookieInterceptor ici = new IamCookieInterceptor("apikey", baseUrl); rqInterceptor = ici; rpInterceptor = ici; } else { fail("Invalid sessionPath " + sessionPath); } }