View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import org.apache.http.auth.AuthScope;
18  import org.apache.http.auth.Credentials;
19  import org.apache.http.impl.auth.BasicScheme;
20  import org.htmlunit.html.HtmlPage;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link DefaultCredentialsProvider}.
25   *
26   * @author Marc Guillemot
27   * @author Ronald Brill
28   */
29  public class DefaultCredentialsProviderTest extends SimpleWebTestCase {
30  
31      /**
32       * Test that successive calls to {@link DefaultCredentialsProvider#addCredentials(String, String)}
33       * overwrite values previously set.
34       * @throws Exception if the test fails
35       */
36      @Test
37      public void overwrite() throws Exception {
38          final String realm = "blah";
39          final String scheme = new BasicScheme().getSchemeName();
40  
41          final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
42          provider.addCredentials("username", "password".toCharArray());
43  
44          Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
45          assertEquals("username", credentials.getUserPrincipal().getName());
46          assertEquals("password", credentials.getPassword());
47  
48          provider.addCredentials("username", "new password".toCharArray());
49          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
50          assertEquals("username", credentials.getUserPrincipal().getName());
51          assertEquals("new password", credentials.getPassword());
52  
53          provider.addCredentials("new username", "other password".toCharArray());
54          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
55          assertEquals("new username", credentials.getUserPrincipal().getName());
56          assertEquals("other password", credentials.getPassword());
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      public void basicAuthenticationTwice() throws Exception {
64          ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider())
65                                          .addCredentials("jetty", "jetty".toCharArray());
66  
67          getMockWebConnection().setResponse(URL_SECOND, "Hello World");
68          HtmlPage page = loadPage("Hi There");
69          assertTrue(page.asNormalizedText().contains("Hi There"));
70          page = getWebClient().getPage(URL_SECOND);
71          assertTrue(page.asNormalizedText().contains("Hello World"));
72      }
73  
74      /**
75       * @throws Exception if the test fails
76       */
77      @Test
78      public void removeCredentials() throws Exception {
79          final String realm = "blah";
80          final String scheme = new BasicScheme().getSchemeName();
81  
82          final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
83          provider.addCredentials("username", "password".toCharArray(), HttpHeader.HOST_LC, 80, realm);
84  
85          Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
86          assertEquals("username", credentials.getUserPrincipal().getName());
87          assertEquals("password", credentials.getPassword());
88  
89          provider.removeCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
90  
91          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
92          assertNull(credentials);
93      }
94  
95      /**
96       * @throws Exception if the test fails
97       */
98      @Test
99      public void passwordNull() throws Exception {
100         final String realm = "blah";
101         final String scheme = new BasicScheme().getSchemeName();
102 
103         final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
104         provider.addCredentials("username", (char[]) null, HttpHeader.HOST_LC, 80, realm);
105 
106         final Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
107         assertEquals("username", credentials.getUserPrincipal().getName());
108         assertNull(credentials.getPassword());
109     }
110 }