You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2023/01/10 19:05:05 UTC
[httpcomponents-client] 03/06: Normalize scheme name in AuthScope
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch 5.3.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit 3a8beba7b607111b1e95f8ff3c4758e777611b08
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sat Nov 26 23:12:59 2022 +0100
Normalize scheme name in AuthScope
---
.../java/org/apache/hc/client5/http/auth/AuthScope.java | 16 +++++-----------
.../org/apache/hc/client5/http/auth/TestAuthScope.java | 4 ++--
.../impl/auth/TestSystemDefaultCredentialsProvider.java | 16 ++++++++++------
3 files changed, 17 insertions(+), 19 deletions(-)
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java
index 312e5b861..35c309b3c 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/auth/AuthScope.java
@@ -76,7 +76,7 @@ public class AuthScope {
this.host = host != null ? host.toLowerCase(Locale.ROOT) : null;
this.port = port >= 0 ? port: -1;
this.realm = realm;
- this.schemeName = schemeName;
+ this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null;
}
/**
@@ -99,7 +99,7 @@ public class AuthScope {
this.host = origin.getHostName().toLowerCase(Locale.ROOT);
this.port = origin.getPort() >= 0 ? origin.getPort() : -1;
this.realm = realm;
- this.schemeName = schemeName;
+ this.schemeName = schemeName != null ? schemeName.toUpperCase(Locale.ROOT) : null;
}
/**
@@ -167,8 +167,7 @@ public class AuthScope {
*/
public int match(final AuthScope that) {
int factor = 0;
- if (Objects.equals(toNullSafeLowerCase(this.schemeName),
- toNullSafeLowerCase(that.schemeName))) {
+ if (Objects.equals(this.schemeName, that.schemeName)) {
factor += 1;
} else {
if (this.schemeName != null && that.schemeName != null) {
@@ -217,8 +216,7 @@ public class AuthScope {
&& Objects.equals(this.host, that.host)
&& this.port == that.port
&& Objects.equals(this.realm, that.realm)
- && Objects.equals(toNullSafeLowerCase(this.schemeName),
- toNullSafeLowerCase(that.schemeName));
+ && Objects.equals(this.schemeName, that.schemeName);
}
return false;
}
@@ -230,14 +228,10 @@ public class AuthScope {
hash = LangUtils.hashCode(hash, this.host);
hash = LangUtils.hashCode(hash, this.port);
hash = LangUtils.hashCode(hash, this.realm);
- hash = LangUtils.hashCode(hash, toNullSafeLowerCase(this.schemeName));
+ hash = LangUtils.hashCode(hash, this.schemeName);
return hash;
}
- private String toNullSafeLowerCase(final String str) {
- return str != null ? str.toLowerCase(Locale.ROOT) : null;
- }
-
@Override
public String toString() {
final StringBuilder buffer = new StringBuilder();
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java
index 61a68a8fe..b7b72b7b4 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/auth/TestAuthScope.java
@@ -38,12 +38,12 @@ public class TestAuthScope {
@Test
public void testBasics() {
final AuthScope authscope = new AuthScope("http", "somehost", 80, "somerealm", "SomeScheme");
- Assertions.assertEquals("SomeScheme", authscope.getSchemeName());
+ Assertions.assertEquals("SOMESCHEME", authscope.getSchemeName());
Assertions.assertEquals("http", authscope.getProtocol());
Assertions.assertEquals("somehost", authscope.getHost());
Assertions.assertEquals(80, authscope.getPort());
Assertions.assertEquals("somerealm", authscope.getRealm());
- Assertions.assertEquals("SomeScheme 'somerealm' http://somehost:80", authscope.toString());
+ Assertions.assertEquals("SOMESCHEME 'somerealm' http://somehost:80", authscope.toString());
}
@Test
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
index 265b4c980..d1fa6ca98 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestSystemDefaultCredentialsProvider.java
@@ -31,6 +31,7 @@ import java.net.Authenticator.RequestorType;
import java.net.InetAddress;
import java.net.PasswordAuthentication;
import java.net.URL;
+import java.util.Locale;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.Credentials;
@@ -99,9 +100,11 @@ public class TestSystemDefaultCredentialsProvider {
final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, coreContext);
- Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
- PROMPT1, StandardAuthScheme.BASIC, httpRequestUrl,
- RequestorType.SERVER);
+ Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
+ PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
+ PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT),
+ httpRequestUrl,
+ RequestorType.SERVER);
Assertions.assertNotNull(receivedCredentials);
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
}
@@ -116,9 +119,10 @@ public class TestSystemDefaultCredentialsProvider {
final Credentials receivedCredentials =
new SystemDefaultCredentialsProvider().getCredentials(authScope, null);
- Mockito.verify(authenticatorDelegate).getPasswordAuthentication(PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
- PROMPT1, StandardAuthScheme.BASIC, null,
- RequestorType.SERVER);
+ Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
+ PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
+ PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT), null,
+ RequestorType.SERVER);
Assertions.assertNotNull(receivedCredentials);
Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
}