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.htmlunit.junit.BrowserRunner;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  
25  /**
26   * Tests for {@link DefaultCredentialsProvider}.
27   *
28   * @author Marc Guillemot
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class DefaultCredentialsProviderTest extends SimpleWebTestCase {
33  
34      /**
35       * Test that successive calls to {@link DefaultCredentialsProvider#addCredentials(String, String)}
36       * overwrite values previously set.
37       * @throws Exception if the test fails
38       */
39      @Test
40      public void overwrite() throws Exception {
41          final String realm = "blah";
42          final String scheme = new BasicScheme().getSchemeName();
43  
44          final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
45          provider.addCredentials("username", "password".toCharArray());
46  
47          Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
48          assertEquals("username", credentials.getUserPrincipal().getName());
49          assertEquals("password", credentials.getPassword());
50  
51          provider.addCredentials("username", "new password".toCharArray());
52          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
53          assertEquals("username", credentials.getUserPrincipal().getName());
54          assertEquals("new password", credentials.getPassword());
55  
56          provider.addCredentials("new username", "other password".toCharArray());
57          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
58          assertEquals("new username", credentials.getUserPrincipal().getName());
59          assertEquals("other password", credentials.getPassword());
60      }
61  
62      /**
63       * @throws Exception if an error occurs
64       */
65      @Test
66      public void basicAuthenticationTwice() throws Exception {
67          ((DefaultCredentialsProvider) getWebClient().getCredentialsProvider())
68                                          .addCredentials("jetty", "jetty".toCharArray());
69  
70          getMockWebConnection().setResponse(URL_SECOND, "Hello World");
71          HtmlPage page = loadPage("Hi There");
72          assertTrue(page.asNormalizedText().contains("Hi There"));
73          page = getWebClient().getPage(URL_SECOND);
74          assertTrue(page.asNormalizedText().contains("Hello World"));
75      }
76  
77      /**
78       * @throws Exception if the test fails
79       */
80      @Test
81      public void removeCredentials() throws Exception {
82          final String realm = "blah";
83          final String scheme = new BasicScheme().getSchemeName();
84  
85          final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
86          provider.addCredentials("username", "password".toCharArray(), HttpHeader.HOST_LC, 80, realm);
87  
88          Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
89          assertEquals("username", credentials.getUserPrincipal().getName());
90          assertEquals("password", credentials.getPassword());
91  
92          provider.removeCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
93  
94          credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
95          assertNull(credentials);
96      }
97  
98      /**
99       * @throws Exception if the test fails
100      */
101     @Test
102     public void passwordNull() throws Exception {
103         final String realm = "blah";
104         final String scheme = new BasicScheme().getSchemeName();
105 
106         final DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
107         provider.addCredentials("username", (char[]) null, HttpHeader.HOST_LC, 80, realm);
108 
109         final Credentials credentials = provider.getCredentials(new AuthScope(HttpHeader.HOST_LC, 80, realm, scheme));
110         assertEquals("username", credentials.getUserPrincipal().getName());
111         assertNull(credentials.getPassword());
112     }
113 }