AuthnRequestUtilsTest.java revision 961910c1409211a8d9223dddd568af1d70ccb7fc
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major/**
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * Copyright 2013 ForgeRock, Inc.
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major *
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * The contents of this file are subject to the terms of the Common Development and
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * Distribution License (the License). You may not use this file except in compliance with the
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * License.
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major *
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * specific language governing permission and limitations under the License.
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major *
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * When distributing Covered Software, include this CDDL Header Notice in each file and include
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * Header, with the fields enclosed by brackets [] replaced by your own identifying
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * information: "Portions copyright [year] [name of copyright owner]".
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major */
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorpackage org.forgerock.openam.auth.shared;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport org.testng.annotations.Test;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport javax.servlet.http.Cookie;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport javax.servlet.http.HttpServletRequest;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport static org.mockito.BDDMockito.given;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport static org.mockito.Mockito.mock;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorimport static org.mockito.Mockito.verify;
1f48f8236de7de97be1c6b9d06bef50b379c8801jenkinsimport static org.testng.AssertJUnit.assertNull;
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major/**
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major * @author robert.wapshott@forgerock.com
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major */
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Majorpublic class AuthnRequestUtilsTest {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major @Test
1f48f8236de7de97be1c6b9d06bef50b379c8801jenkins public void shouldReturnNullIfNoCookies() {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // Given
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major String key = "badger";
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major HttpServletRequest request = mock(HttpServletRequest.class);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major AuthnRequestUtils utils = new AuthnRequestUtils(key);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // When
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major String response = utils.getTokenId(request);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // Then
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major assertNull(response);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major }
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major @Test
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major public void shouldUseCookiesToFindTokenId() {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // Given
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major String key = "badger";
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major HttpServletRequest request = mock(HttpServletRequest.class);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major given(request.getCookies()).willReturn(new Cookie[]{});
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major AuthnRequestUtils utils = new AuthnRequestUtils(key);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // When
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major utils.getTokenId(request);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // Then
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major verify(request).getCookies();
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major }
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major @Test
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major public void shouldUseCookieNameToSelectCookie() {
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major // Given
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major String key = "badger";
1f48f8236de7de97be1c6b9d06bef50b379c8801jenkins HttpServletRequest request = mock(HttpServletRequest.class);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major Cookie one = mock(Cookie.class);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major given(one.getName()).willReturn(key);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major given(request.getCookies()).willReturn(new Cookie[]{one});
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major AuthnRequestUtils utils = new AuthnRequestUtils(key);
80ca0b9f5ad61b2335af25d4dcf25a04ebfcbc91Peter Major
// When
utils.getTokenId(request);
// Then
verify(one).getValue();
}
}