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.Assert;
31  import org.junit.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          Assert.assertEquals("SomeScheme", authscope.getSchemeName());
42          Assert.assertEquals("http", authscope.getProtocol());
43          Assert.assertEquals("somehost", authscope.getHost());
44          Assert.assertEquals(80, authscope.getPort());
45          Assert.assertEquals("somerealm", authscope.getRealm());
46          Assert.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          Assert.assertEquals(null, authscope.getSchemeName());
54          Assert.assertEquals("somehost", authscope.getHost());
55          Assert.assertEquals(8080, authscope.getPort());
56          Assert.assertEquals(null, authscope.getRealm());
57          Assert.assertEquals("http", authscope.getProtocol());
58          Assert.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          Assert.assertEquals(null, authscope.getSchemeName());
65          Assert.assertEquals("somehost", authscope.getHost());
66          Assert.assertEquals(80, authscope.getPort());
67          Assert.assertEquals(null, authscope.getRealm());
68          Assert.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          Assert.assertEquals("somehost", authscope.getHost());
76      }
77  
78      @Test
79      public void testBasicsAllOptional() {
80          final AuthScope authscope = new AuthScope(null, null, -1, null, null);
81          Assert.assertEquals(null, authscope.getSchemeName());
82          Assert.assertEquals(null, authscope.getHost());
83          Assert.assertEquals(-1, authscope.getPort());
84          Assert.assertEquals(null, authscope.getRealm());
85          Assert.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          Assert.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          Assert.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         Assert.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         Assert.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         Assert.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         Assert.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         Assert.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         Assert.assertTrue(authscope1.equals(authscope1));
130         Assert.assertFalse(authscope1.equals(authscope2));
131         Assert.assertTrue(authscope1.equals(authscope3));
132         Assert.assertFalse(authscope1.equals(authscope4));
133         Assert.assertFalse(authscope1.equals(authscope5));
134         Assert.assertFalse(authscope1.equals(authscope6));
135         Assert.assertFalse(authscope1.equals(authscope7));
136         Assert.assertTrue(authscope7.equals(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         Assert.assertTrue(authscope1.hashCode() == authscope1.hashCode());
150         Assert.assertFalse(authscope1.hashCode() == authscope2.hashCode());
151         Assert.assertTrue(authscope1.hashCode() == authscope3.hashCode());
152         Assert.assertFalse(authscope1.hashCode() == authscope4.hashCode());
153         Assert.assertFalse(authscope1.hashCode() == authscope5.hashCode());
154         Assert.assertFalse(authscope1.hashCode() == authscope6.hashCode());
155         Assert.assertFalse(authscope1.hashCode() == authscope7.hashCode());
156         Assert.assertTrue(authscope7.hashCode() == authscope8.hashCode());
157     }
158 
159 }