TestProxy xref

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