Added test coverage for authentication

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@759926 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-03-30 11:34:33 +00:00
parent 165507b8d7
commit e2520f8509
14 changed files with 497 additions and 80 deletions

View File

@ -51,7 +51,8 @@ import org.apache.http.cookie.SetCookie2;
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 @@ import org.apache.http.protocol.HttpRequestHandler;
*
* @version $Revision$
*/
public class TestCookie2Support extends ServerTestBase {
public class TestCookie2Support extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestCookie2Support(final String testName) {
@ -82,6 +83,13 @@ public class TestCookie2Support extends ServerTestBase {
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(

View File

@ -54,7 +54,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.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 @@ import junit.framework.TestSuite;
*
* @version $Revision$
*/
public class TestRedirects extends ServerTestBase {
public class TestRedirects extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestRedirects(final String testName) {
@ -89,6 +90,13 @@ public class TestRedirects extends ServerTestBase {
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;

View File

@ -43,11 +43,12 @@ import org.apache.http.ProtocolVersion;
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 @@ public class TestUriEscapes extends ServerTestBase {
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;

View File

@ -43,7 +43,7 @@ package org.apache.http.conn.ssl;
*
* @since 11-Dec-2006
*/
public interface CertificatesToPlayWith {
public class CertificatesToPlayWith {
/**
* CN=foo.com

View File

@ -44,11 +44,8 @@ import junit.framework.TestSuite;
/**
* 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 class TestHostnameVerifier extends TestCase
}
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 @@ public class TestHostnameVerifier extends TestCase
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 class TestHostnameVerifier extends TestCase
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;

View File

@ -64,11 +64,8 @@ import org.apache.http.localserver.LocalTestServer;
/**
* 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 class TestSSLSocketFactory extends TestCase
}
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 @@ public class TestSSLSocketFactory extends TestCase
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 @@ public class TestSSLSocketFactory extends TestCase
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);

View File

@ -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());
}
}

View File

@ -69,7 +69,8 @@ import org.apache.http.entity.StringEntity;
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 @@ import org.apache.http.protocol.HttpRequestHandler;
/**
* 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 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
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

View File

@ -41,7 +41,8 @@ import org.apache.http.HttpResponse;
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 @@ import org.apache.http.protocol.HttpRequestHandler;
*
* @version $Revision$
*/
public class TestRequestWrapper extends ServerTestBase {
public class TestRequestWrapper extends BasicServerTestBase {
// ------------------------------------------------------------ Constructor
public TestRequestWrapper(final String testName) {
@ -71,6 +72,13 @@ public class TestRequestWrapper extends ServerTestBase {
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() {

View File

@ -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());
}
}

View File

@ -186,7 +186,7 @@ public class LocalTestServer {
HttpParams params = new BasicHttpParams();
params
.setIntParameter(CoreConnectionPNames.SO_TIMEOUT,
5000)
60000)
.setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,
8 * 1024)
.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,

View File

@ -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");
}
}
}
}
}

View File

@ -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\"");
}
}
}

View File

@ -32,11 +32,8 @@ package org.apache.http.localserver;
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.impl.DefaultHttpClientConnection;
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 @@ import org.apache.http.protocol.RequestContent;
* 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 @@ public abstract class ServerTestBase extends TestCase {
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}.