001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v 1.11 2004/12/11 22:35:26 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029: package org.apache.commons.httpclient;
030:
031: import java.io.IOException;
032: import java.util.Enumeration;
033:
034: import junit.extensions.TestSetup;
035: import junit.framework.Test;
036: import junit.framework.TestSuite;
037:
038: import org.apache.commons.httpclient.auth.AuthScheme;
039: import org.apache.commons.httpclient.auth.AuthScope;
040: import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
041: import org.apache.commons.httpclient.auth.CredentialsProvider;
042: import org.apache.commons.httpclient.methods.GetMethod;
043: import org.apache.commons.httpclient.methods.PostMethod;
044: import org.apache.commons.httpclient.methods.StringRequestEntity;
045: import org.apache.commons.httpclient.server.AuthRequestHandler;
046: import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
047: import org.apache.commons.httpclient.server.HttpServiceHandler;
048:
049: /**
050: * Tests for proxied connections.
051: *
052: * @author Ortwin Glueck
053: * @author Oleg Kalnichevski
054: */
055: public class TestProxy extends HttpClientTestBase {
056:
057: public TestProxy(String testName) throws IOException {
058: super (testName);
059: setUseProxy(true);
060: }
061:
062: static class SSLDecorator extends TestSetup {
063:
064: public static void addTests(TestSuite suite) {
065: TestSuite ts2 = new TestSuite();
066: addTest(ts2, suite);
067: suite.addTest(ts2);
068: }
069:
070: private static void addTest(TestSuite suite, Test t) {
071: if (t instanceof TestProxy) {
072: suite.addTest(new SSLDecorator((TestProxy) t));
073: } else if (t instanceof TestSuite) {
074: Enumeration en = ((TestSuite) t).tests();
075: while (en.hasMoreElements()) {
076: addTest(suite, (Test) en.nextElement());
077: }
078: }
079: }
080:
081: public SSLDecorator(TestProxy test) {
082: super (test);
083: }
084:
085: protected void setUp() throws Exception {
086: TestProxy base = (TestProxy) getTest();
087: base.setUseSSL(true);
088: }
089: }
090:
091: public static Test suite() {
092: TestSuite suite = new TestSuite(TestProxy.class);
093: SSLDecorator.addTests(suite);
094: return suite;
095: }
096:
097: class GetItWrongThenGetItRight implements CredentialsProvider {
098:
099: private int hostcount = 0;
100: private int proxycount = 0;
101:
102: public GetItWrongThenGetItRight() {
103: super ();
104: }
105:
106: public Credentials getCredentials(AuthScheme scheme,
107: String host, int port, boolean proxy)
108: throws CredentialsNotAvailableException {
109: if (!proxy) {
110: this .hostcount++;
111: return provideCredentials(this .hostcount);
112: } else {
113: this .proxycount++;
114: return provideCredentials(this .proxycount);
115: }
116: }
117:
118: private Credentials provideCredentials(int count) {
119: switch (count) {
120: case 1:
121: return new UsernamePasswordCredentials("testuser",
122: "wrongstuff");
123: case 2:
124: return new UsernamePasswordCredentials("testuser",
125: "testpass");
126: default:
127: return null;
128: }
129: }
130:
131: }
132:
133: /**
134: * Tests GET via non-authenticating proxy
135: */
136: public void testSimpleGet() throws Exception {
137: this .server.setHttpService(new FeedbackService());
138: GetMethod get = new GetMethod("/");
139: try {
140: this .client.executeMethod(get);
141: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
142: } finally {
143: get.releaseConnection();
144: }
145: }
146:
147: /**
148: * Tests GET via non-authenticating proxy + host auth + connection keep-alive
149: */
150: public void testGetHostAuthConnKeepAlive() throws Exception {
151:
152: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
153: "testuser", "testpass");
154:
155: this .client.getState().setCredentials(AuthScope.ANY, creds);
156:
157: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
158: handlerchain.appendHandler(new AuthRequestHandler(creds,
159: "test", true));
160: handlerchain.appendHandler(new HttpServiceHandler(
161: new FeedbackService()));
162:
163: this .server.setRequestHandler(handlerchain);
164:
165: GetMethod get = new GetMethod("/");
166: try {
167: this .client.executeMethod(get);
168: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
169: } finally {
170: get.releaseConnection();
171: }
172: }
173:
174: /**
175: * Tests GET via non-authenticating proxy + host auth + connection close
176: */
177: public void testGetHostAuthConnClose() throws Exception {
178:
179: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
180: "testuser", "testpass");
181:
182: this .client.getState().setCredentials(AuthScope.ANY, creds);
183:
184: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
185: handlerchain.appendHandler(new AuthRequestHandler(creds,
186: "test", false));
187: handlerchain.appendHandler(new HttpServiceHandler(
188: new FeedbackService()));
189:
190: this .server.setRequestHandler(handlerchain);
191:
192: GetMethod get = new GetMethod("/");
193: try {
194: this .client.executeMethod(get);
195: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
196: } finally {
197: get.releaseConnection();
198: }
199: }
200:
201: /**
202: * Tests GET via non-authenticating proxy + invalid host auth
203: */
204: public void testGetHostInvalidAuth() throws Exception {
205:
206: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
207: "testuser", "testpass");
208:
209: this .client.getState().setCredentials(AuthScope.ANY, creds);
210:
211: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
212: handlerchain.appendHandler(new AuthRequestHandler(creds));
213: handlerchain.appendHandler(new HttpServiceHandler(
214: new FeedbackService()));
215:
216: this .client.getState().setCredentials(
217: AuthScope.ANY,
218: new UsernamePasswordCredentials("testuser",
219: "wrongstuff"));
220:
221: this .server.setRequestHandler(handlerchain);
222:
223: GetMethod get = new GetMethod("/");
224: try {
225: this .client.executeMethod(get);
226: assertEquals(HttpStatus.SC_UNAUTHORIZED, get
227: .getStatusCode());
228: } finally {
229: get.releaseConnection();
230: }
231: }
232:
233: /**
234: * Tests GET via non-authenticating proxy + interactive host auth + connection keep-alive
235: */
236: public void testGetInteractiveHostAuthConnKeepAlive()
237: throws Exception {
238:
239: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
240: "testuser", "testpass");
241:
242: this .client.getParams().setParameter(
243: CredentialsProvider.PROVIDER,
244: new GetItWrongThenGetItRight());
245:
246: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
247: handlerchain.appendHandler(new AuthRequestHandler(creds,
248: "test", true));
249: handlerchain.appendHandler(new HttpServiceHandler(
250: new FeedbackService()));
251:
252: this .server.setRequestHandler(handlerchain);
253:
254: GetMethod get = new GetMethod("/");
255: try {
256: this .client.executeMethod(get);
257: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
258: } finally {
259: get.releaseConnection();
260: }
261: }
262:
263: /**
264: * Tests GET via non-authenticating proxy + interactive host auth + connection close
265: */
266: public void testGetInteractiveHostAuthConnClose() throws Exception {
267:
268: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
269: "testuser", "testpass");
270:
271: this .client.getParams().setParameter(
272: CredentialsProvider.PROVIDER,
273: new GetItWrongThenGetItRight());
274:
275: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
276: handlerchain.appendHandler(new AuthRequestHandler(creds,
277: "test", false));
278: handlerchain.appendHandler(new HttpServiceHandler(
279: new FeedbackService()));
280:
281: this .server.setRequestHandler(handlerchain);
282:
283: GetMethod get = new GetMethod("/");
284: try {
285: this .client.executeMethod(get);
286: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
287: } finally {
288: get.releaseConnection();
289: }
290: }
291:
292: /**
293: * Tests GET via authenticating proxy + host auth + connection keep-alive
294: */
295: public void testGetProxyAuthHostAuthConnKeepAlive()
296: throws Exception {
297:
298: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
299: "testuser", "testpass");
300:
301: this .client.getState().setCredentials(AuthScope.ANY, creds);
302: this .client.getState()
303: .setProxyCredentials(AuthScope.ANY, creds);
304:
305: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
306: handlerchain.appendHandler(new AuthRequestHandler(creds,
307: "test", true));
308: handlerchain.appendHandler(new HttpServiceHandler(
309: new FeedbackService()));
310:
311: this .server.setRequestHandler(handlerchain);
312:
313: this .proxy.requireAuthentication(creds, "test", true);
314:
315: GetMethod get = new GetMethod("/");
316: try {
317: this .client.executeMethod(get);
318: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
319: } finally {
320: get.releaseConnection();
321: }
322: }
323:
324: /**
325: * Tests GET via authenticating proxy
326: */
327: public void testGetAuthProxy() throws Exception {
328: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
329: "testuser", "testpass");
330:
331: this .client.getState()
332: .setProxyCredentials(AuthScope.ANY, creds);
333: this .server.setHttpService(new FeedbackService());
334:
335: this .proxy.requireAuthentication(creds, "test", true);
336:
337: GetMethod get = new GetMethod("/");
338: try {
339: this .client.executeMethod(get);
340: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
341: } finally {
342: get.releaseConnection();
343: }
344: }
345:
346: /**
347: * Tests GET via authenticating proxy + host auth + connection close
348: */
349: public void testGetProxyAuthHostAuthConnClose() throws Exception {
350:
351: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
352: "testuser", "testpass");
353:
354: this .client.getState().setCredentials(AuthScope.ANY, creds);
355: this .client.getState()
356: .setProxyCredentials(AuthScope.ANY, creds);
357:
358: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
359: handlerchain.appendHandler(new AuthRequestHandler(creds,
360: "test", false));
361: handlerchain.appendHandler(new HttpServiceHandler(
362: new FeedbackService()));
363:
364: this .server.setRequestHandler(handlerchain);
365:
366: this .proxy.requireAuthentication(creds, "test", true);
367:
368: GetMethod get = new GetMethod("/");
369: try {
370: this .client.executeMethod(get);
371: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
372: } finally {
373: get.releaseConnection();
374: }
375: }
376:
377: /**
378: * Tests GET via authenticating proxy + invalid host auth
379: */
380: public void testGetProxyAuthHostInvalidAuth() throws Exception {
381:
382: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
383: "testuser", "testpass");
384:
385: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
386: handlerchain.appendHandler(new AuthRequestHandler(creds));
387: handlerchain.appendHandler(new HttpServiceHandler(
388: new FeedbackService()));
389:
390: this .client.getState().setCredentials(
391: AuthScope.ANY,
392: new UsernamePasswordCredentials("testuser",
393: "wrongstuff"));
394: this .client.getState()
395: .setProxyCredentials(AuthScope.ANY, creds);
396:
397: this .server.setRequestHandler(handlerchain);
398:
399: this .proxy.requireAuthentication(creds, "test", true);
400:
401: GetMethod get = new GetMethod("/");
402: try {
403: this .client.executeMethod(get);
404: assertEquals(HttpStatus.SC_UNAUTHORIZED, get
405: .getStatusCode());
406: } finally {
407: get.releaseConnection();
408: }
409: }
410:
411: /**
412: * Tests GET via authenticating proxy + interactive host and proxy auth + connection keep-alive
413: */
414: public void testGetInteractiveProxyAuthHostAuthConnKeepAlive()
415: throws Exception {
416:
417: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
418: "testuser", "testpass");
419:
420: this .client.getParams().setParameter(
421: CredentialsProvider.PROVIDER,
422: new GetItWrongThenGetItRight());
423:
424: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
425: handlerchain.appendHandler(new AuthRequestHandler(creds,
426: "test", true));
427: handlerchain.appendHandler(new HttpServiceHandler(
428: new FeedbackService()));
429:
430: this .server.setRequestHandler(handlerchain);
431:
432: this .proxy.requireAuthentication(creds, "test", true);
433:
434: GetMethod get = new GetMethod("/");
435: try {
436: this .client.executeMethod(get);
437: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
438: } finally {
439: get.releaseConnection();
440: }
441: }
442:
443: /**
444: * Tests GET via authenticating proxy + interactive host and proxy auth + connection close
445: */
446: public void testGetInteractiveProxyAuthHostAuthConnClose()
447: throws Exception {
448:
449: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
450: "testuser", "testpass");
451:
452: this .client.getParams().setParameter(
453: CredentialsProvider.PROVIDER,
454: new GetItWrongThenGetItRight());
455:
456: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
457: handlerchain.appendHandler(new AuthRequestHandler(creds,
458: "test", false));
459: handlerchain.appendHandler(new HttpServiceHandler(
460: new FeedbackService()));
461:
462: this .server.setRequestHandler(handlerchain);
463:
464: this .proxy.requireAuthentication(creds, "test", true);
465:
466: GetMethod get = new GetMethod("/");
467: try {
468: this .client.executeMethod(get);
469: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
470: } finally {
471: get.releaseConnection();
472: }
473: }
474:
475: /**
476: * Tests POST via non-authenticating proxy
477: */
478: public void testSimplePost() throws Exception {
479: this .server.setHttpService(new FeedbackService());
480: PostMethod post = new PostMethod("/");
481: post.setRequestEntity(new StringRequestEntity(
482: "Like tons of stuff", null, null));
483: try {
484: this .client.executeMethod(post);
485: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
486: assertNotNull(post.getResponseBodyAsString());
487: } finally {
488: post.releaseConnection();
489: }
490: }
491:
492: /**
493: * Tests POST via non-authenticating proxy + host auth + connection keep-alive
494: */
495: public void testPostHostAuthConnKeepAlive() throws Exception {
496: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
497: "testuser", "testpass");
498:
499: this .client.getState().setCredentials(AuthScope.ANY, creds);
500:
501: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
502: handlerchain.appendHandler(new AuthRequestHandler(creds,
503: "test", true));
504: handlerchain.appendHandler(new HttpServiceHandler(
505: new FeedbackService()));
506:
507: this .server.setRequestHandler(handlerchain);
508:
509: PostMethod post = new PostMethod("/");
510: post.setRequestEntity(new StringRequestEntity(
511: "Like tons of stuff", null, null));
512: try {
513: this .client.executeMethod(post);
514: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
515: assertNotNull(post.getResponseBodyAsString());
516: } finally {
517: post.releaseConnection();
518: }
519: }
520:
521: /**
522: * Tests POST via non-authenticating proxy + host auth + connection close
523: */
524: public void testPostHostAuthConnClose() throws Exception {
525: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
526: "testuser", "testpass");
527:
528: this .client.getState().setCredentials(AuthScope.ANY, creds);
529:
530: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
531: handlerchain.appendHandler(new AuthRequestHandler(creds,
532: "test", false));
533: handlerchain.appendHandler(new HttpServiceHandler(
534: new FeedbackService()));
535:
536: this .server.setRequestHandler(handlerchain);
537:
538: PostMethod post = new PostMethod("/");
539: post.setRequestEntity(new StringRequestEntity(
540: "Like tons of stuff", null, null));
541: try {
542: this .client.executeMethod(post);
543: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
544: assertNotNull(post.getResponseBodyAsString());
545: } finally {
546: post.releaseConnection();
547: }
548: }
549:
550: /**
551: * Tests POST via non-authenticating proxy + invalid host auth
552: */
553: public void testPostHostInvalidAuth() throws Exception {
554:
555: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
556: "testuser", "testpass");
557:
558: this .client.getState().setCredentials(AuthScope.ANY, creds);
559:
560: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
561: handlerchain.appendHandler(new AuthRequestHandler(creds));
562: handlerchain.appendHandler(new HttpServiceHandler(
563: new FeedbackService()));
564:
565: this .client.getState().setCredentials(
566: AuthScope.ANY,
567: new UsernamePasswordCredentials("testuser",
568: "wrongstuff"));
569:
570: this .server.setRequestHandler(handlerchain);
571:
572: PostMethod post = new PostMethod("/");
573: post.setRequestEntity(new StringRequestEntity(
574: "Like tons of stuff", null, null));
575: try {
576: this .client.executeMethod(post);
577: assertEquals(HttpStatus.SC_UNAUTHORIZED, post
578: .getStatusCode());
579: } finally {
580: post.releaseConnection();
581: }
582: }
583:
584: /**
585: * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive
586: */
587: public void testPostInteractiveHostAuthConnKeepAlive()
588: throws Exception {
589: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
590: "testuser", "testpass");
591:
592: this .client.getParams().setParameter(
593: CredentialsProvider.PROVIDER,
594: new GetItWrongThenGetItRight());
595:
596: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
597: handlerchain.appendHandler(new AuthRequestHandler(creds,
598: "test", true));
599: handlerchain.appendHandler(new HttpServiceHandler(
600: new FeedbackService()));
601:
602: this .server.setRequestHandler(handlerchain);
603:
604: PostMethod post = new PostMethod("/");
605: post.setRequestEntity(new StringRequestEntity(
606: "Like tons of stuff", null, null));
607: try {
608: this .client.executeMethod(post);
609: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
610: assertNotNull(post.getResponseBodyAsString());
611: } finally {
612: post.releaseConnection();
613: }
614: }
615:
616: /**
617: * Tests POST via non-authenticating proxy + interactive host auth + connection close
618: */
619: public void testPostInteractiveHostAuthConnClose() throws Exception {
620: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
621: "testuser", "testpass");
622:
623: this .client.getParams().setParameter(
624: CredentialsProvider.PROVIDER,
625: new GetItWrongThenGetItRight());
626:
627: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
628: handlerchain.appendHandler(new AuthRequestHandler(creds,
629: "test", false));
630: handlerchain.appendHandler(new HttpServiceHandler(
631: new FeedbackService()));
632:
633: this .server.setRequestHandler(handlerchain);
634:
635: PostMethod post = new PostMethod("/");
636: post.setRequestEntity(new StringRequestEntity(
637: "Like tons of stuff", null, null));
638: try {
639: this .client.executeMethod(post);
640: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
641: assertNotNull(post.getResponseBodyAsString());
642: } finally {
643: post.releaseConnection();
644: }
645: }
646:
647: /**
648: * Tests POST via authenticating proxy
649: */
650: public void testPostAuthProxy() throws Exception {
651: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
652: "testuser", "testpass");
653:
654: this .client.getState()
655: .setProxyCredentials(AuthScope.ANY, creds);
656: this .server.setHttpService(new FeedbackService());
657:
658: this .proxy.requireAuthentication(creds, "test", true);
659:
660: PostMethod post = new PostMethod("/");
661: post.setRequestEntity(new StringRequestEntity(
662: "Like tons of stuff", null, null));
663: try {
664: this .client.executeMethod(post);
665: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
666: assertNotNull(post.getResponseBodyAsString());
667: } finally {
668: post.releaseConnection();
669: }
670: }
671:
672: /**
673: * Tests POST via authenticating proxy + host auth + connection keep-alive
674: */
675: public void testPostProxyAuthHostAuthConnKeepAlive()
676: throws Exception {
677: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
678: "testuser", "testpass");
679:
680: this .client.getState().setCredentials(AuthScope.ANY, creds);
681: this .client.getState()
682: .setProxyCredentials(AuthScope.ANY, creds);
683:
684: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
685: handlerchain.appendHandler(new AuthRequestHandler(creds,
686: "test", true));
687: handlerchain.appendHandler(new HttpServiceHandler(
688: new FeedbackService()));
689:
690: this .server.setRequestHandler(handlerchain);
691:
692: this .proxy.requireAuthentication(creds, "test", true);
693:
694: PostMethod post = new PostMethod("/");
695: post.setRequestEntity(new StringRequestEntity(
696: "Like tons of stuff", null, null));
697: try {
698: this .client.executeMethod(post);
699: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
700: assertNotNull(post.getResponseBodyAsString());
701: } finally {
702: post.releaseConnection();
703: }
704: }
705:
706: /**
707: * Tests POST via authenticating proxy + host auth + connection close
708: */
709: public void testPostProxyAuthHostAuthConnClose() throws Exception {
710: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
711: "testuser", "testpass");
712:
713: this .client.getState().setCredentials(AuthScope.ANY, creds);
714: this .client.getState()
715: .setProxyCredentials(AuthScope.ANY, creds);
716:
717: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
718: handlerchain.appendHandler(new AuthRequestHandler(creds,
719: "test", false));
720: handlerchain.appendHandler(new HttpServiceHandler(
721: new FeedbackService()));
722:
723: this .server.setRequestHandler(handlerchain);
724:
725: this .proxy.requireAuthentication(creds, "test", true);
726:
727: PostMethod post = new PostMethod("/");
728: post.setRequestEntity(new StringRequestEntity(
729: "Like tons of stuff", null, null));
730: try {
731: this .client.executeMethod(post);
732: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
733: assertNotNull(post.getResponseBodyAsString());
734: } finally {
735: post.releaseConnection();
736: }
737: }
738:
739: /**
740: * Tests POST via non-authenticating proxy + invalid host auth
741: */
742: public void testPostProxyAuthHostInvalidAuth() throws Exception {
743:
744: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
745: "testuser", "testpass");
746:
747: this .client.getState()
748: .setProxyCredentials(AuthScope.ANY, creds);
749:
750: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
751: handlerchain.appendHandler(new AuthRequestHandler(creds));
752: handlerchain.appendHandler(new HttpServiceHandler(
753: new FeedbackService()));
754:
755: this .client.getState().setCredentials(
756: AuthScope.ANY,
757: new UsernamePasswordCredentials("testuser",
758: "wrongstuff"));
759:
760: this .server.setRequestHandler(handlerchain);
761:
762: this .proxy.requireAuthentication(creds, "test", true);
763:
764: PostMethod post = new PostMethod("/");
765: post.setRequestEntity(new StringRequestEntity(
766: "Like tons of stuff", null, null));
767: try {
768: this .client.executeMethod(post);
769: assertEquals(HttpStatus.SC_UNAUTHORIZED, post
770: .getStatusCode());
771: } finally {
772: post.releaseConnection();
773: }
774: }
775:
776: /**
777: * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive
778: */
779: public void testPostInteractiveProxyAuthHostAuthConnKeepAlive()
780: throws Exception {
781: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
782: "testuser", "testpass");
783:
784: this .client.getParams().setParameter(
785: CredentialsProvider.PROVIDER,
786: new GetItWrongThenGetItRight());
787:
788: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
789: handlerchain.appendHandler(new AuthRequestHandler(creds,
790: "test", true));
791: handlerchain.appendHandler(new HttpServiceHandler(
792: new FeedbackService()));
793:
794: this .server.setRequestHandler(handlerchain);
795:
796: this .proxy.requireAuthentication(creds, "test", true);
797:
798: PostMethod post = new PostMethod("/");
799: post.setRequestEntity(new StringRequestEntity(
800: "Like tons of stuff", null, null));
801: try {
802: this .client.executeMethod(post);
803: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
804: assertNotNull(post.getResponseBodyAsString());
805: } finally {
806: post.releaseConnection();
807: }
808: }
809:
810: /**
811: * Tests POST via non-authenticating proxy + interactive host auth + connection close
812: */
813: public void testPostInteractiveProxyAuthHostAuthConnClose()
814: throws Exception {
815: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
816: "testuser", "testpass");
817:
818: this .client.getParams().setParameter(
819: CredentialsProvider.PROVIDER,
820: new GetItWrongThenGetItRight());
821:
822: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
823: handlerchain.appendHandler(new AuthRequestHandler(creds,
824: "test", false));
825: handlerchain.appendHandler(new HttpServiceHandler(
826: new FeedbackService()));
827:
828: this .server.setRequestHandler(handlerchain);
829:
830: this .proxy.requireAuthentication(creds, "test", true);
831:
832: PostMethod post = new PostMethod("/");
833: post.setRequestEntity(new StringRequestEntity(
834: "Like tons of stuff", null, null));
835: try {
836: this .client.executeMethod(post);
837: assertEquals(HttpStatus.SC_OK, post.getStatusCode());
838: assertNotNull(post.getResponseBodyAsString());
839: } finally {
840: post.releaseConnection();
841: }
842: }
843:
844: public void testPreemptiveAuthProxy() throws Exception {
845: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
846: "testuser", "testpass");
847:
848: this .client.getState()
849: .setProxyCredentials(AuthScope.ANY, creds);
850: this .client.getParams().setAuthenticationPreemptive(true);
851: this .server.setHttpService(new FeedbackService());
852:
853: this .proxy.requireAuthentication(creds, "test", true);
854:
855: GetMethod get = new GetMethod("/");
856: try {
857: this .client.executeMethod(get);
858: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
859: if (isUseSSL()) {
860: assertNull(get.getRequestHeader("Proxy-Authorization"));
861: } else {
862: assertNotNull(get
863: .getRequestHeader("Proxy-Authorization"));
864: }
865: } finally {
866: get.releaseConnection();
867: }
868: }
869:
870: /**
871: * Tests GET via authenticating proxy + host auth + HTTP/1.0
872: */
873: public void testGetProxyAuthHostAuthHTTP10() throws Exception {
874:
875: UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
876: "testuser", "testpass");
877:
878: this .client.getState().setCredentials(AuthScope.ANY, creds);
879: this .client.getState()
880: .setProxyCredentials(AuthScope.ANY, creds);
881: this .client.getParams().setVersion(HttpVersion.HTTP_1_0);
882:
883: HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
884: handlerchain.appendHandler(new AuthRequestHandler(creds,
885: "test", true));
886: handlerchain.appendHandler(new HttpServiceHandler(
887: new FeedbackService()));
888:
889: this .server.setRequestHandler(handlerchain);
890:
891: this .proxy.requireAuthentication(creds, "test", false);
892:
893: GetMethod get = new GetMethod("/");
894: try {
895: this.client.executeMethod(get);
896: assertEquals(HttpStatus.SC_OK, get.getStatusCode());
897: } finally {
898: get.releaseConnection();
899: }
900: }
901:
902: }
|