View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.auth;
28  
29  import org.apache.hc.client5.http.auth.AuthChallenge;
30  import org.apache.hc.client5.http.auth.AuthScheme;
31  import org.apache.hc.client5.http.auth.AuthScope;
32  import org.apache.hc.client5.http.auth.BearerToken;
33  import org.apache.hc.client5.http.auth.ChallengeType;
34  import org.apache.hc.client5.http.auth.CredentialsProvider;
35  import org.apache.hc.core5.http.HttpHost;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.message.BasicHttpRequest;
38  import org.apache.hc.core5.http.message.BasicNameValuePair;
39  import org.junit.jupiter.api.Assertions;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * Bearer authentication test cases.
44   */
45  public class TestBearerScheme {
46  
47      @Test
48      public void testBearerAuthenticationEmptyChallenge() throws Exception {
49          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "BEARER");
50          final AuthScheme authscheme = new BearerScheme();
51          authscheme.processChallenge(authChallenge, null);
52          Assertions.assertNull(authscheme.getRealm());
53      }
54  
55      @Test
56      public void testBearerAuthentication() throws Exception {
57          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Bearer",
58                  new BasicNameValuePair("realm", "test"));
59  
60          final AuthScheme authscheme = new BearerScheme();
61          authscheme.processChallenge(authChallenge, null);
62  
63          final HttpHost host  = new HttpHost("somehost", 80);
64          final CredentialsProvider credentialsProvider = CredentialsProviderBuilder.create()
65                  .add(new AuthScope(host, "test", null), new BearerToken("some token"))
66                  .build();
67  
68          final HttpRequest request = new BasicHttpRequest("GET", "/");
69          Assertions.assertTrue(authscheme.isResponseReady(host, credentialsProvider, null));
70          authscheme.generateAuthResponse(host, request, null);
71  
72          Assertions.assertEquals("test", authscheme.getRealm());
73          Assertions.assertTrue(authscheme.isChallengeComplete());
74          Assertions.assertFalse(authscheme.isConnectionBased());
75      }
76  
77      @Test
78      public void testStateStorage() throws Exception {
79          final AuthChallenge authChallenge = new AuthChallenge(ChallengeType.TARGET, "Bearer",
80                  new BasicNameValuePair("realm", "test"),
81                  new BasicNameValuePair("code", "read"));
82  
83          final BearerScheme authscheme = new BearerScheme();
84          authscheme.processChallenge(authChallenge, null);
85  
86          final BearerScheme.State state = authscheme.store();
87          final BearerScheme cached = new BearerScheme();
88          cached.restore(state);
89  
90          Assertions.assertEquals(cached.getName(), cached.getName());
91          Assertions.assertEquals(cached.getRealm(), cached.getRealm());
92          Assertions.assertEquals(cached.isChallengeComplete(), cached.isChallengeComplete());
93      }
94  
95  }