1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java $
3    * $Revision: 161963 $
4    * $Date: 2005-04-19 16:25:06 -0400 (Tue, 19 Apr 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  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  
30  package org.apache.commons.httpclient;
31  
32  import java.io.ByteArrayInputStream;
33  import java.io.IOException;
34  import java.io.InputStream;
35  
36  import junit.framework.Test;
37  import junit.framework.TestSuite;
38  
39  import org.apache.commons.httpclient.auth.AuthScope;
40  import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
41  import org.apache.commons.httpclient.methods.PostMethod;
42  import org.apache.commons.httpclient.methods.RequestEntity;
43  import org.apache.commons.httpclient.server.AuthRequestHandler;
44  import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
45  import org.apache.commons.httpclient.server.HttpServiceHandler;
46  
47  /***
48   * Tests specific to entity enclosing methods.
49   *
50   * @author Oleg Kalnichevski
51   * @version $Id: TestEntityEnclosingMethod.java 161963 2005-04-19 20:25:06Z olegk $
52   */
53  public class TestEntityEnclosingMethod extends HttpClientTestBase {
54  
55      public TestEntityEnclosingMethod(String testName) throws IOException {
56          super(testName);
57      }
58  
59      public static Test suite() {
60          TestSuite suite = new TestSuite(TestEntityEnclosingMethod.class);
61          return suite;
62      }
63  
64      public static void main(String args[]) {
65          String[] testCaseName = { TestEntityEnclosingMethod.class.getName() };
66          junit.textui.TestRunner.main(testCaseName);
67      }
68  
69      // ------------------------------------------------------------------ Tests
70      
71      public void testEnclosedEntityAutoLength() throws Exception {
72          String inputstr = "This is a test message";
73          byte[] input = inputstr.getBytes("US-ASCII");
74          InputStream instream = new ByteArrayInputStream(input);
75          
76          RequestEntity requestentity = new InputStreamRequestEntity(
77                  instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO); 
78          PostMethod method = new PostMethod("/");
79          method.setRequestEntity(requestentity);
80          this.server.setHttpService(new EchoService());
81          try {
82              this.client.executeMethod(method);
83              assertEquals(200, method.getStatusCode());
84              String body = method.getResponseBodyAsString();
85              assertEquals(inputstr, body);
86              assertNull(method.getRequestHeader("Transfer-Encoding"));
87              assertNotNull(method.getRequestHeader("Content-Length"));
88              assertEquals(input.length, Integer.parseInt(
89                      method.getRequestHeader("Content-Length").getValue()));
90          } finally {
91              method.releaseConnection();
92          }
93      }
94  
95      public void testEnclosedEntityExplicitLength() throws Exception {
96          String inputstr = "This is a test message";
97          byte[] input = inputstr.getBytes("US-ASCII");
98          InputStream instream = new ByteArrayInputStream(input);
99          
100         RequestEntity requestentity = new InputStreamRequestEntity(
101                 instream, 14); 
102         PostMethod method = new PostMethod("/");
103         method.setRequestEntity(requestentity);
104         this.server.setHttpService(new EchoService());
105         try {
106             this.client.executeMethod(method);
107             assertEquals(200, method.getStatusCode());
108             String body = method.getResponseBodyAsString();
109             assertEquals("This is a test", body);
110             assertNull(method.getRequestHeader("Transfer-Encoding"));
111             assertNotNull(method.getRequestHeader("Content-Length"));
112             assertEquals(14, Integer.parseInt(
113                     method.getRequestHeader("Content-Length").getValue()));
114         } finally {
115             method.releaseConnection();
116         }
117     }
118 
119     public void testEnclosedEntityChunked() throws Exception {
120         String inputstr = "This is a test message";
121         byte[] input = inputstr.getBytes("US-ASCII");
122         InputStream instream = new ByteArrayInputStream(input);
123         
124         RequestEntity requestentity = new InputStreamRequestEntity(
125                 instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO); 
126         PostMethod method = new PostMethod("/");
127         method.setRequestEntity(requestentity);
128         method.setContentChunked(true);
129         this.server.setHttpService(new EchoService());
130         try {
131             this.client.executeMethod(method);
132             assertEquals(200, method.getStatusCode());
133             String body = method.getResponseBodyAsString();
134             assertEquals(inputstr, body);
135             assertNotNull(method.getRequestHeader("Transfer-Encoding"));
136             assertNull(method.getRequestHeader("Content-Length"));
137         } finally {
138             method.releaseConnection();
139         }
140     }
141     
142     public void testEnclosedEntityChunkedHTTP1_0() throws Exception {
143         String inputstr = "This is a test message";
144         byte[] input = inputstr.getBytes("US-ASCII");
145         InputStream instream = new ByteArrayInputStream(input);
146         
147         RequestEntity requestentity = new InputStreamRequestEntity(
148                 instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO); 
149         PostMethod method = new PostMethod("/");
150         method.setRequestEntity(requestentity);
151         method.setContentChunked(true);
152         method.getParams().setVersion(HttpVersion.HTTP_1_0);
153         this.server.setHttpService(new EchoService());
154         try {
155             this.client.executeMethod(method);
156             fail("ProtocolException should have been thrown");
157         } catch (ProtocolException ex) {
158             // expected
159         } finally {
160             method.releaseConnection();
161         }
162     }
163 
164     public void testEnclosedEntityRepeatable() throws Exception {
165         String inputstr = "This is a test message";
166         byte[] input = inputstr.getBytes("US-ASCII");
167         InputStream instream = new ByteArrayInputStream(input);
168         
169         RequestEntity requestentity = new InputStreamRequestEntity(
170                 instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO); 
171         PostMethod method = new PostMethod("/");
172         method.setRequestEntity(requestentity);
173 
174         UsernamePasswordCredentials creds = 
175             new UsernamePasswordCredentials("testuser", "testpass");
176         
177         HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
178         handlerchain.appendHandler(new AuthRequestHandler(creds));
179         handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
180         this.server.setRequestHandler(handlerchain);
181         this.client.getState().setCredentials(AuthScope.ANY, creds);
182         try {
183             this.client.executeMethod(method);
184             assertEquals(200, method.getStatusCode());
185             String body = method.getResponseBodyAsString();
186             assertEquals(inputstr, body);
187             assertNull(method.getRequestHeader("Transfer-Encoding"));
188             assertNotNull(method.getRequestHeader("Content-Length"));
189             assertEquals(input.length, Integer.parseInt(
190                     method.getRequestHeader("Content-Length").getValue()));
191         } finally {
192             method.releaseConnection();
193         }
194     }
195 
196     public void testEnclosedEntityNonRepeatable() throws Exception {
197         String inputstr = "This is a test message";
198         byte[] input = inputstr.getBytes("US-ASCII");
199         InputStream instream = new ByteArrayInputStream(input);
200         
201         RequestEntity requestentity = new InputStreamRequestEntity(
202                 instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO); 
203         PostMethod method = new PostMethod("/");
204         method.setRequestEntity(requestentity);
205         method.setContentChunked(true);
206 
207         UsernamePasswordCredentials creds = 
208             new UsernamePasswordCredentials("testuser", "testpass");
209         
210         HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
211         handlerchain.appendHandler(new AuthRequestHandler(creds));
212         handlerchain.appendHandler(new HttpServiceHandler(new EchoService()));
213         this.server.setRequestHandler(handlerchain);
214         this.client.getState().setCredentials(AuthScope.ANY, creds);
215         try {
216             this.client.executeMethod(method);
217             fail("ProtocolException should have been thrown");
218         } catch (ProtocolException ex) {
219             // expected
220         } finally {
221             method.releaseConnection();
222         }
223     }
224     
225     public void testEnclosedEntityNegativeLength() throws Exception {
226         
227         String inputstr = "This is a test message";
228         byte[] input = inputstr.getBytes("US-ASCII");
229         InputStream instream = new ByteArrayInputStream(input);
230         
231         RequestEntity requestentity = new InputStreamRequestEntity(
232                 instream, -14); 
233         PostMethod method = new PostMethod("/");
234         method.setRequestEntity(requestentity);
235         method.setContentChunked(false);
236         this.server.setHttpService(new EchoService());
237         try {
238             this.client.executeMethod(method);
239             assertEquals(200, method.getStatusCode());
240             String body = method.getResponseBodyAsString();
241             assertEquals(inputstr, body);
242             assertNotNull(method.getRequestHeader("Transfer-Encoding"));
243             assertNull(method.getRequestHeader("Content-Length"));
244         } finally {
245             method.releaseConnection();
246         }
247     }
248 
249     public void testEnclosedEntityNegativeLengthHTTP1_0() throws Exception {
250         
251         String inputstr = "This is a test message";
252         byte[] input = inputstr.getBytes("US-ASCII");
253         InputStream instream = new ByteArrayInputStream(input);
254         
255         RequestEntity requestentity = new InputStreamRequestEntity(
256                 instream, -14); 
257         PostMethod method = new PostMethod("/");
258         method.setRequestEntity(requestentity);
259         method.setContentChunked(false);
260         method.getParams().setVersion(HttpVersion.HTTP_1_0);
261         this.server.setHttpService(new EchoService());
262         try {
263             this.client.executeMethod(method);
264             fail("ProtocolException should have been thrown");
265         } catch (ProtocolException ex) {
266             // expected
267         } finally {
268             method.releaseConnection();
269         }
270     }
271 }
272