|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.client.authentication; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import javax.ws.rs.client.ClientRequestContext; |
| 22 | +import javax.ws.rs.client.ClientResponseContext; |
| 23 | +import javax.ws.rs.core.HttpHeaders; |
| 24 | +import javax.ws.rs.core.MultivaluedMap; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 30 | +import static org.mockito.Mockito.mock; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +class BasicAuthenticatorTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void filterResponseAndAuthenticateNoAuthHeadersTest() { |
| 37 | + final BasicAuthenticator authenticator |
| 38 | + = new BasicAuthenticator(new HttpAuthenticationFilter.Credentials("foo", "bar")); |
| 39 | + final ClientRequestContext request = mock(ClientRequestContext.class); |
| 40 | + final ClientResponseContext response = mock(ClientResponseContext.class); |
| 41 | + |
| 42 | + when(response.getHeaders()).thenReturn(mock(MultivaluedMap.class)); |
| 43 | + |
| 44 | + assertFalse(authenticator.filterResponseAndAuthenticate(request, response)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void filterResponseAndAuthenticateAuthHeaderNotBasicTest() { |
| 49 | + final BasicAuthenticator authenticator |
| 50 | + = new BasicAuthenticator(new HttpAuthenticationFilter.Credentials("foo", "bar")); |
| 51 | + final ClientRequestContext request = mock(ClientRequestContext.class); |
| 52 | + final ClientResponseContext response = mock(ClientResponseContext.class); |
| 53 | + |
| 54 | + final MultivaluedMap<String, String> headers = mock(MultivaluedMap.class); |
| 55 | + when(response.getHeaders()).thenReturn(headers); |
| 56 | + when(headers.get(HttpHeaders.WWW_AUTHENTICATE)).thenReturn(Collections.singletonList("Digest realm=\"test\"")); |
| 57 | + |
| 58 | + assertFalse(authenticator.filterResponseAndAuthenticate(request, response)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void filterResponseAndAuthenticateEmptyListTest() { |
| 63 | + final BasicAuthenticator authenticator |
| 64 | + = new BasicAuthenticator(new HttpAuthenticationFilter.Credentials("foo", "bar")); |
| 65 | + final ClientRequestContext request = mock(ClientRequestContext.class); |
| 66 | + final ClientResponseContext response = mock(ClientResponseContext.class); |
| 67 | + |
| 68 | + final MultivaluedMap<String, String> headers = mock(MultivaluedMap.class); |
| 69 | + when(response.getHeaders()).thenReturn(headers); |
| 70 | + when(headers.get(HttpHeaders.WWW_AUTHENTICATE)).thenReturn(Collections.emptyList()); |
| 71 | + |
| 72 | + assertFalse(authenticator.filterResponseAndAuthenticate(request, response)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void filterResponseAndAuthenticateNullListTest() { |
| 77 | + final BasicAuthenticator authenticator |
| 78 | + = new BasicAuthenticator(new HttpAuthenticationFilter.Credentials("foo", "bar")); |
| 79 | + final ClientRequestContext request = mock(ClientRequestContext.class); |
| 80 | + final ClientResponseContext response = mock(ClientResponseContext.class); |
| 81 | + |
| 82 | + final MultivaluedMap<String, String> headers = mock(MultivaluedMap.class); |
| 83 | + when(response.getHeaders()).thenReturn(headers); |
| 84 | + when(headers.get(HttpHeaders.WWW_AUTHENTICATE)).thenReturn(null); |
| 85 | + |
| 86 | + assertFalse(authenticator.filterResponseAndAuthenticate(request, response)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void filterResponseAndAuthenticateMissingCredentialsMultipleAuthRealmsTest() { |
| 91 | + final String[] authHeaders = new String[] { |
| 92 | + "Digest realm=\"test\"", |
| 93 | + "Basic realm=\"test\"" |
| 94 | + }; |
| 95 | + final BasicAuthenticator authenticator = new BasicAuthenticator(null); |
| 96 | + final ClientRequestContext request = mock(ClientRequestContext.class); |
| 97 | + final ClientResponseContext response = mock(ClientResponseContext.class); |
| 98 | + |
| 99 | + final MultivaluedMap<String, String> headers = mock(MultivaluedMap.class); |
| 100 | + when(response.getHeaders()).thenReturn(headers); |
| 101 | + when(headers.get(HttpHeaders.WWW_AUTHENTICATE)).thenReturn(Arrays.asList(authHeaders)); |
| 102 | + when(response.hasEntity()).thenReturn(false); |
| 103 | + |
| 104 | + assertThrows(ResponseAuthenticationException.class, |
| 105 | + () -> authenticator.filterResponseAndAuthenticate(request, response)); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments