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.auth;
28  
29  import org.apache.hc.core5.http.HttpHost;
30  import org.junit.jupiter.api.Assertions;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests for {@link org.apache.hc.client5.http.auth.AuthScope}.
35   */
36  public class TestAuthScope {
37  
38      @Test
39      public void testBasics() {
40          final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme");
41          Assertions.assertEquals("SomeScheme", authscope.getSchemeName());
42          Assertions.assertEquals("http", authscope.getProtocol());
43          Assertions.assertEquals("somehost", authscope.getHost());
44          Assertions.assertEquals(80, authscope.getPort());
45          Assertions.assertEquals("somerealm", authscope.getRealm());
46          Assertions.assertEquals("SomeScheme 'somerealm' http://somehost:80", authscope.toString());
47      }
48  
49      @Test
50      public void testByOrigin() {
51          final HttpHost host = new HttpHost("http", "somehost", 8080);
52          final AuthScope authscope = new AuthScope(host);
53          Assertions.assertNull(authscope.getSchemeName());
54          Assertions.assertEquals("somehost", authscope.getHost());
55          Assertions.assertEquals(8080, authscope.getPort());
56          Assertions.assertNull(authscope.getRealm());
57          Assertions.assertEquals("http", authscope.getProtocol());
58          Assertions.assertEquals("<any auth scheme> <any realm> http://somehost:8080", authscope.toString());
59      }
60  
61      @Test
62      public void testMixedCaseHostname() {
63          final AuthScope authscope = new AuthScope("SomeHost", 80);
64          Assertions.assertNull(authscope.getSchemeName());
65          Assertions.assertEquals("somehost", authscope.getHost());
66          Assertions.assertEquals(80, authscope.getPort());
67          Assertions.assertNull(authscope.getRealm());
68          Assertions.assertEquals("<any auth scheme> <any realm> <any protocol>://somehost:80", authscope.toString());
69      }
70  
71      @Test
72      public void testByOriginMixedCaseHostname() throws Exception {
73          final HttpHost host = new HttpHost("http", "SomeHost", 8080);
74          final AuthScope authscope = new AuthScope(host);
75          Assertions.assertEquals("somehost", authscope.getHost());
76      }
77  
78      @Test
79      public void testBasicsAllOptional() {
80          final AuthScope authscope = new AuthScope(null, null, -1, null, null);
81          Assertions.assertNull(authscope.getSchemeName());
82          Assertions.assertNull(authscope.getHost());
83          Assertions.assertEquals(-1, authscope.getPort());
84          Assertions.assertNull(authscope.getRealm());
85          Assertions.assertEquals("<any auth scheme> <any realm> <any protocol>://<any host>:<any port>", authscope.toString());
86      }
87  
88      @Test
89      public void testScopeMatching() {
90          final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
91          final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
92          Assertions.assertTrue(authscope1.match(authscope2) < 0);
93  
94          int m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
95          int m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
96          Assertions.assertTrue(m2 > m1);
97  
98          m1 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
99          m2 = authscope1.match(new AuthScope(null, null, -1, "somerealm", null));
100         Assertions.assertTrue(m2 > m1);
101 
102         m1 = authscope1.match(new AuthScope(null, null, -1, "somerealm", "somescheme"));
103         m2 = authscope1.match(new AuthScope(null, null, 80, null, null));
104         Assertions.assertTrue(m2 > m1);
105 
106         m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
107         m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
108         Assertions.assertTrue(m2 > m1);
109 
110         m1 = authscope1.match(new AuthScope(null, null, 80, "somerealm", "somescheme"));
111         m2 = authscope1.match(new AuthScope(null, "somehost", -1, null, null));
112         Assertions.assertTrue(m2 > m1);
113 
114         m1 = authscope1.match(new AuthScope(null, null, -1, null, null));
115         m2 = authscope1.match(new AuthScope(null, null, -1, null, "somescheme"));
116         Assertions.assertTrue(m2 > m1);
117     }
118 
119     @Test
120     public void testEquals() {
121         final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
122         final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
123         final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
124         final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
125         final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
126         final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
127         final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme");
128         final AuthScope authscope8 = new AuthScope("https", "somehost", 80, "somerealm", "SomeScheme");
129         Assertions.assertEquals(authscope1, authscope1);
130         Assertions.assertNotEquals(authscope1, authscope2);
131         Assertions.assertEquals(authscope1, authscope3);
132         Assertions.assertNotEquals(authscope1, authscope4);
133         Assertions.assertNotEquals(authscope1, authscope5);
134         Assertions.assertNotEquals(authscope1, authscope6);
135         Assertions.assertNotEquals(authscope1, authscope7);
136         Assertions.assertEquals(authscope7, authscope8);
137     }
138 
139     @Test
140     public void testHash() {
141         final AuthScope authscope1 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
142         final AuthScope authscope2 = new AuthScope("http", "someotherhost", 80, "somerealm", "somescheme");
143         final AuthScope authscope3 = new AuthScope("http", "somehost", 80, "somerealm", "somescheme");
144         final AuthScope authscope4 = new AuthScope("http", "somehost", 8080, "somerealm", "somescheme");
145         final AuthScope authscope5 = new AuthScope("http", "somehost", 80, "someotherrealm", "somescheme");
146         final AuthScope authscope6 = new AuthScope("http", "somehost", 80, "somerealm", "someotherscheme");
147         final AuthScope authscope7 = new AuthScope("https", "somehost", 80, "somerealm", "somescheme");
148         final AuthScope authscope8 = new AuthScope("https", "somehost", 80, "somerealm", "SomeScheme");
149         Assertions.assertEquals(authscope1.hashCode(), authscope1.hashCode());
150         Assertions.assertNotEquals(authscope1.hashCode(), authscope2.hashCode());
151         Assertions.assertEquals(authscope1.hashCode(), authscope3.hashCode());
152         Assertions.assertNotEquals(authscope1.hashCode(), authscope4.hashCode());
153         Assertions.assertNotEquals(authscope1.hashCode(), authscope5.hashCode());
154         Assertions.assertNotEquals(authscope1.hashCode(), authscope6.hashCode());
155         Assertions.assertNotEquals(authscope1.hashCode(), authscope7.hashCode());
156         Assertions.assertEquals(authscope7.hashCode(), authscope8.hashCode());
157     }
158 
159 }