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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,7 @@ package org.apache.http.localserver;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.After; import org.junit.After;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -41,9 +42,17 @@ public abstract class BasicServerTestBase extends Mockito {
/** The local server for testing. */ /** The local server for testing. */
protected LocalTestServer localServer; protected LocalTestServer localServer;
protected DefaultHttpClient httpclient;
@After @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) { if (localServer != null) {
localServer.stop(); localServer.stop();
} }