1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.commons.httpclient;
29
30 import java.util.Enumeration;
31
32 import org.apache.commons.httpclient.auth.AuthScheme;
33 import org.apache.commons.httpclient.auth.AuthScope;
34 import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
35 import org.apache.commons.httpclient.auth.CredentialsProvider;
36 import org.apache.commons.httpclient.methods.GetMethod;
37 import org.apache.commons.httpclient.methods.PostMethod;
38 import org.apache.commons.httpclient.methods.StringRequestEntity;
39 import org.apache.commons.httpclient.protocol.Protocol;
40 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
41 import org.apache.commons.httpclient.server.AuthRequestHandler;
42 import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
43 import org.apache.commons.httpclient.server.HttpServiceHandler;
44 import org.apache.commons.httpclient.server.SimpleHttpServer;
45 import org.apache.commons.httpclient.server.SimplePlainSocketFactory;
46 import org.apache.commons.httpclient.server.SimpleProxy;
47 import org.apache.commons.httpclient.server.SimpleSocketFactory;
48 import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory;
49 import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory;
50
51 import junit.extensions.TestSetup;
52 import junit.framework.Test;
53 import junit.framework.TestCase;
54 import junit.framework.TestSuite;
55
56 /***
57 * Tests for proxied connections.
58 *
59 * @author Ortwin Glueck
60 * @author Oleg Kalnichevski
61 */
62 public class TestProxy extends TestCase {
63
64 private SimpleProxy proxy = null;
65 private SimpleHttpServer httpserver = null;
66 private HttpClient httpclient = null;
67 private boolean usessl = false;
68
69 public TestProxy(String testName) {
70 super(testName);
71 }
72
73 static class SSLDecorator extends TestSetup {
74
75 public static void addTests(TestSuite suite) {
76 TestSuite ts2 = new TestSuite();
77 addTest(ts2, suite);
78 suite.addTest(ts2);
79 }
80
81 private static void addTest(TestSuite suite, Test t) {
82 if (t instanceof TestProxy) {
83 suite.addTest(new SSLDecorator((TestProxy) t));
84 } else if (t instanceof TestSuite) {
85 Enumeration en = ((TestSuite) t).tests();
86 while (en.hasMoreElements()) {
87 addTest(suite, (Test) en.nextElement());
88 }
89 }
90 }
91
92 public SSLDecorator(TestProxy test) {
93 super(test);
94 }
95
96 protected void setUp() throws Exception {
97 TestProxy base = (TestProxy)getTest();
98 base.setUseSSL(true);
99 }
100 }
101
102 public static Test suite() {
103 TestSuite suite = new TestSuite(TestProxy.class);
104 SSLDecorator.addTests(suite);
105 return suite;
106 }
107
108 protected void setUp() throws Exception {
109 super.setUp();
110 this.httpclient = new HttpClient();
111 this.proxy = new SimpleProxy();
112 this.httpclient.getHostConfiguration().setProxy(
113 this.proxy.getLocalAddress(),
114 this.proxy.getLocalPort());
115
116 SimpleSocketFactory serversocketfactory = null;
117 Protocol testhttp = null;
118 if (this.usessl) {
119 serversocketfactory = new SimpleSSLSocketFactory();
120 testhttp = new Protocol("https",
121 (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
122 } else {
123 serversocketfactory = new SimplePlainSocketFactory();
124 testhttp = Protocol.getProtocol("http");
125 }
126 this.httpserver = new SimpleHttpServer(serversocketfactory, 0);
127 this.httpclient.getHostConfiguration().setHost(
128 this.httpserver.getLocalAddress(),
129 this.httpserver.getLocalPort(),
130 testhttp);
131 }
132
133 protected void tearDown() throws Exception {
134 this.httpclient = null;
135 this.proxy.destroy();
136 this.proxy = null;
137 this.httpserver.destroy();
138 this.httpserver = null;
139 super.tearDown();
140 }
141
142 public void setUseSSL(boolean b) {
143 this.usessl = b;
144 }
145
146 public boolean isUseSSL() {
147 return this.usessl;
148 }
149
150 class GetItWrongThenGetItRight implements CredentialsProvider {
151
152 private int hostcount = 0;
153 private int proxycount = 0;
154
155 public GetItWrongThenGetItRight() {
156 super();
157 }
158
159 public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy)
160 throws CredentialsNotAvailableException {
161 if (!proxy) {
162 this.hostcount++;
163 return provideCredentials(this.hostcount);
164 } else {
165 this.proxycount++;
166 return provideCredentials(this.proxycount);
167 }
168 }
169
170 private Credentials provideCredentials(int count) {
171 switch (count) {
172 case 1:
173 return new UsernamePasswordCredentials("testuser", "wrongstuff");
174 case 2:
175 return new UsernamePasswordCredentials("testuser", "testpass");
176 default:
177 return null;
178 }
179 }
180
181 }
182
183 /***
184 * Tests GET via non-authenticating proxy
185 */
186 public void testSimpleGet() throws Exception {
187 this.httpserver.setHttpService(new FeedbackService());
188 GetMethod get = new GetMethod("/");
189 try {
190 this.httpclient.executeMethod(get);
191 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
192 } finally {
193 get.releaseConnection();
194 }
195 }
196
197 /***
198 * Tests GET via non-authenticating proxy + host auth + connection keep-alive
199 */
200 public void testGetHostAuthConnKeepAlive() throws Exception {
201
202 UsernamePasswordCredentials creds =
203 new UsernamePasswordCredentials("testuser", "testpass");
204
205 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
206
207 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
208 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
209 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
210
211 this.httpserver.setRequestHandler(handlerchain);
212
213 GetMethod get = new GetMethod("/");
214 try {
215 this.httpclient.executeMethod(get);
216 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
217 } finally {
218 get.releaseConnection();
219 }
220 }
221
222 /***
223 * Tests GET via non-authenticating proxy + host auth + connection close
224 */
225 public void testGetHostAuthConnClose() throws Exception {
226
227 UsernamePasswordCredentials creds =
228 new UsernamePasswordCredentials("testuser", "testpass");
229
230 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
231
232 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
233 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
234 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
235
236 this.httpserver.setRequestHandler(handlerchain);
237
238 GetMethod get = new GetMethod("/");
239 try {
240 this.httpclient.executeMethod(get);
241 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
242 } finally {
243 get.releaseConnection();
244 }
245 }
246
247 /***
248 * Tests GET via non-authenticating proxy + invalid host auth
249 */
250 public void testGetHostInvalidAuth() throws Exception {
251
252 UsernamePasswordCredentials creds =
253 new UsernamePasswordCredentials("testuser", "testpass");
254
255 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
256
257 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
258 handlerchain.appendHandler(new AuthRequestHandler(creds));
259 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
260
261 this.httpclient.getState().setCredentials(AuthScope.ANY,
262 new UsernamePasswordCredentials("testuser", "wrongstuff"));
263
264 this.httpserver.setRequestHandler(handlerchain);
265
266 GetMethod get = new GetMethod("/");
267 try {
268 this.httpclient.executeMethod(get);
269 assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
270 } finally {
271 get.releaseConnection();
272 }
273 }
274
275 /***
276 * Tests GET via non-authenticating proxy + interactive host auth + connection keep-alive
277 */
278 public void testGetInteractiveHostAuthConnKeepAlive() throws Exception {
279
280 UsernamePasswordCredentials creds =
281 new UsernamePasswordCredentials("testuser", "testpass");
282
283 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
284 new GetItWrongThenGetItRight());
285
286 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
287 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
288 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
289
290 this.httpserver.setRequestHandler(handlerchain);
291
292 GetMethod get = new GetMethod("/");
293 try {
294 this.httpclient.executeMethod(get);
295 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
296 } finally {
297 get.releaseConnection();
298 }
299 }
300
301 /***
302 * Tests GET via non-authenticating proxy + interactive host auth + connection close
303 */
304 public void testGetInteractiveHostAuthConnClose() throws Exception {
305
306 UsernamePasswordCredentials creds =
307 new UsernamePasswordCredentials("testuser", "testpass");
308
309 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
310 new GetItWrongThenGetItRight());
311
312 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
313 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
314 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
315
316 this.httpserver.setRequestHandler(handlerchain);
317
318 GetMethod get = new GetMethod("/");
319 try {
320 this.httpclient.executeMethod(get);
321 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
322 } finally {
323 get.releaseConnection();
324 }
325 }
326
327 /***
328 * Tests GET via authenticating proxy + host auth + connection keep-alive
329 */
330 public void testGetProxyAuthHostAuthConnKeepAlive() throws Exception {
331
332 UsernamePasswordCredentials creds =
333 new UsernamePasswordCredentials("testuser", "testpass");
334
335 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
336 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
337
338 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
339 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
340 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
341
342 this.httpserver.setRequestHandler(handlerchain);
343
344 this.proxy.requireAuthentication(creds, "test", true);
345
346 GetMethod get = new GetMethod("/");
347 try {
348 this.httpclient.executeMethod(get);
349 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
350 } finally {
351 get.releaseConnection();
352 }
353 }
354
355 /***
356 * Tests GET via authenticating proxy
357 */
358 public void testGetAuthProxy() throws Exception {
359 UsernamePasswordCredentials creds =
360 new UsernamePasswordCredentials("testuser", "testpass");
361
362 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
363 this.httpserver.setHttpService(new FeedbackService());
364
365 this.proxy.requireAuthentication(creds, "test", true);
366
367 GetMethod get = new GetMethod("/");
368 try {
369 this.httpclient.executeMethod(get);
370 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
371 } finally {
372 get.releaseConnection();
373 }
374 }
375
376 /***
377 * Tests GET via authenticating proxy + host auth + connection close
378 */
379 public void testGetProxyAuthHostAuthConnClose() throws Exception {
380
381 UsernamePasswordCredentials creds =
382 new UsernamePasswordCredentials("testuser", "testpass");
383
384 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
385 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
386
387 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
388 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
389 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
390
391 this.httpserver.setRequestHandler(handlerchain);
392
393 this.proxy.requireAuthentication(creds, "test", true);
394
395 GetMethod get = new GetMethod("/");
396 try {
397 this.httpclient.executeMethod(get);
398 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
399 } finally {
400 get.releaseConnection();
401 }
402 }
403
404 /***
405 * Tests GET via authenticating proxy + invalid host auth
406 */
407 public void testGetProxyAuthHostInvalidAuth() throws Exception {
408
409 UsernamePasswordCredentials creds =
410 new UsernamePasswordCredentials("testuser", "testpass");
411
412 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
413 handlerchain.appendHandler(new AuthRequestHandler(creds));
414 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
415
416 this.httpclient.getState().setCredentials(AuthScope.ANY,
417 new UsernamePasswordCredentials("testuser", "wrongstuff"));
418 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
419
420 this.httpserver.setRequestHandler(handlerchain);
421
422 this.proxy.requireAuthentication(creds, "test", true);
423
424 GetMethod get = new GetMethod("/");
425 try {
426 this.httpclient.executeMethod(get);
427 assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
428 } finally {
429 get.releaseConnection();
430 }
431 }
432
433 /***
434 * Tests GET via authenticating proxy + interactive host and proxy auth + connection keep-alive
435 */
436 public void testGetInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception {
437
438 UsernamePasswordCredentials creds =
439 new UsernamePasswordCredentials("testuser", "testpass");
440
441 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
442 new GetItWrongThenGetItRight());
443
444 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
445 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
446 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
447
448 this.httpserver.setRequestHandler(handlerchain);
449
450 this.proxy.requireAuthentication(creds, "test", true);
451
452 GetMethod get = new GetMethod("/");
453 try {
454 this.httpclient.executeMethod(get);
455 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
456 } finally {
457 get.releaseConnection();
458 }
459 }
460
461 /***
462 * Tests GET via authenticating proxy + interactive host and proxy auth + connection close
463 */
464 public void testGetInteractiveProxyAuthHostAuthConnClose() throws Exception {
465
466 UsernamePasswordCredentials creds =
467 new UsernamePasswordCredentials("testuser", "testpass");
468
469 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
470 new GetItWrongThenGetItRight());
471
472 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
473 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
474 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
475
476 this.httpserver.setRequestHandler(handlerchain);
477
478 this.proxy.requireAuthentication(creds, "test", true);
479
480 GetMethod get = new GetMethod("/");
481 try {
482 this.httpclient.executeMethod(get);
483 assertEquals(HttpStatus.SC_OK, get.getStatusCode());
484 } finally {
485 get.releaseConnection();
486 }
487 }
488
489 /***
490 * Tests POST via non-authenticating proxy
491 */
492 public void testSimplePost() throws Exception {
493 this.httpserver.setHttpService(new FeedbackService());
494 PostMethod post = new PostMethod("/");
495 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
496 try {
497 this.httpclient.executeMethod(post);
498 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
499 assertNotNull(post.getResponseBodyAsString());
500 } finally {
501 post.releaseConnection();
502 }
503 }
504
505 /***
506 * Tests POST via non-authenticating proxy + host auth + connection keep-alive
507 */
508 public void testPostHostAuthConnKeepAlive() throws Exception {
509 UsernamePasswordCredentials creds =
510 new UsernamePasswordCredentials("testuser", "testpass");
511
512 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
513
514 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
515 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
516 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
517
518 this.httpserver.setRequestHandler(handlerchain);
519
520 PostMethod post = new PostMethod("/");
521 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
522 try {
523 this.httpclient.executeMethod(post);
524 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
525 assertNotNull(post.getResponseBodyAsString());
526 } finally {
527 post.releaseConnection();
528 }
529 }
530
531 /***
532 * Tests POST via non-authenticating proxy + host auth + connection close
533 */
534 public void testPostHostAuthConnClose() throws Exception {
535 UsernamePasswordCredentials creds =
536 new UsernamePasswordCredentials("testuser", "testpass");
537
538 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
539
540 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
541 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
542 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
543
544 this.httpserver.setRequestHandler(handlerchain);
545
546 PostMethod post = new PostMethod("/");
547 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
548 try {
549 this.httpclient.executeMethod(post);
550 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
551 assertNotNull(post.getResponseBodyAsString());
552 } finally {
553 post.releaseConnection();
554 }
555 }
556
557 /***
558 * Tests POST via non-authenticating proxy + invalid host auth
559 */
560 public void testPostHostInvalidAuth() throws Exception {
561
562 UsernamePasswordCredentials creds =
563 new UsernamePasswordCredentials("testuser", "testpass");
564
565 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
566
567 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
568 handlerchain.appendHandler(new AuthRequestHandler(creds));
569 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
570
571 this.httpclient.getState().setCredentials(AuthScope.ANY,
572 new UsernamePasswordCredentials("testuser", "wrongstuff"));
573
574 this.httpserver.setRequestHandler(handlerchain);
575
576 PostMethod post = new PostMethod("/");
577 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
578 try {
579 this.httpclient.executeMethod(post);
580 assertEquals(HttpStatus.SC_UNAUTHORIZED, post.getStatusCode());
581 } finally {
582 post.releaseConnection();
583 }
584 }
585
586 /***
587 * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive
588 */
589 public void testPostInteractiveHostAuthConnKeepAlive() throws Exception {
590 UsernamePasswordCredentials creds =
591 new UsernamePasswordCredentials("testuser", "testpass");
592
593 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
594 new GetItWrongThenGetItRight());
595
596 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
597 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
598 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
599
600 this.httpserver.setRequestHandler(handlerchain);
601
602 PostMethod post = new PostMethod("/");
603 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
604 try {
605 this.httpclient.executeMethod(post);
606 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
607 assertNotNull(post.getResponseBodyAsString());
608 } finally {
609 post.releaseConnection();
610 }
611 }
612
613 /***
614 * Tests POST via non-authenticating proxy + interactive host auth + connection close
615 */
616 public void testPostInteractiveHostAuthConnClose() throws Exception {
617 UsernamePasswordCredentials creds =
618 new UsernamePasswordCredentials("testuser", "testpass");
619
620 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
621 new GetItWrongThenGetItRight());
622
623 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
624 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
625 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
626
627 this.httpserver.setRequestHandler(handlerchain);
628
629 PostMethod post = new PostMethod("/");
630 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
631 try {
632 this.httpclient.executeMethod(post);
633 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
634 assertNotNull(post.getResponseBodyAsString());
635 } finally {
636 post.releaseConnection();
637 }
638 }
639
640 /***
641 * Tests POST via authenticating proxy
642 */
643 public void testPostAuthProxy() throws Exception {
644 UsernamePasswordCredentials creds =
645 new UsernamePasswordCredentials("testuser", "testpass");
646
647 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
648 this.httpserver.setHttpService(new FeedbackService());
649
650 this.proxy.requireAuthentication(creds, "test", true);
651
652 PostMethod post = new PostMethod("/");
653 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
654 try {
655 this.httpclient.executeMethod(post);
656 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
657 assertNotNull(post.getResponseBodyAsString());
658 } finally {
659 post.releaseConnection();
660 }
661 }
662
663 /***
664 * Tests POST via authenticating proxy + host auth + connection keep-alive
665 */
666 public void testPostProxyAuthHostAuthConnKeepAlive() throws Exception {
667 UsernamePasswordCredentials creds =
668 new UsernamePasswordCredentials("testuser", "testpass");
669
670 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
671 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
672
673 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
674 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
675 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
676
677 this.httpserver.setRequestHandler(handlerchain);
678
679 this.proxy.requireAuthentication(creds, "test", true);
680
681 PostMethod post = new PostMethod("/");
682 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
683 try {
684 this.httpclient.executeMethod(post);
685 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
686 assertNotNull(post.getResponseBodyAsString());
687 } finally {
688 post.releaseConnection();
689 }
690 }
691
692 /***
693 * Tests POST via authenticating proxy + host auth + connection close
694 */
695 public void testPostProxyAuthHostAuthConnClose() throws Exception {
696 UsernamePasswordCredentials creds =
697 new UsernamePasswordCredentials("testuser", "testpass");
698
699 this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
700 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
701
702 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
703 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
704 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
705
706 this.httpserver.setRequestHandler(handlerchain);
707
708 this.proxy.requireAuthentication(creds, "test", true);
709
710 PostMethod post = new PostMethod("/");
711 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
712 try {
713 this.httpclient.executeMethod(post);
714 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
715 assertNotNull(post.getResponseBodyAsString());
716 } finally {
717 post.releaseConnection();
718 }
719 }
720
721 /***
722 * Tests POST via non-authenticating proxy + invalid host auth
723 */
724 public void testPostProxyAuthHostInvalidAuth() throws Exception {
725
726 UsernamePasswordCredentials creds =
727 new UsernamePasswordCredentials("testuser", "testpass");
728
729 this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
730
731 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
732 handlerchain.appendHandler(new AuthRequestHandler(creds));
733 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
734
735 this.httpclient.getState().setCredentials(AuthScope.ANY,
736 new UsernamePasswordCredentials("testuser", "wrongstuff"));
737
738 this.httpserver.setRequestHandler(handlerchain);
739
740 this.proxy.requireAuthentication(creds, "test", true);
741
742 PostMethod post = new PostMethod("/");
743 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
744 try {
745 this.httpclient.executeMethod(post);
746 assertEquals(HttpStatus.SC_UNAUTHORIZED, post.getStatusCode());
747 } finally {
748 post.releaseConnection();
749 }
750 }
751
752 /***
753 * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive
754 */
755 public void testPostInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception {
756 UsernamePasswordCredentials creds =
757 new UsernamePasswordCredentials("testuser", "testpass");
758
759 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
760 new GetItWrongThenGetItRight());
761
762 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
763 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
764 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
765
766 this.httpserver.setRequestHandler(handlerchain);
767
768 this.proxy.requireAuthentication(creds, "test", true);
769
770 PostMethod post = new PostMethod("/");
771 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
772 try {
773 this.httpclient.executeMethod(post);
774 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
775 assertNotNull(post.getResponseBodyAsString());
776 } finally {
777 post.releaseConnection();
778 }
779 }
780
781 /***
782 * Tests POST via non-authenticating proxy + interactive host auth + connection close
783 */
784 public void testPostInteractiveProxyAuthHostAuthConnClose() throws Exception {
785 UsernamePasswordCredentials creds =
786 new UsernamePasswordCredentials("testuser", "testpass");
787
788 this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER,
789 new GetItWrongThenGetItRight());
790
791 HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
792 handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
793 handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
794
795 this.httpserver.setRequestHandler(handlerchain);
796
797 this.proxy.requireAuthentication(creds, "test", true);
798
799 PostMethod post = new PostMethod("/");
800 post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
801 try {
802 this.httpclient.executeMethod(post);
803 assertEquals(HttpStatus.SC_OK, post.getStatusCode());
804 assertNotNull(post.getResponseBodyAsString());
805 } finally {
806 post.releaseConnection();
807 }
808 }
809
810 }