Always shut down HttpClient used in local server based tests

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1174702 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-09-23 12:31:31 +00:00
parent 28fbd4df7c
commit 04626cb5ed
9 changed files with 144 additions and 167 deletions

View File

@ -64,9 +64,10 @@ public class TestCookie2Support extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
private static class CookieVer0Service implements HttpRequestHandler {
@ -88,8 +89,7 @@ public class TestCookie2Support extends BasicServerTestBase {
public void testCookieVersionSupportHeader1() throws Exception {
this.localServer.register("*", new CookieVer0Service());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
this.httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
@ -97,7 +97,7 @@ public class TestCookie2Support extends BasicServerTestBase {
HttpGet httpget = new HttpGet("/test/");
HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
HttpResponse response1 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
@ -105,7 +105,7 @@ public class TestCookie2Support extends BasicServerTestBase {
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
HttpResponse response2 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
@ -136,8 +136,7 @@ public class TestCookie2Support extends BasicServerTestBase {
public void testCookieVersionSupportHeader2() throws Exception {
this.localServer.register("*", new CookieVer1Service());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
this.httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
@ -145,7 +144,7 @@ public class TestCookie2Support extends BasicServerTestBase {
HttpGet httpget = new HttpGet("/test/");
HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
HttpResponse response1 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
@ -153,7 +152,7 @@ public class TestCookie2Support extends BasicServerTestBase {
Assert.assertNotNull(cookies);
Assert.assertEquals(2, cookies.size());
HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
HttpResponse response2 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
@ -183,8 +182,7 @@ public class TestCookie2Support extends BasicServerTestBase {
public void testCookieVersionSupportHeader3() throws Exception {
this.localServer.register("*", new CookieVer2Service());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
this.httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
@ -192,7 +190,7 @@ public class TestCookie2Support extends BasicServerTestBase {
HttpGet httpget = new HttpGet("/test/");
HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
HttpResponse response1 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);
@ -200,7 +198,7 @@ public class TestCookie2Support extends BasicServerTestBase {
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
HttpResponse response2 = client.execute(getServerHttp(), httpget, context);
HttpResponse response2 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e2 = response2.getEntity();
EntityUtils.consume(e2);
@ -231,8 +229,7 @@ public class TestCookie2Support extends BasicServerTestBase {
public void testSetCookieVersionMix() throws Exception {
this.localServer.register("*", new SetCookieVersionMixService());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
this.httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BEST_MATCH);
CookieStore cookieStore = new BasicCookieStore();
HttpContext context = new BasicHttpContext();
@ -240,7 +237,7 @@ public class TestCookie2Support extends BasicServerTestBase {
HttpGet httpget = new HttpGet("/test/");
HttpResponse response1 = client.execute(getServerHttp(), httpget, context);
HttpResponse response1 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity e1 = response1.getEntity();
EntityUtils.consume(e1);

View File

@ -71,9 +71,10 @@ public class TestRedirects extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
private static class BasicRedirectService implements HttpRequestHandler {
@ -225,12 +226,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_MULTIPLE_CHOICES));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -248,12 +248,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_MOVED_PERMANENTLY));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -275,12 +274,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_MOVED_TEMPORARILY));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -310,12 +308,11 @@ public class TestRedirects extends BasicServerTestBase {
});
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -337,12 +334,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_SEE_OTHER));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -364,12 +360,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_NOT_MODIFIED));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -387,12 +382,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_USE_PROXY));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -410,12 +404,11 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port, HttpStatus.SC_TEMPORARY_REDIRECT));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -433,13 +426,12 @@ public class TestRedirects extends BasicServerTestBase {
public void testMaxRedirectCheck() throws Exception {
this.localServer.register("*", new CircularRedirectService());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
client.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 5);
this.httpclient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
this.httpclient.getParams().setIntParameter(ClientPNames.MAX_REDIRECTS, 5);
HttpGet httpget = new HttpGet("/circular-oldlocation/");
try {
client.execute(getServerHttp(), httpget);
this.httpclient.execute(getServerHttp(), httpget);
} catch (ClientProtocolException e) {
Assert.assertTrue(e.getCause() instanceof RedirectException);
throw e;
@ -450,13 +442,12 @@ public class TestRedirects extends BasicServerTestBase {
public void testCircularRedirect() throws Exception {
this.localServer.register("*", new CircularRedirectService());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);
this.httpclient.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);
HttpGet httpget = new HttpGet("/circular-oldlocation/");
try {
client.execute(getServerHttp(), httpget);
this.httpclient.execute(getServerHttp(), httpget);
} catch (ClientProtocolException e) {
Assert.assertTrue(e.getCause() instanceof CircularRedirectException);
throw e;
@ -470,13 +461,12 @@ public class TestRedirects extends BasicServerTestBase {
String host = address.getHostName();
this.localServer.register("*", new BasicRedirectService(host, port));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpPost httppost = new HttpPost("/oldlocation/");
httppost.setEntity(new StringEntity("stuff"));
HttpResponse response = client.execute(getServerHttp(), httppost, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httppost, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -495,13 +485,12 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*", new BasicRedirectService(host, port,
HttpStatus.SC_SEE_OTHER));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpPost httppost = new HttpPost("/oldlocation/");
httppost.setEntity(new StringEntity("stuff"));
HttpResponse response = client.execute(getServerHttp(), httppost, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httppost, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -519,14 +508,13 @@ public class TestRedirects extends BasicServerTestBase {
String host = address.getHostName();
this.localServer.register("*", new RelativeRedirectService());
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
client.getParams().setBooleanParameter(
this.httpclient.getParams().setBooleanParameter(
ClientPNames.REJECT_RELATIVE_REDIRECT, false);
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -547,14 +535,13 @@ public class TestRedirects extends BasicServerTestBase {
String host = address.getHostName();
this.localServer.register("*", new RelativeRedirectService2());
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
client.getParams().setBooleanParameter(
this.httpclient.getParams().setBooleanParameter(
ClientPNames.REJECT_RELATIVE_REDIRECT, false);
HttpGet httpget = new HttpGet("/test/oldlocation");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -572,14 +559,12 @@ public class TestRedirects extends BasicServerTestBase {
public void testRejectRelativeRedirect() throws Exception {
this.localServer.register("*", new RelativeRedirectService());
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setBooleanParameter(
this.httpclient.getParams().setBooleanParameter(
ClientPNames.REJECT_RELATIVE_REDIRECT, true);
HttpGet httpget = new HttpGet("/oldlocation/");
try {
client.execute(getServerHttp(), httpget);
this.httpclient.execute(getServerHttp(), httpget);
} catch (ClientProtocolException e) {
Assert.assertTrue(e.getCause() instanceof ProtocolException);
throw e;
@ -590,11 +575,9 @@ public class TestRedirects extends BasicServerTestBase {
public void testRejectBogusRedirectLocation() throws Exception {
this.localServer.register("*", new BogusRedirectService("xxx://bogus"));
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("/oldlocation/");
client.execute(getServerHttp(), httpget);
this.httpclient.execute(getServerHttp(), httpget);
}
@Test(expected=ClientProtocolException.class)
@ -605,12 +588,10 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BogusRedirectService("http://"+ host +":"+ port +"/newlocation/?p=I have spaces"));
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("/oldlocation/");
try {
client.execute(getServerHttp(), httpget);
this.httpclient.execute(getServerHttp(), httpget);
} catch (ClientProtocolException e) {
Assert.assertTrue(e.getCause() instanceof ProtocolException);
throw e;
@ -626,10 +607,8 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port));
DefaultHttpClient client = new DefaultHttpClient();
CookieStore cookieStore = new BasicCookieStore();
client.setCookieStore(cookieStore);
this.httpclient.setCookieStore(cookieStore);
BasicClientCookie cookie = new BasicClientCookie("name", "value");
cookie.setDomain(host);
@ -641,7 +620,7 @@ public class TestRedirects extends BasicServerTestBase {
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -663,18 +642,17 @@ public class TestRedirects extends BasicServerTestBase {
this.localServer.register("*",
new BasicRedirectService(host, port));
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
List<Header> defaultHeaders = new ArrayList<Header>(1);
defaultHeaders.add(new BasicHeader(HTTP.USER_AGENT, "my-test-client"));
client.getParams().setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);
this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);
HttpGet httpget = new HttpGet("/oldlocation/");
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(

View File

@ -51,9 +51,10 @@ public class TestUriEscapes extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
private static class UriListeningService implements HttpRequestHandler {
@ -83,18 +84,17 @@ public class TestUriEscapes extends BasicServerTestBase {
UriListeningService listener = new UriListeningService();
this.localServer.register("*", listener);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response;
if(!relative) {
String request = "http://" + host + ":" + port + uri;
HttpGet httpget = new HttpGet(request);
response = client.execute(httpget);
response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
} else {
HttpHost target = new HttpHost(host, port);
HttpGet httpget = new HttpGet(uri);
response = client.execute(target, httpget);
response = this.httpclient.execute(target, httpget);
EntityUtils.consume(response.getEntity());
}

View File

@ -108,6 +108,7 @@ public class TestSSLSocketFactory extends BasicServerTestBase {
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
@Override
@ -150,12 +151,11 @@ public class TestSSLSocketFactory extends BasicServerTestBase {
SSLSocketFactory socketFactory = new SSLSocketFactory(this.clientSSLContext, hostVerifier);
Scheme https = new Scheme("https", 443, socketFactory);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(https);
this.httpclient.getConnectionManager().getSchemeRegistry().register(https);
HttpHost target = getServerHttp();
HttpGet httpget = new HttpGet("/random/100");
HttpResponse response = httpclient.execute(target, httpget);
HttpResponse response = this.httpclient.execute(target, httpget);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Assert.assertTrue(hostVerifier.isFired());
}
@ -170,12 +170,11 @@ public class TestSSLSocketFactory extends BasicServerTestBase {
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 443, socketFactory);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(https);
this.httpclient.getConnectionManager().getSchemeRegistry().register(https);
HttpHost target = getServerHttp();
HttpGet httpget = new HttpGet("/random/100");
httpclient.execute(target, httpget);
this.httpclient.execute(target, httpget);
}
@Test
@ -194,12 +193,11 @@ public class TestSSLSocketFactory extends BasicServerTestBase {
}, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 443, socketFactory);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getConnectionManager().getSchemeRegistry().register(https);
this.httpclient.getConnectionManager().getSchemeRegistry().register(https);
HttpHost target = getServerHttp();
HttpGet httpget = new HttpGet("/random/100");
HttpResponse response = httpclient.execute(target, httpget);
HttpResponse response = this.httpclient.execute(target, httpget);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
}

View File

@ -68,10 +68,11 @@ public class TestNegotiateScheme extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
this.localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
/**
@ -159,20 +160,19 @@ public class TestNegotiateScheme extends BasicServerTestBase {
this.localServer.register("*", new PleaseNegotiateService());
HttpHost target = new HttpHost("localhost", port);
DefaultHttpClient client = new DefaultHttpClient();
NegotiateSchemeFactory nsf = new NegotiateSchemeFactoryWithMockGssManager();
client.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
this.httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
Credentials use_jaas_creds = new UseJaasCredentials();
client.getCredentialsProvider().setCredentials(
this.httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1, null), use_jaas_creds);
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
String s = "/path";
HttpGet httpget = new HttpGet(s);
HttpResponse response = client.execute(httpget);
HttpResponse response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
@ -188,20 +188,19 @@ public class TestNegotiateScheme extends BasicServerTestBase {
this.localServer.register("*", new PleaseNegotiateService());
HttpHost target = new HttpHost("localhost", port);
DefaultHttpClient client = new DefaultHttpClient();
NegotiateSchemeFactoryWithMockGssManager nsf = new NegotiateSchemeFactoryWithMockGssManager();
client.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
this.httpclient.getAuthSchemes().register(AuthPolicy.SPNEGO, nsf);
Credentials use_jaas_creds = new UseJaasCredentials();
client.getCredentialsProvider().setCredentials(
this.httpclient.getCredentialsProvider().setCredentials(
new AuthScope(null, -1, null), use_jaas_creds);
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
String s = "/path";
HttpGet httpget = new HttpGet(s);
HttpResponse response = client.execute(httpget);
HttpResponse response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());

View File

@ -80,7 +80,8 @@ public class TestClientAuthentication extends BasicServerTestBase {
httpproc.addInterceptor(new RequestBasicAuth());
httpproc.addInterceptor(new ResponseBasicUnauthorized());
localServer = new LocalTestServer(httpproc, null);
this.localServer = new LocalTestServer(httpproc, null);
this.httpclient = new DefaultHttpClient();
}
static class AuthHandler implements HttpRequestHandler {
@ -153,17 +154,17 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test
public void testBasicAuthenticationNoCreds() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(null);
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
HttpResponse response = httpclient.execute(getServerHttp(), httpget);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
@ -175,18 +176,18 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test
public void testBasicAuthenticationFailure() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "all-wrong"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
HttpResponse response = httpclient.execute(getServerHttp(), httpget);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
@ -198,18 +199,18 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test
public void testBasicAuthenticationSuccess() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpGet httpget = new HttpGet("/");
HttpResponse response = httpclient.execute(getServerHttp(), httpget);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
@ -228,16 +229,16 @@ public class TestClientAuthentication extends BasicServerTestBase {
httpproc.addInterceptor(new ResponseConnControl());
httpproc.addInterceptor(new RequestBasicAuth());
httpproc.addInterceptor(new ResponseBasicUnauthorized());
localServer = new LocalTestServer(
this.localServer = new LocalTestServer(
httpproc, null, null, new AuthExpectationVerifier(), null, null);
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpPut httpput = new HttpPut("/");
httpput.setEntity(new InputStreamEntity(
@ -246,7 +247,7 @@ public class TestClientAuthentication extends BasicServerTestBase {
-1));
httpput.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, true);
HttpResponse response = httpclient.execute(getServerHttp(), httpput);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpput);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
@ -254,14 +255,14 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test(expected=ClientProtocolException.class)
public void testBasicAuthenticationFailureOnNonRepeatablePutDontExpectContinue() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpPut httpput = new HttpPut("/");
httpput.setEntity(new InputStreamEntity(
@ -271,7 +272,7 @@ public class TestClientAuthentication extends BasicServerTestBase {
httpput.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
try {
httpclient.execute(getServerHttp(), httpput);
this.httpclient.execute(getServerHttp(), httpput);
Assert.fail("ClientProtocolException should have been thrown");
} catch (ClientProtocolException ex) {
Throwable cause = ex.getCause();
@ -283,19 +284,19 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test
public void testBasicAuthenticationSuccessOnRepeatablePost() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpPost httppost = new HttpPost("/");
httppost.setEntity(new StringEntity("some important stuff", HTTP.ISO_8859_1));
HttpResponse response = httpclient.execute(getServerHttp(), httppost);
HttpResponse response = this.httpclient.execute(getServerHttp(), httppost);
HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
@ -307,14 +308,14 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test(expected=ClientProtocolException.class)
public void testBasicAuthenticationFailureOnNonRepeatablePost() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setCredentialsProvider(credsProvider);
HttpPost httppost = new HttpPost("/");
httppost.setEntity(new InputStreamEntity(
@ -322,7 +323,7 @@ public class TestClientAuthentication extends BasicServerTestBase {
new byte[] { 0,1,2,3,4,5,6,7,8,9 }), -1));
try {
httpclient.execute(getServerHttp(), httppost);
this.httpclient.execute(getServerHttp(), httppost);
Assert.fail("ClientProtocolException should have been thrown");
} catch (ClientProtocolException ex) {
Throwable cause = ex.getCause();
@ -364,8 +365,8 @@ public class TestClientAuthentication extends BasicServerTestBase {
@Test
public void testBasicAuthenticationCredentialsCaching() throws Exception {
localServer.register("*", new AuthHandler());
localServer.start();
this.localServer.register("*", new AuthHandler());
this.localServer.start();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY,
@ -373,21 +374,20 @@ public class TestClientAuthentication extends BasicServerTestBase {
TestTargetAuthenticationHandler authHandler = new TestTargetAuthenticationHandler();
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.setCredentialsProvider(credsProvider);
httpclient.setTargetAuthenticationHandler(authHandler);
this.httpclient.setCredentialsProvider(credsProvider);
this.httpclient.setTargetAuthenticationHandler(authHandler);
HttpContext context = new BasicHttpContext();
HttpGet httpget = new HttpGet("/");
HttpResponse response1 = httpclient.execute(getServerHttp(), httpget, context);
HttpResponse response1 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity entity1 = response1.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
Assert.assertNotNull(entity1);
EntityUtils.consume(entity1);
HttpResponse response2 = httpclient.execute(getServerHttp(), httpget, context);
HttpResponse response2 = this.httpclient.execute(getServerHttp(), httpget, context);
HttpEntity entity2 = response1.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
Assert.assertNotNull(entity2);

View File

@ -84,9 +84,10 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
/**
@ -547,13 +548,12 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
HttpHost target = new HttpHost("localhost", port);
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target);
String s = "/path";
HttpGet httpget = new HttpGet(s);
HttpResponse response = client.execute(httpget);
HttpResponse response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}
@ -569,8 +569,7 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
String s = "http://localhost:" + port;
HttpGet httpget = new HttpGet(s);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -595,10 +594,9 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
String s = "http://localhost:" + port;
HttpGet httpget = new HttpGet(s);
DefaultHttpClient client = new DefaultHttpClient();
String virtHost = "virtual";
httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost, port));
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -625,11 +623,10 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
String s = "http://localhost:" + port;
HttpGet httpget = new HttpGet(s);
DefaultHttpClient client = new DefaultHttpClient();
String virtHost = "virtual";
int virtPort = 9876;
httpget.getParams().setParameter(ClientPNames.VIRTUAL_HOST, new HttpHost(virtHost, virtPort));
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -651,14 +648,13 @@ public class TestDefaultClientRequestDirector extends BasicServerTestBase {
HttpHost target1 = new HttpHost("whatever", 80);
HttpHost target2 = new HttpHost("localhost", port);
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.DEFAULT_HOST, target1);
this.httpclient.getParams().setParameter(ClientPNames.DEFAULT_HOST, target1);
String s = "/path";
HttpGet httpget = new HttpGet(s);
httpget.getParams().setParameter(ClientPNames.DEFAULT_HOST, target2);
HttpResponse response = client.execute(httpget);
HttpResponse response = this.httpclient.execute(httpget);
EntityUtils.consume(response.getEntity());
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}

View File

@ -52,9 +52,10 @@ public class TestRequestWrapper extends BasicServerTestBase {
@Before
public void setUp() throws Exception {
localServer = new LocalTestServer(null, null);
localServer.registerDefaultHandlers();
localServer.start();
this.localServer = new LocalTestServer(null, null);
this.localServer.registerDefaultHandlers();
this.localServer.start();
this.httpclient = new DefaultHttpClient();
}
private static class SimpleService implements HttpRequestHandler {
@ -78,13 +79,13 @@ public class TestRequestWrapper extends BasicServerTestBase {
int port = this.localServer.getServiceAddress().getPort();
this.localServer.register("*", new SimpleService());
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
String s = "http://localhost:" + port + "/path";
HttpGet httpget = new HttpGet(s);
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(
@ -101,13 +102,12 @@ public class TestRequestWrapper extends BasicServerTestBase {
int port = this.localServer.getServiceAddress().getPort();
this.localServer.register("*", new SimpleService());
DefaultHttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
String s = "http://localhost:" + port;
HttpGet httpget = new HttpGet(s);
HttpResponse response = client.execute(getServerHttp(), httpget, context);
HttpResponse response = this.httpclient.execute(getServerHttp(), httpget, context);
EntityUtils.consume(response.getEntity());
HttpRequest reqWrapper = (HttpRequest) context.getAttribute(

View File

@ -30,6 +30,7 @@ package org.apache.http.localserver;
import java.net.InetSocketAddress;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.After;
import org.mockito.Mockito;
@ -41,9 +42,17 @@ public abstract class BasicServerTestBase extends Mockito {
/** The local server for testing. */
protected LocalTestServer localServer;
protected DefaultHttpClient httpclient;
@After
public void tearDown() throws Exception {
public void shutDownClient() throws Exception {
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
}
}
@After
public void shutDownServer() throws Exception {
if (localServer != null) {
localServer.stop();
}