public CookieClient(final String cookieName, final Authenticator cookieAuthenticator) { this.cookieName = cookieName; defaultAuthenticator(cookieAuthenticator); }
@Override protected void clientInit() { CommonHelper.assertNotBlank("cookieName", this.cookieName); defaultCredentialsExtractor(new CookieExtractor(this.cookieName)); }
@Test public void testAuthentication() { final CookieClient client = new CookieClient(USERNAME, new SimpleTestTokenAuthenticator()); final MockWebContext context = MockWebContext.create(); final Cookie c = new Cookie(USERNAME, Base64.getEncoder().encodeToString(getClass().getName().getBytes(StandardCharsets.UTF_8))); context.getRequestCookies().add(c); final TokenCredentials credentials = client.getCredentials(context); final CommonProfile profile = client.getUserProfile(credentials, context); assertEquals(c.getValue(), profile.getId()); } }
@Test public void testMissingProfileCreator() { final CookieClient cookieClient = new CookieClient("testcookie", new SimpleTestTokenAuthenticator()); cookieClient.setProfileCreator(null); TestsHelper.expectException(() -> cookieClient.getUserProfile(new TokenCredentials(TOKEN), MockWebContext.create()), TechnicalException.class, "profileCreator cannot be null"); }
@Test public void testHasDefaultProfileCreator() { final CookieClient cookieClient = new CookieClient("testcookie", new SimpleTestTokenAuthenticator()); cookieClient.init(); }
@Test public void testMissingUsernamePasswordAuthenticator() { final CookieClient cookieClient = new CookieClient("testcookie", null); TestsHelper.expectException(() -> cookieClient.getCredentials(MockWebContext.create()), TechnicalException.class, "authenticator cannot be null"); }
@Test(expected=Exception.class) public void testMissingCookieName() { final CookieClient cookieClient = new CookieClient(null, new SimpleTestTokenAuthenticator()); cookieClient.init(); }