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  
32  /***
33   * Tests cases intended to test if entity enclosing methods
34   * can deal with non-compliant HTTP servers or proxies
35   * 
36   * @author Oleg Kalnichevski
37   * @author Jeff Dever
38   */
39  
40  public class TestWebappNoncompliant extends TestWebappBase
41  {
42      public TestWebappNoncompliant(String s)
43      {
44          super(s);
45      }
46  
47      public static Test suite() {
48          TestSuite suite = new TestSuite(TestWebappNoncompliant.class);
49          return suite;
50      }
51  
52      public static void main(String args[]) {
53          String[] testCaseName = { TestWebappNoncompliant.class.getName() };
54          junit.textui.TestRunner.main(testCaseName);
55      }
56  
57      /***
58       * Tests if client is able able to recover gracefully when 
59       * HTTP server or proxy fails to send 100 status code when
60       * expected. The client should resume sending the request body 
61       * after a defined timeout without having received "continue"
62       * code.
63       */
64      public void testNoncompliantPostMethodString()
65      {
66          HttpClient client = createHttpClient();
67          NoncompliantPostMethod method = new NoncompliantPostMethod("/" + getWebappContext() + "/body");
68          method.setUseExpectHeader(true);
69          method.setRequestBody("This is data to be sent in the body of an HTTP POST.");
70          try {
71              client.executeMethod(method);
72          } catch (Exception e) {
73              e.printStackTrace();
74              fail("Unexpected exception: " + e.toString());
75          }
76          assertEquals(200,method.getStatusCode());
77      }
78  
79      /***
80       */
81      public void testNoncompliantStatusLine()
82      {
83          HttpClient client = createHttpClient();
84          GetMethod method = new GetMethod("/" + getWebappContext() + "/statusline");
85          method.setRequestHeader("Set-StatusCode", 444+"");
86          method.setRequestHeader("Set-StatusMessage", "This status message contains\n"
87                  + " a newline and a\r"
88                  + " carrage return but that should be OK.");
89          try {
90              client.executeMethod(method);
91          } catch (Exception e) {
92              e.printStackTrace();
93              fail("Unexpected exception: " + e.toString());
94          }
95          assertEquals(444, method.getStatusCode());
96      }
97  
98  
99      /*** 
100      * Test if a response to HEAD method from non-compliant server
101      * that contains an unexpected body content can be correctly redirected 
102      */ 
103 
104     public void testNoncompliantHeadWithResponseBody() 
105       throws Exception {
106           HttpClient client = createHttpClient();
107           HeadMethod method = new NoncompliantHeadMethod("/" + getWebappContext() + "/redirect");
108           method.setBodyCheckTimeout(50);
109           client.executeMethod(method);
110           assertEquals(200,method.getStatusCode());
111           method.releaseConnection();
112     }
113 
114     /*** 
115      * Test if a response to HEAD method from non-compliant server
116      * causes an HttpException to be thrown 
117      */ 
118 
119     public void testNoncompliantHeadStrictMode() 
120       throws Exception {
121           HttpClient client = createHttpClient();
122           client.setStrictMode(true);
123           HeadMethod method = new NoncompliantHeadMethod("/" + getWebappContext() + "/body");
124           method.setBodyCheckTimeout(50);
125           try {
126               client.executeMethod(method);
127               fail("HttpException should have been thrown"); 
128           } catch(HttpException e) {
129               // Expected
130           }
131           method.releaseConnection();
132     }
133 
134 }