You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2004/06/13 14:13:08 UTC
cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestAuthenticator.java TestHttpState.java TestHttps.java TestMethodsExternalHost.java TestProxy.java TestWebappBasicAuth.java
olegk 2004/06/13 05:13:08
Modified: httpclient/src/test/org/apache/commons/httpclient
TestAuthenticator.java TestHttpState.java
TestHttps.java TestMethodsExternalHost.java
TestProxy.java TestWebappBasicAuth.java
Log:
Deprecated methods in test cases cleaned up
Revision Changes Path
1.43 +44 -95 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java
Index: TestAuthenticator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v
retrieving revision 1.42
retrieving revision 1.43
diff -u -r1.42 -r1.43
--- TestAuthenticator.java 29 Apr 2004 14:28:29 -0000 1.42
+++ TestAuthenticator.java 13 Jun 2004 12:13:08 -0000 1.43
@@ -37,6 +37,7 @@
import org.apache.commons.httpclient.auth.AuthChallengeParser;
import org.apache.commons.httpclient.auth.AuthScheme;
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.auth.AuthenticationException;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.DigestScheme;
@@ -89,21 +90,25 @@
if (state == null) {
throw new IllegalArgumentException("HTTP state may not be null");
}
- String host = null;
+ String host = AuthScope.ANY_HOST;
+ int port = AuthScope.ANY_PORT;
if (conn != null) {
if (proxy) {
host = conn.getProxyHost();
+ port = conn.getProxyPort();
} else {
host = conn.getVirtualHost();
if (host == null) {
host = conn.getHost();
}
+ port = conn.getPort();
}
}
String realm = authscheme.getRealm();
+ AuthScope authscope = new AuthScope(host, port, realm);
Credentials credentials = proxy
- ? state.getProxyCredentials(realm, host)
- : state.getCredentials(realm, host);
+ ? state.getProxyCredentials(authscope)
+ : state.getCredentials(authscope);
if (credentials == null) {
throw new CredentialsNotAvailableException("No credentials available");
}
@@ -222,7 +227,7 @@
String challenge = "dIgEsT ReAlM=\"realm1\", nOnCE=\"f2a3F18799759D4f1a1C068b92b573cB\"";
HttpState state = new HttpState();
UsernamePasswordCredentials cred = new UsernamePasswordCredentials("username","password");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WwW-AuThEnTiCaTe", challenge));
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
@@ -235,7 +240,7 @@
String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
HttpState state = new HttpState();
UsernamePasswordCredentials cred = new UsernamePasswordCredentials("username","password");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
@@ -253,7 +258,7 @@
String challenge = "Digest realm=\"realm1\", nonce=\"f2a3f18799759d4f1a1c068b92b573cb\"";
HttpState state = new HttpState();
UsernamePasswordCredentials cred = new UsernamePasswordCredentials("username","password");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
@@ -291,7 +296,7 @@
conn.addResponse(headers2);
conn.addResponse(headers3);
UsernamePasswordCredentials cred = new UsernamePasswordCredentials("username","password");
- client.getState().setCredentials(null, null, cred);
+ client.getState().setCredentials(AuthScope.ANY, cred);
connectionManager.setConnection(conn);
@@ -311,9 +316,9 @@
String challenge2 = "Digest realm=\"realm2\", nonce=\"123546\"";
HttpState state = new HttpState();
UsernamePasswordCredentials cred = new UsernamePasswordCredentials("username","password");
- state.setCredentials("realm1", null, cred);
+ state.setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1"), cred);
UsernamePasswordCredentials cred2 = new UsernamePasswordCredentials("uname2","password2");
- state.setCredentials("realm2", null, cred2);
+ state.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2"), cred2);
AuthScheme authscheme1 = new DigestScheme();
authscheme1.processChallenge(challenge1);
AuthScheme authscheme2 = new DigestScheme();
@@ -363,7 +368,7 @@
HttpState state = new HttpState();
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
- state.setCredentials(realm, null, cred);
+ state.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, realm), cred);
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
HttpMethod method =
@@ -405,7 +410,7 @@
HttpState state = new HttpState();
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
- state.setCredentials(realm, null, cred);
+ state.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, realm), cred);
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
HttpMethod method =
@@ -447,7 +452,7 @@
HttpState state = new HttpState();
UsernamePasswordCredentials cred =
new UsernamePasswordCredentials(username, password);
- state.setCredentials(realm, null, cred);
+ state.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1"), cred);
try {
AuthScheme authscheme = new DigestScheme();
authscheme.processChallenge(challenge);
@@ -493,7 +498,7 @@
HttpState state = new HttpState();
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WwW-AuThEnTiCaTe", challenge));
AuthScheme authscheme = new NTLMScheme(challenge);
assertTrue(authenticate(authscheme, method, null, state));
@@ -507,7 +512,7 @@
HttpState state = new HttpState();
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
AuthScheme authscheme = new NTLMScheme(challenge);
assertTrue(authenticate(authscheme, method, null, state));
@@ -530,7 +535,7 @@
HttpState state = new HttpState();
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
AuthScheme authscheme = new NTLMScheme(challenge);
assertTrue(authenticate(authscheme, method, null, state));
@@ -552,7 +557,7 @@
HttpState state = new HttpState();
NTCredentials cred = new NTCredentials("username","password", "host",
"domain");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge));
method.addRequestHeader("Host", "host");
AuthScheme authscheme = new NTLMScheme(challenge);
@@ -564,7 +569,7 @@
public void testNTLMAuthenticationRetry() throws Exception {
NTCredentials cred = new NTCredentials("username", "password", "host", "domain");
- client.getState().setCredentials(null, null, cred);
+ client.getState().setCredentials(AuthScope.ANY, cred);
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 401 Unauthorized\r\n" +
@@ -590,8 +595,9 @@
* Test that the Unauthorized response is returned when doAuthentication is false.
*/
public void testDoAuthenticateFalse() throws Exception {
- client.getState().setCredentials(null, "Protected",
- new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setCredentials(
+ new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "Protected"),
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
method.setDoAuthentication(false);
conn.addResponse(
@@ -614,7 +620,9 @@
/**
*/
public void testInvalidCredentials() throws Exception {
- client.getState().setCredentials(null, "Protected", new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setCredentials(
+ new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "Protected"),
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
method.setDoAuthentication(false);
conn.addResponse(
@@ -631,7 +639,8 @@
// --------------------------------- Test Methods for Multiple Authentication
public void testMultipleChallengeBasic() throws Exception {
- client.getState().setCredentials(null, null, new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 401 Unauthorized\r\n" +
@@ -654,7 +663,8 @@
}
public void testMultipleChallengeBasicLongRealm() throws Exception {
- client.getState().setCredentials(null, null, new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 401 Unauthorized\r\n" +
@@ -680,7 +690,8 @@
public void testMultipleChallengeDigest() throws Exception {
- client.getState().setCredentials(null, null, new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setCredentials(AuthScope.ANY,
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 401 Unauthorized\r\n" +
@@ -705,7 +716,9 @@
public void testMultipleProxyChallengeBasic() throws Exception {
- client.getState().setProxyCredentials("Protected", null, new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setProxyCredentials(
+ new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "Protected"),
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
@@ -729,7 +742,9 @@
public void testMultipleProxyChallengeDigest() throws Exception {
- client.getState().setProxyCredentials("Protected", null, new UsernamePasswordCredentials("name", "pass"));
+ client.getState().setProxyCredentials(
+ new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "Protected"),
+ new UsernamePasswordCredentials("name", "pass"));
HttpMethod method = new SimpleHttpMethod();
conn.addResponse(
"HTTP/1.1 407 Proxy Authentication Required\r\n" +
@@ -752,70 +767,4 @@
assertTrue(authValue.startsWith("Digest"));
}
-
- // --------------------------------- Test Methods for Selecting Credentials
-
- public void testDefaultCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials expected = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials(null, null, expected);
- Credentials got = state.getCredentials("realm", "host");
- assertEquals(got, expected);
- }
-
- public void testRealmCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials expected = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials("realm", "host", expected);
- Credentials got = state.getCredentials("realm", "host");
- assertEquals(expected, got);
- }
-
- public void testHostCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials expected = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials(null, "host", expected);
- Credentials got = state.getCredentials("realm", "host");
- assertEquals(expected, got);
- }
-
- public void testBothCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials expected = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials("realm", "host", expected);
- Credentials got = state.getCredentials("realm", "host");
- assertEquals(expected, got);
- }
-
- public void testWrongHostCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials expected = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials(null, "host1", expected);
- Credentials got = state.getCredentials("realm", "host2");
- assertNotSame(expected, got);
- }
-
- public void testWrongRealmCredentials() throws Exception {
- HttpState state = new HttpState();
- Credentials cred = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials("realm1", "host", cred);
- Credentials got = state.getCredentials("realm2", "host");
- assertNotSame(cred, got);
- }
-
- public void testRealmSpoof() throws Exception {
- HttpState state = new HttpState();
- Credentials cred = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials(null, "admin.apache.org", cred);
- Credentials got = state.getCredentials("admin.apache.org", "myhost");
- assertNotSame(cred, got);
- }
-
- public void testRealmSpoof2() throws Exception {
- HttpState state = new HttpState();
- Credentials cred = new UsernamePasswordCredentials("name", "pass");
- state.setCredentials(null, "whatever", cred);
- Credentials got = state.getCredentials("nullwhatever", null);
- assertNotSame(cred, got);
- }
}
1.6 +72 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java
Index: TestHttpState.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestHttpState.java 22 Feb 2004 18:08:49 -0000 1.5
+++ TestHttpState.java 13 Jun 2004 12:13:08 -0000 1.6
@@ -74,8 +74,8 @@
public void testHttpStateCredentials() {
HttpState state = new HttpState();
- state.setCredentials(realm1, null, creds1);
- state.setCredentials(realm2, null, creds2);
+ state.setCredentials(realm1, null, creds1);
+ state.setCredentials(realm2, null, creds2);
assertEquals(creds1, state.getCredentials(realm1, null));
assertEquals(creds2, state.getCredentials(realm2, null));
}
@@ -129,6 +129,72 @@
state.setProxyCredentials(null, null, creds1);
state.setProxyCredentials(realm2, null, creds2);
assertEquals(creds1, state.getProxyCredentials("bogus", null));
+ }
+
+ // --------------------------------- Test Methods for Selecting Credentials
+
+ public void testDefaultCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials expected = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials(null, null, expected);
+ Credentials got = state.getCredentials("realm", "host");
+ assertEquals(got, expected);
+ }
+
+ public void testRealmCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials expected = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials("realm", "host", expected);
+ Credentials got = state.getCredentials("realm", "host");
+ assertEquals(expected, got);
+ }
+
+ public void testHostCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials expected = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials(null, "host", expected);
+ Credentials got = state.getCredentials("realm", "host");
+ assertEquals(expected, got);
+ }
+
+ public void testBothCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials expected = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials("realm", "host", expected);
+ Credentials got = state.getCredentials("realm", "host");
+ assertEquals(expected, got);
+ }
+
+ public void testWrongHostCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials expected = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials(null, "host1", expected);
+ Credentials got = state.getCredentials("realm", "host2");
+ assertNotSame(expected, got);
+ }
+
+ public void testWrongRealmCredentials() throws Exception {
+ HttpState state = new HttpState();
+ Credentials cred = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials("realm1", "host", cred);
+ Credentials got = state.getCredentials("realm2", "host");
+ assertNotSame(cred, got);
+ }
+
+ public void testRealmSpoof() throws Exception {
+ HttpState state = new HttpState();
+ Credentials cred = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials(null, "admin.apache.org", cred);
+ Credentials got = state.getCredentials("admin.apache.org", "myhost");
+ assertNotSame(cred, got);
+ }
+
+ public void testRealmSpoof2() throws Exception {
+ HttpState state = new HttpState();
+ Credentials cred = new UsernamePasswordCredentials("name", "pass");
+ state.setCredentials(null, "whatever", cred);
+ Credentials got = state.getCredentials("nullwhatever", null);
+ assertNotSame(cred, got);
}
}
1.12 +7 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttps.java
Index: TestHttps.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestHttps.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- TestHttps.java 22 Feb 2004 18:08:49 -0000 1.11
+++ TestHttps.java 13 Jun 2004 12:13:08 -0000 1.12
@@ -34,6 +34,7 @@
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
/**
@@ -86,7 +87,7 @@
if (PROXY_HOST != null) {
if (PROXY_USER != null) {
HttpState state = client.getState();
- state.setProxyCredentials(null, null, new UsernamePasswordCredentials(
+ state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
PROXY_USER, PROXY_PASS));
}
client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
@@ -115,7 +116,7 @@
if (PROXY_HOST != null) {
if (PROXY_USER != null) {
HttpState state = client.getState();
- state.setProxyCredentials(null, null, new UsernamePasswordCredentials(
+ state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
PROXY_USER, PROXY_PASS));
}
client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
1.13 +7 -5 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java
Index: TestMethodsExternalHost.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodsExternalHost.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- TestMethodsExternalHost.java 22 Feb 2004 18:08:49 -0000 1.12
+++ TestMethodsExternalHost.java 13 Jun 2004 12:13:08 -0000 1.13
@@ -33,6 +33,8 @@
import java.io.IOException;
import java.util.Enumeration;
import junit.framework.*;
+
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
/**
@@ -88,7 +90,7 @@
if (PROXY_HOST != null) {
if (PROXY_USER != null) {
HttpState state = client.getState();
- state.setProxyCredentials(null, null, new UsernamePasswordCredentials(
+ state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
PROXY_USER, PROXY_PASS));
}
client.getHostConfiguration().setProxy(PROXY_HOST, Integer.parseInt(PROXY_PORT));
1.6 +5 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java
Index: TestProxy.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestProxy.java 22 Feb 2004 18:08:49 -0000 1.5
+++ TestProxy.java 13 Jun 2004 12:13:08 -0000 1.6
@@ -29,6 +29,7 @@
*/
package org.apache.commons.httpclient;
+import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.server.SimpleProxy;
@@ -86,7 +87,7 @@
hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http"));
hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
client.setHostConfiguration(hc);
- client.getState().setProxyCredentials(null, null, creds);
+ client.getState().setProxyCredentials(AuthScope.ANY, creds);
GetMethod get = new GetMethod("/");
client.executeMethod(get);
1.18 +5 -5 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappBasicAuth.java
Index: TestWebappBasicAuth.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappBasicAuth.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- TestWebappBasicAuth.java 12 Jun 2004 22:47:23 -0000 1.17
+++ TestWebappBasicAuth.java 13 Jun 2004 12:13:08 -0000 1.18
@@ -235,7 +235,7 @@
HttpClient client = new HttpClient();
HttpState state = client.getState();
Credentials cred = new UsernamePasswordCredentials("jakarta", "commons");
- state.setCredentials(null, null, cred);
+ state.setCredentials(AuthScope.ANY, cred);
HostConfiguration hc = new HostConfiguration();
hc.setHost(getHost(), getPort(), getProtocol());
client.setHostConfiguration(hc);
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org