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 2009/03/30 13:34:34 UTC
svn commit: r759926 - in
/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http:
client/protocol/ conn/ssl/ impl/client/ localserver/
Author: olegk
Date: Mon Mar 30 11:34:33 2009
New Revision: 759926
URL: http://svn.apache.org/viewvc?rev=759926&view=rev
Log:
Added test coverage for authentication
Added:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java (with props)
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java (with props)
Modified:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/CertificatesToPlayWith.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestHostnameVerifier.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestCookie2Support.java Mon Mar 30 11:34:33 2009
@@ -51,7 +51,8 @@
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
@@ -63,7 +64,7 @@
*
* @version $Revision$
*/
-public class TestCookie2Support extends ServerTestBase {
+public class TestCookie2Support extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestCookie2Support(final String testName) {
@@ -82,6 +83,13 @@
return new TestSuite(TestCookie2Support.class);
}
+ @Override
+ protected void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ }
+
private static class CookieVer0Service implements HttpRequestHandler {
public void handle(
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestRedirects.java Mon Mar 30 11:34:33 2009
@@ -54,7 +54,8 @@
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.cookie.BasicClientCookie;
-import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
@@ -70,7 +71,7 @@
*
* @version $Revision$
*/
-public class TestRedirects extends ServerTestBase {
+public class TestRedirects extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestRedirects(final String testName) {
@@ -89,6 +90,13 @@
return new TestSuite(TestRedirects.class);
}
+ @Override
+ protected void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ }
+
private class BasicRedirectService implements HttpRequestHandler {
private int statuscode = HttpStatus.SC_MOVED_TEMPORARILY;
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/client/protocol/TestUriEscapes.java Mon Mar 30 11:34:33 2009
@@ -43,11 +43,12 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
-public class TestUriEscapes extends ServerTestBase {
+public class TestUriEscapes extends BasicServerTestBase {
public TestUriEscapes(final String testName) {
super(testName);
@@ -62,6 +63,13 @@
return new TestSuite(TestUriEscapes.class);
}
+ @Override
+ protected void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ }
+
private class UriListeningService implements HttpRequestHandler {
private volatile String requestedUri;
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/CertificatesToPlayWith.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/CertificatesToPlayWith.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/CertificatesToPlayWith.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/CertificatesToPlayWith.java Mon Mar 30 11:34:33 2009
@@ -43,7 +43,7 @@
*
* @since 11-Dec-2006
*/
-public interface CertificatesToPlayWith {
+public class CertificatesToPlayWith {
/**
* CN=foo.com
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestHostnameVerifier.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestHostnameVerifier.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestHostnameVerifier.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestHostnameVerifier.java Mon Mar 30 11:34:33 2009
@@ -44,11 +44,8 @@
/**
* Unit tests for {@link X509HostnameVerifier}.
- *
- * @since 11-Dec-2006
*/
-public class TestHostnameVerifier extends TestCase
- implements CertificatesToPlayWith {
+public class TestHostnameVerifier extends TestCase {
public TestHostnameVerifier(String testName) {
super(testName);
@@ -60,9 +57,7 @@
}
public static Test suite() {
- TestSuite ts = new TestSuite();
- ts.addTestSuite(TestHostnameVerifier.class);
- return ts;
+ return new TestSuite(TestHostnameVerifier.class);
}
public void testVerify() throws Exception {
@@ -72,7 +67,7 @@
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in;
X509Certificate x509;
- in = new ByteArrayInputStream(X509_FOO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("foo.com", x509);
@@ -85,14 +80,14 @@
ALLOW_ALL.verify("a.foo.com", x509);
ALLOW_ALL.verify("bar.com", x509);
- in = new ByteArrayInputStream(X509_HANAKO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_HANAKO);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("\u82b1\u5b50.co.jp", x509);
STRICT.verify("\u82b1\u5b50.co.jp", x509);
exceptionPlease(DEFAULT, "a.\u82b1\u5b50.co.jp", x509);
exceptionPlease(STRICT, "a.\u82b1\u5b50.co.jp", x509);
- in = new ByteArrayInputStream(X509_FOO_BAR);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO_BAR);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("foo.com", x509);
STRICT.verify("foo.com", x509);
@@ -103,7 +98,7 @@
exceptionPlease(DEFAULT, "a.bar.com", x509);
exceptionPlease(STRICT, "a.bar.com", x509);
- in = new ByteArrayInputStream(X509_FOO_BAR_HANAKO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO_BAR_HANAKO);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("foo.com", x509);
STRICT.verify("foo.com", x509);
@@ -123,21 +118,21 @@
exceptionPlease(DEFAULT, "a.\u82b1\u5b50.co.jp", x509);
exceptionPlease(STRICT, "a.\u82b1\u5b50.co.jp", x509);
- in = new ByteArrayInputStream(X509_NO_CNS_FOO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_NO_CNS_FOO);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("foo.com", x509);
STRICT.verify("foo.com", x509);
exceptionPlease(DEFAULT, "a.foo.com", x509);
exceptionPlease(STRICT, "a.foo.com", x509);
- in = new ByteArrayInputStream(X509_NO_CNS_FOO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_NO_CNS_FOO);
x509 = (X509Certificate) cf.generateCertificate(in);
DEFAULT.verify("foo.com", x509);
STRICT.verify("foo.com", x509);
exceptionPlease(DEFAULT, "a.foo.com", x509);
exceptionPlease(STRICT, "a.foo.com", x509);
- in = new ByteArrayInputStream(X509_THREE_CNS_FOO_BAR_HANAKO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_THREE_CNS_FOO_BAR_HANAKO);
x509 = (X509Certificate) cf.generateCertificate(in);
exceptionPlease(DEFAULT, "foo.com", x509);
exceptionPlease(STRICT, "foo.com", x509);
@@ -152,7 +147,7 @@
exceptionPlease(DEFAULT, "a.\u82b1\u5b50.co.jp", x509);
exceptionPlease(STRICT, "a.\u82b1\u5b50.co.jp", x509);
- in = new ByteArrayInputStream(X509_WILD_FOO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_FOO);
x509 = (X509Certificate) cf.generateCertificate(in);
exceptionPlease(DEFAULT, "foo.com", x509);
exceptionPlease(STRICT, "foo.com", x509);
@@ -163,7 +158,7 @@
DEFAULT.verify("a.b.foo.com", x509);
exceptionPlease(STRICT, "a.b.foo.com", x509);
- in = new ByteArrayInputStream(X509_WILD_CO_JP);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_CO_JP);
x509 = (X509Certificate) cf.generateCertificate(in);
// Silly test because no-one would ever be able to lookup an IP address
// using "*.co.jp".
@@ -174,7 +169,7 @@
exceptionPlease(DEFAULT, "\u82b1\u5b50.co.jp", x509);
exceptionPlease(STRICT, "\u82b1\u5b50.co.jp", x509);
- in = new ByteArrayInputStream(X509_WILD_FOO_BAR_HANAKO);
+ in = new ByteArrayInputStream(CertificatesToPlayWith.X509_WILD_FOO_BAR_HANAKO);
x509 = (X509Certificate) cf.generateCertificate(in);
// try the foo.com variations
exceptionPlease(DEFAULT, "foo.com", x509);
@@ -211,7 +206,7 @@
public void testSubjectAlt() throws Exception {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
- InputStream in = new ByteArrayInputStream(X509_MULTIPLE_SUBJECT_ALT);
+ InputStream in = new ByteArrayInputStream(CertificatesToPlayWith.X509_MULTIPLE_SUBJECT_ALT);
X509Certificate x509 = (X509Certificate) cf.generateCertificate(in);
X509HostnameVerifier verifier = SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER;
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/conn/ssl/TestSSLSocketFactory.java Mon Mar 30 11:34:33 2009
@@ -64,11 +64,8 @@
/**
* Unit tests for {@link SSLSocketFactory}.
- *
- * @since 8-Dec-2006
*/
-public class TestSSLSocketFactory extends TestCase
- implements CertificatesToPlayWith {
+public class TestSSLSocketFactory extends TestCase {
public TestSSLSocketFactory(String testName) {
super(testName);
@@ -80,9 +77,7 @@
}
public static Test suite() {
- TestSuite ts = new TestSuite();
- ts.addTestSuite(TestSSLSocketFactory.class);
- return ts;
+ return new TestSuite(TestSSLSocketFactory.class);
}
static class TestX509HostnameVerifier implements X509HostnameVerifier {
@@ -114,14 +109,14 @@
char[] pwd = password.toCharArray();
RSAPrivateCrtKeySpec k;
- k = new RSAPrivateCrtKeySpec(new BigInteger(RSA_PUBLIC_MODULUS, 16),
- new BigInteger(RSA_PUBLIC_EXPONENT, 10),
- new BigInteger(RSA_PRIVATE_EXPONENT, 16),
- new BigInteger(RSA_PRIME1, 16),
- new BigInteger(RSA_PRIME2, 16),
- new BigInteger(RSA_EXPONENT1, 16),
- new BigInteger(RSA_EXPONENT2, 16),
- new BigInteger(RSA_COEFFICIENT, 16));
+ k = new RSAPrivateCrtKeySpec(new BigInteger(CertificatesToPlayWith.RSA_PUBLIC_MODULUS, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_PUBLIC_EXPONENT, 10),
+ new BigInteger(CertificatesToPlayWith.RSA_PRIVATE_EXPONENT, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_PRIME1, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_PRIME2, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_EXPONENT1, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_EXPONENT2, 16),
+ new BigInteger(CertificatesToPlayWith.RSA_COEFFICIENT, 16));
PrivateKey pk = KeyFactory.getInstance("RSA").generatePrivate(k);
KeyStore ks = KeyStore.getInstance("JKS");
@@ -129,9 +124,9 @@
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream in1, in2, in3;
- in1 = new ByteArrayInputStream(X509_FOO);
- in2 = new ByteArrayInputStream(X509_INTERMEDIATE_CA);
- in3 = new ByteArrayInputStream(X509_ROOT_CA);
+ in1 = new ByteArrayInputStream(CertificatesToPlayWith.X509_FOO);
+ in2 = new ByteArrayInputStream(CertificatesToPlayWith.X509_INTERMEDIATE_CA);
+ in3 = new ByteArrayInputStream(CertificatesToPlayWith.X509_ROOT_CA);
X509Certificate[] chain = new X509Certificate[3];
chain[0] = (X509Certificate) cf.generateCertificate(in1);
chain[1] = (X509Certificate) cf.generateCertificate(in2);
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java?rev=759926&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java Mon Mar 30 11:34:33 2009
@@ -0,0 +1,201 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+
+package org.apache.http.impl.client;
+
+import java.io.IOException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
+import org.apache.http.localserver.RequestBasicAuth;
+import org.apache.http.localserver.ResponseBasicUnauthorized;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.ResponseConnControl;
+import org.apache.http.protocol.ResponseContent;
+import org.apache.http.protocol.ResponseDate;
+import org.apache.http.protocol.ResponseServer;
+
+/**
+ * Unit tests for automatic client authentication.
+ */
+public class TestClientAuthentication extends BasicServerTestBase {
+
+ public TestClientAuthentication(final String testName) {
+ super(testName);
+ }
+
+ public static void main(String args[]) {
+ String[] testCaseName = { TestClientAuthentication.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestClientAuthentication.class);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ BasicHttpProcessor httpproc = new BasicHttpProcessor();
+ httpproc.addInterceptor(new ResponseDate());
+ httpproc.addInterceptor(new ResponseServer());
+ httpproc.addInterceptor(new ResponseContent());
+ httpproc.addInterceptor(new ResponseConnControl());
+ httpproc.addInterceptor(new RequestBasicAuth());
+ httpproc.addInterceptor(new ResponseBasicUnauthorized());
+
+ localServer = new LocalTestServer(httpproc, null);
+ }
+
+ static class AuthHandler implements HttpRequestHandler {
+
+ public void handle(
+ final HttpRequest request,
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+ String creds = (String) context.getAttribute("creds");
+ if (creds == null || !creds.equals("test:test")) {
+ response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+ } else {
+ response.setStatusCode(HttpStatus.SC_OK);
+ StringEntity entity = new StringEntity("success", HTTP.ASCII);
+ response.setEntity(entity);
+ }
+ }
+
+ }
+
+ static class TestCredentialsProvider implements CredentialsProvider {
+
+ private final Credentials creds;
+ private AuthScope authscope;
+
+ TestCredentialsProvider(final Credentials creds) {
+ super();
+ this.creds = creds;
+ }
+
+ public void clear() {
+ }
+
+ public Credentials getCredentials(AuthScope authscope) {
+ this.authscope = authscope;
+ return this.creds;
+ }
+
+ public void setCredentials(AuthScope authscope, Credentials credentials) {
+ }
+
+ public AuthScope getAuthScope() {
+ return this.authscope;
+ }
+
+ }
+
+ public void testBasicAuthenticationNoCreds() throws Exception {
+ localServer.register("*", new AuthHandler());
+ localServer.start();
+
+ TestCredentialsProvider credsProvider = new TestCredentialsProvider(null);
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ httpclient.setCredentialsProvider(credsProvider);
+
+ HttpGet httpget = new HttpGet("/");
+
+ HttpResponse response = httpclient.execute(getServerHttp(), httpget);
+ HttpEntity entity = response.getEntity();
+ assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
+ assertNotNull(entity);
+ entity.consumeContent();
+ AuthScope authscope = credsProvider.getAuthScope();
+ assertNotNull(authscope);
+ assertEquals("test realm", authscope.getRealm());
+ }
+
+ public void testBasicAuthenticationFailure() throws Exception {
+ localServer.register("*", new AuthHandler());
+ localServer.start();
+
+ TestCredentialsProvider credsProvider = new TestCredentialsProvider(
+ new UsernamePasswordCredentials("test", "all-wrong"));
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ httpclient.setCredentialsProvider(credsProvider);
+
+ HttpGet httpget = new HttpGet("/");
+
+ HttpResponse response = httpclient.execute(getServerHttp(), httpget);
+ HttpEntity entity = response.getEntity();
+ assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
+ assertNotNull(entity);
+ entity.consumeContent();
+ AuthScope authscope = credsProvider.getAuthScope();
+ assertNotNull(authscope);
+ assertEquals("test realm", authscope.getRealm());
+ }
+
+ public void testBasicAuthenticationSuccess() throws Exception {
+ localServer.register("*", new AuthHandler());
+ localServer.start();
+
+ TestCredentialsProvider credsProvider = new TestCredentialsProvider(
+ new UsernamePasswordCredentials("test", "test"));
+
+ DefaultHttpClient httpclient = new DefaultHttpClient();
+ httpclient.setCredentialsProvider(credsProvider);
+
+ HttpGet httpget = new HttpGet("/");
+
+ HttpResponse response = httpclient.execute(getServerHttp(), httpget);
+ HttpEntity entity = response.getEntity();
+ assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
+ assertNotNull(entity);
+ entity.consumeContent();
+ AuthScope authscope = credsProvider.getAuthScope();
+ assertNotNull(authscope);
+ assertEquals("test realm", authscope.getRealm());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java Mon Mar 30 11:34:33 2009
@@ -69,7 +69,8 @@
import org.apache.http.impl.conn.ClientConnAdapterMockup;
import org.apache.http.impl.conn.SingleClientConnManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
-import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
import org.apache.http.message.BasicHeader;
import org.apache.http.mockup.SocketFactoryMockup;
import org.apache.http.params.BasicHttpParams;
@@ -83,7 +84,7 @@
/**
* Unit tests for {@link DefaultRequestDirector}
*/
-public class TestDefaultClientRequestDirector extends ServerTestBase {
+public class TestDefaultClientRequestDirector extends BasicServerTestBase {
public TestDefaultClientRequestDirector(final String testName) {
super(testName);
@@ -98,6 +99,13 @@
return new TestSuite(TestDefaultClientRequestDirector.class);
}
+ @Override
+ protected void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ }
+
/**
* Tests that if abort is called on an {@link AbortableHttpRequest} while
* {@link DefaultRequestDirector} is allocating a connection, that the
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestRequestWrapper.java Mon Mar 30 11:34:33 2009
@@ -41,7 +41,8 @@
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
-import org.apache.http.localserver.ServerTestBase;
+import org.apache.http.localserver.BasicServerTestBase;
+import org.apache.http.localserver.LocalTestServer;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
@@ -52,7 +53,7 @@
*
* @version $Revision$
*/
-public class TestRequestWrapper extends ServerTestBase {
+public class TestRequestWrapper extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestRequestWrapper(final String testName) {
@@ -71,6 +72,13 @@
return new TestSuite(TestRequestWrapper.class);
}
+ @Override
+ protected void setUp() throws Exception {
+ localServer = new LocalTestServer(null, null);
+ localServer.registerDefaultHandlers();
+ localServer.start();
+ }
+
private class SimpleService implements HttpRequestHandler {
public SimpleService() {
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java?rev=759926&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java Mon Mar 30 11:34:33 2009
@@ -0,0 +1,79 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.localserver;
+
+import junit.framework.TestCase;
+
+import org.apache.http.HttpHost;
+import org.apache.http.conn.routing.HttpRoute;
+
+/**
+ * Base class for tests using {@link LocalTestServer}. The server will not be started
+ * per default.
+ */
+public abstract class BasicServerTestBase extends TestCase {
+
+ /** The local server for testing. */
+ protected LocalTestServer localServer;
+
+ protected BasicServerTestBase(String testName) {
+ super(testName);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ if (localServer != null) {
+ localServer.stop();
+ }
+ }
+
+ /**
+ * Obtains the address of the local test server.
+ *
+ * @return the test server host, with a scheme name of "http"
+ */
+ protected HttpHost getServerHttp() {
+
+ return new HttpHost(LocalTestServer.TEST_SERVER_ADDR.getHostName(),
+ localServer.getServicePort(),
+ "http");
+ }
+
+ /**
+ * Obtains the default route to the local test server.
+ *
+ * @return the default route to the local test server
+ */
+ protected HttpRoute getDefaultRoute() {
+ return new HttpRoute(getServerHttp());
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/BasicServerTestBase.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/LocalTestServer.java Mon Mar 30 11:34:33 2009
@@ -186,7 +186,7 @@
HttpParams params = new BasicHttpParams();
params
.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
- 5000)
+ 60000)
.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
8 * 1024)
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java?rev=759926&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java Mon Mar 30 11:34:33 2009
@@ -0,0 +1,84 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.localserver;
+
+import java.io.IOException;
+
+import org.apache.commons.codec.BinaryDecoder;
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.http.Header;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpRequestInterceptor;
+import org.apache.http.ProtocolException;
+import org.apache.http.auth.AUTH;
+import org.apache.http.protocol.HTTP;
+import org.apache.http.protocol.HttpContext;
+
+public class RequestBasicAuth implements HttpRequestInterceptor {
+
+ public void process(
+ final HttpRequest request,
+ final HttpContext context) throws HttpException, IOException {
+
+ String auth = null;
+
+ Header h = request.getFirstHeader(AUTH.WWW_AUTH_RESP);
+ if (h != null) {
+ String s = h.getValue();
+ if (s != null) {
+ auth = s.trim();
+ }
+ }
+
+ if (auth != null) {
+ int i = auth.indexOf(' ');
+ if (i == -1) {
+ throw new ProtocolException("Invalid Authorization header: " + auth);
+ }
+ String authscheme = auth.substring(0, i);
+ if (authscheme.equalsIgnoreCase("basic")) {
+ String s = auth.substring(i + 1).trim();
+ byte[] credsRaw = s.getBytes(HTTP.ASCII);
+ BinaryDecoder codec = new Base64();
+ try {
+ String creds = new String(codec.decode(credsRaw), HTTP.ASCII);
+ context.setAttribute("creds", creds);
+ } catch (DecoderException ex) {
+ throw new ProtocolException("Malformed BASIC credentials");
+ }
+ }
+ }
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/RequestBasicAuth.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java?rev=759926&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java Mon Mar 30 11:34:33 2009
@@ -0,0 +1,53 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.localserver;
+
+import java.io.IOException;
+
+import org.apache.http.HttpException;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpResponseInterceptor;
+import org.apache.http.HttpStatus;
+import org.apache.http.auth.AUTH;
+import org.apache.http.protocol.HttpContext;
+
+public class ResponseBasicUnauthorized implements HttpResponseInterceptor {
+
+ public void process(
+ final HttpResponse response,
+ final HttpContext context) throws HttpException, IOException {
+ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
+ response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\"");
+ }
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java?rev=759926&r1=759925&r2=759926&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ServerTestBase.java Mon Mar 30 11:34:33 2009
@@ -32,11 +32,8 @@
import java.net.Socket;
-import junit.framework.TestCase;
-
import org.apache.http.HttpHost;
import org.apache.http.HttpVersion;
-import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
@@ -45,8 +42,8 @@
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
-import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.BasicHttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
@@ -60,10 +57,7 @@
* server. If you have some tests that require the server and others
* that don't, split them in two different classes.
*/
-public abstract class ServerTestBase extends TestCase {
-
- /** The local server for testing. */
- protected LocalTestServer localServer;
+public abstract class ServerTestBase extends BasicServerTestBase {
/** The available schemes. */
protected SchemeRegistry supportedSchemes;
@@ -165,30 +159,6 @@
localServer.stop();
}
-
- /**
- * Obtains the address of the local test server.
- *
- * @return the test server host, with a scheme name of "http"
- */
- protected HttpHost getServerHttp() {
-
- return new HttpHost(LocalTestServer.TEST_SERVER_ADDR.getHostName(),
- localServer.getServicePort(),
- "http");
- }
-
-
- /**
- * Obtains the default route to the local test server.
- *
- * @return the default route to the local test server
- */
- protected HttpRoute getDefaultRoute() {
- return new HttpRoute(getServerHttp());
- }
-
-
/**
* Opens a connection to the given target using
* {@link #defaultParams default parameters}.