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