1   /*
2    * ====================================================================
3    *
4    *  Copyright 2003-2004 The Apache Software Foundation
5    *
6    *  Licensed under the Apache License, Version 2.0 (the "License");
7    *  you may not use this file except in compliance with the License.
8    *  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   * ====================================================================
18   *
19   * This software consists of voluntary contributions made by many
20   * individuals on behalf of the Apache Software Foundation.  For more
21   * information on the Apache Software Foundation, please see
22   * <http://www.apache.org/>.
23   *
24   * [Additional notices, if required by prior licensing conditions]
25   *
26   */
27  package org.apache.commons.httpclient;
28  
29  import junit.framework.*;
30  import org.apache.commons.httpclient.methods.*;
31  import org.apache.commons.httpclient.params.HttpMethodParams;
32  
33  /***
34   * Tests cases intended to test if entity enclosing methods
35   * can deal with non-compliant HTTP servers or proxies
36   * 
37   * @author Oleg Kalnichevski
38   * @author Jeff Dever
39   */
40  
41  public class TestWebappNoncompliant extends TestWebappBase
42  {
43      public TestWebappNoncompliant(String s)
44      {
45          super(s);
46      }
47  
48      public static Test suite() {
49          TestSuite suite = new TestSuite(TestWebappNoncompliant.class);
50          return suite;
51      }
52  
53      public static void main(String args[]) {
54          String[] testCaseName = { TestWebappNoncompliant.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      /***
59       * Tests if client is able able to recover gracefully when 
60       * HTTP server or proxy fails to send 100 status code when
61       * expected. The client should resume sending the request body 
62       * after a defined timeout without having received "continue"
63       * code.
64       */
65      public void testNoncompliantPostMethodString()
66      {
67          HttpClient client = createHttpClient();
68          NoncompliantPostMethod method = new NoncompliantPostMethod("/" + getWebappContext() + "/body");
69          method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
70          method.setRequestEntity(new StringRequestEntity("This is data to be sent in the body of an HTTP POST."));
71          try {
72              client.executeMethod(method);
73          } catch (Exception e) {
74              e.printStackTrace();
75              fail("Unexpected exception: " + e.toString());
76          }
77          assertEquals(200,method.getStatusCode());
78      }
79  
80      /***
81       */
82      public void testNoncompliantStatusLine()
83      {
84          HttpClient client = createHttpClient();
85          GetMethod method = new GetMethod("/" + getWebappContext() + "/statusline");
86          method.setRequestHeader("Set-StatusCode", 444+"");
87          method.setRequestHeader("Set-StatusMessage", "This status message contains\n"
88                  + " a newline and a\r"
89                  + " carrage return but that should be OK.");
90          try {
91              client.executeMethod(method);
92          } catch (Exception e) {
93              e.printStackTrace();
94              fail("Unexpected exception: " + e.toString());
95          }
96          assertEquals(444, method.getStatusCode());
97      }
98  
99  
100     /*** 
101      * Test if a response to HEAD method from non-compliant server
102      * that contains an unexpected body content can be correctly redirected 
103      */ 
104 
105     public void testNoncompliantHeadWithResponseBody() 
106       throws Exception {
107           HttpClient client = createHttpClient();
108           HeadMethod method = new NoncompliantHeadMethod("/" + getWebappContext() + "/redirect");
109           method.getParams().setIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, 50);
110           client.executeMethod(method);
111           assertEquals(200,method.getStatusCode());
112           method.releaseConnection();
113     }
114 
115     /*** 
116      * Test if a response to HEAD method from non-compliant server
117      * causes an HttpException to be thrown 
118      */ 
119 
120     public void testNoncompliantHeadStrictMode() 
121       throws Exception {
122           HttpClient client = createHttpClient();
123           client.getParams().setBooleanParameter(HttpMethodParams.REJECT_HEAD_BODY, true);
124           HeadMethod method = new NoncompliantHeadMethod("/" + getWebappContext() + "/body");
125           method.getParams().setIntParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, 50);
126           try {
127               client.executeMethod(method);
128               fail("HttpException should have been thrown"); 
129           } catch(HttpException e) {
130               // Expected
131           }
132           method.releaseConnection();
133     }
134 
135 }