Source Code Cross Referenced for TestEntityEnclosingMethod.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache common HttpClient » org.apache.commons.httpclient 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/oac.hc3x/tags/HTTPCLIENT_3_1/src/test/org/apache/commons/httpclient/TestEntityEnclosingMethod.java $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         *
006:         * ====================================================================
007:         *
008:         *  Licensed to the Apache Software Foundation (ASF) under one or more
009:         *  contributor license agreements.  See the NOTICE file distributed with
010:         *  this work for additional information regarding copyright ownership.
011:         *  The ASF licenses this file to You under the Apache License, Version 2.0
012:         *  (the "License"); you may not use this file except in compliance with
013:         *  the License.  You may obtain a copy of the License at
014:         *
015:         *      http://www.apache.org/licenses/LICENSE-2.0
016:         *
017:         *  Unless required by applicable law or agreed to in writing, software
018:         *  distributed under the License is distributed on an "AS IS" BASIS,
019:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020:         *  See the License for the specific language governing permissions and
021:         *  limitations under the License.
022:         * ====================================================================
023:         *
024:         * This software consists of voluntary contributions made by many
025:         * individuals on behalf of the Apache Software Foundation.  For more
026:         * information on the Apache Software Foundation, please see
027:         * <http://www.apache.org/>.
028:         *
029:         */
030:
031:        package org.apache.commons.httpclient;
032:
033:        import java.io.ByteArrayInputStream;
034:        import java.io.IOException;
035:        import java.io.InputStream;
036:
037:        import junit.framework.Test;
038:        import junit.framework.TestSuite;
039:
040:        import org.apache.commons.httpclient.auth.AuthScope;
041:        import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
042:        import org.apache.commons.httpclient.methods.PostMethod;
043:        import org.apache.commons.httpclient.methods.RequestEntity;
044:        import org.apache.commons.httpclient.methods.StringRequestEntity;
045:        import org.apache.commons.httpclient.server.AuthRequestHandler;
046:        import org.apache.commons.httpclient.server.HttpRequestHandlerChain;
047:        import org.apache.commons.httpclient.server.HttpService;
048:        import org.apache.commons.httpclient.server.HttpServiceHandler;
049:        import org.apache.commons.httpclient.server.SimpleRequest;
050:        import org.apache.commons.httpclient.server.SimpleResponse;
051:
052:        /**
053:         * Tests specific to entity enclosing methods.
054:         *
055:         * @author Oleg Kalnichevski
056:         * @version $Id: TestEntityEnclosingMethod.java 480424 2006-11-29 05:56:49Z bayard $
057:         */
058:        public class TestEntityEnclosingMethod extends HttpClientTestBase {
059:
060:            public TestEntityEnclosingMethod(String testName)
061:                    throws IOException {
062:                super (testName);
063:            }
064:
065:            public static Test suite() {
066:                TestSuite suite = new TestSuite(TestEntityEnclosingMethod.class);
067:                return suite;
068:            }
069:
070:            public static void main(String args[]) {
071:                String[] testCaseName = { TestEntityEnclosingMethod.class
072:                        .getName() };
073:                junit.textui.TestRunner.main(testCaseName);
074:            }
075:
076:            // ------------------------------------------------------------------ Tests
077:
078:            public void testEnclosedEntityAutoLength() throws Exception {
079:                String inputstr = "This is a test message";
080:                byte[] input = inputstr.getBytes("US-ASCII");
081:                InputStream instream = new ByteArrayInputStream(input);
082:
083:                RequestEntity requestentity = new InputStreamRequestEntity(
084:                        instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
085:                PostMethod method = new PostMethod("/");
086:                method.setRequestEntity(requestentity);
087:                this .server.setHttpService(new EchoService());
088:                try {
089:                    this .client.executeMethod(method);
090:                    assertEquals(200, method.getStatusCode());
091:                    String body = method.getResponseBodyAsString();
092:                    assertEquals(inputstr, body);
093:                    assertNull(method.getRequestHeader("Transfer-Encoding"));
094:                    assertNotNull(method.getRequestHeader("Content-Length"));
095:                    assertEquals(input.length, Integer.parseInt(method
096:                            .getRequestHeader("Content-Length").getValue()));
097:                } finally {
098:                    method.releaseConnection();
099:                }
100:            }
101:
102:            public void testEnclosedEntityExplicitLength() throws Exception {
103:                String inputstr = "This is a test message";
104:                byte[] input = inputstr.getBytes("US-ASCII");
105:                InputStream instream = new ByteArrayInputStream(input);
106:
107:                RequestEntity requestentity = new InputStreamRequestEntity(
108:                        instream, 14);
109:                PostMethod method = new PostMethod("/");
110:                method.setRequestEntity(requestentity);
111:                this .server.setHttpService(new EchoService());
112:                try {
113:                    this .client.executeMethod(method);
114:                    assertEquals(200, method.getStatusCode());
115:                    String body = method.getResponseBodyAsString();
116:                    assertEquals("This is a test", body);
117:                    assertNull(method.getRequestHeader("Transfer-Encoding"));
118:                    assertNotNull(method.getRequestHeader("Content-Length"));
119:                    assertEquals(14, Integer.parseInt(method.getRequestHeader(
120:                            "Content-Length").getValue()));
121:                } finally {
122:                    method.releaseConnection();
123:                }
124:            }
125:
126:            public void testEnclosedEntityChunked() throws Exception {
127:                String inputstr = "This is a test message";
128:                byte[] input = inputstr.getBytes("US-ASCII");
129:                InputStream instream = new ByteArrayInputStream(input);
130:
131:                RequestEntity requestentity = new InputStreamRequestEntity(
132:                        instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
133:                PostMethod method = new PostMethod("/");
134:                method.setRequestEntity(requestentity);
135:                method.setContentChunked(true);
136:                this .server.setHttpService(new EchoService());
137:                try {
138:                    this .client.executeMethod(method);
139:                    assertEquals(200, method.getStatusCode());
140:                    String body = method.getResponseBodyAsString();
141:                    assertEquals(inputstr, body);
142:                    assertNotNull(method.getRequestHeader("Transfer-Encoding"));
143:                    assertNull(method.getRequestHeader("Content-Length"));
144:                } finally {
145:                    method.releaseConnection();
146:                }
147:            }
148:
149:            public void testEnclosedEntityChunkedHTTP1_0() throws Exception {
150:                String inputstr = "This is a test message";
151:                byte[] input = inputstr.getBytes("US-ASCII");
152:                InputStream instream = new ByteArrayInputStream(input);
153:
154:                RequestEntity requestentity = new InputStreamRequestEntity(
155:                        instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
156:                PostMethod method = new PostMethod("/");
157:                method.setRequestEntity(requestentity);
158:                method.setContentChunked(true);
159:                method.getParams().setVersion(HttpVersion.HTTP_1_0);
160:                this .server.setHttpService(new EchoService());
161:                try {
162:                    this .client.executeMethod(method);
163:                    fail("ProtocolException should have been thrown");
164:                } catch (ProtocolException ex) {
165:                    // expected
166:                } finally {
167:                    method.releaseConnection();
168:                }
169:            }
170:
171:            public void testEnclosedEntityRepeatable() throws Exception {
172:                String inputstr = "This is a test message";
173:                byte[] input = inputstr.getBytes("US-ASCII");
174:                InputStream instream = new ByteArrayInputStream(input);
175:
176:                RequestEntity requestentity = new InputStreamRequestEntity(
177:                        instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
178:                PostMethod method = new PostMethod("/");
179:                method.setRequestEntity(requestentity);
180:
181:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
182:                        "testuser", "testpass");
183:
184:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
185:                handlerchain.appendHandler(new AuthRequestHandler(creds));
186:                handlerchain.appendHandler(new HttpServiceHandler(
187:                        new EchoService()));
188:                this .server.setRequestHandler(handlerchain);
189:                this .client.getState().setCredentials(AuthScope.ANY, creds);
190:                try {
191:                    this .client.executeMethod(method);
192:                    assertEquals(200, method.getStatusCode());
193:                    String body = method.getResponseBodyAsString();
194:                    assertEquals(inputstr, body);
195:                    assertNull(method.getRequestHeader("Transfer-Encoding"));
196:                    assertNotNull(method.getRequestHeader("Content-Length"));
197:                    assertEquals(input.length, Integer.parseInt(method
198:                            .getRequestHeader("Content-Length").getValue()));
199:                } finally {
200:                    method.releaseConnection();
201:                }
202:            }
203:
204:            public void testEnclosedEntityNonRepeatable() throws Exception {
205:                String inputstr = "This is a test message";
206:                byte[] input = inputstr.getBytes("US-ASCII");
207:                InputStream instream = new ByteArrayInputStream(input);
208:
209:                RequestEntity requestentity = new InputStreamRequestEntity(
210:                        instream, InputStreamRequestEntity.CONTENT_LENGTH_AUTO);
211:                PostMethod method = new PostMethod("/");
212:                method.setRequestEntity(requestentity);
213:                method.setContentChunked(true);
214:
215:                UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
216:                        "testuser", "testpass");
217:
218:                HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
219:                handlerchain.appendHandler(new AuthRequestHandler(creds));
220:                handlerchain.appendHandler(new HttpServiceHandler(
221:                        new EchoService()));
222:                this .server.setRequestHandler(handlerchain);
223:                this .client.getState().setCredentials(AuthScope.ANY, creds);
224:                try {
225:                    this .client.executeMethod(method);
226:                    fail("ProtocolException should have been thrown");
227:                } catch (ProtocolException ex) {
228:                    // expected
229:                } finally {
230:                    method.releaseConnection();
231:                }
232:            }
233:
234:            public void testEnclosedEntityNegativeLength() throws Exception {
235:
236:                String inputstr = "This is a test message";
237:                byte[] input = inputstr.getBytes("US-ASCII");
238:                InputStream instream = new ByteArrayInputStream(input);
239:
240:                RequestEntity requestentity = new InputStreamRequestEntity(
241:                        instream, -14);
242:                PostMethod method = new PostMethod("/");
243:                method.setRequestEntity(requestentity);
244:                method.setContentChunked(false);
245:                this .server.setHttpService(new EchoService());
246:                try {
247:                    this .client.executeMethod(method);
248:                    assertEquals(200, method.getStatusCode());
249:                    String body = method.getResponseBodyAsString();
250:                    assertEquals(inputstr, body);
251:                    assertNotNull(method.getRequestHeader("Transfer-Encoding"));
252:                    assertNull(method.getRequestHeader("Content-Length"));
253:                } finally {
254:                    method.releaseConnection();
255:                }
256:            }
257:
258:            public void testEnclosedEntityNegativeLengthHTTP1_0()
259:                    throws Exception {
260:
261:                String inputstr = "This is a test message";
262:                byte[] input = inputstr.getBytes("US-ASCII");
263:                InputStream instream = new ByteArrayInputStream(input);
264:
265:                RequestEntity requestentity = new InputStreamRequestEntity(
266:                        instream, -14);
267:                PostMethod method = new PostMethod("/");
268:                method.setRequestEntity(requestentity);
269:                method.setContentChunked(false);
270:                method.getParams().setVersion(HttpVersion.HTTP_1_0);
271:                this .server.setHttpService(new EchoService());
272:                try {
273:                    this .client.executeMethod(method);
274:                    fail("ProtocolException should have been thrown");
275:                } catch (ProtocolException ex) {
276:                    // expected
277:                } finally {
278:                    method.releaseConnection();
279:                }
280:            }
281:
282:            class RequestBodyStatsService implements  HttpService {
283:
284:                public RequestBodyStatsService() {
285:                    super ();
286:                }
287:
288:                public boolean process(final SimpleRequest request,
289:                        final SimpleResponse response) throws IOException {
290:                    HttpVersion httpversion = request.getRequestLine()
291:                            .getHttpVersion();
292:                    response.setStatusLine(httpversion, HttpStatus.SC_OK);
293:                    response
294:                            .addHeader(new Header("Content-Type", "text/plain"));
295:
296:                    StringBuffer buffer = new StringBuffer();
297:                    buffer.append("Request bosy stats:\r\n");
298:                    buffer.append("===================\r\n");
299:                    long l = request.getContentLength();
300:                    if (l >= 0) {
301:                        buffer.append("Content-Length: ");
302:                        buffer.append(l);
303:                        buffer.append("\r\n");
304:                    }
305:                    Header te = request.getFirstHeader("Transfer-Encoding");
306:                    if (te != null) {
307:                        buffer.append("Content-Length: ");
308:                        buffer.append(te.getValue());
309:                        buffer.append("\r\n");
310:                    }
311:                    byte[] b = request.getBodyBytes();
312:                    if (b.length <= 0) {
313:                        buffer.append("No body submitted\r\n");
314:                    }
315:                    response.setBodyString(buffer.toString());
316:                    return true;
317:                }
318:            }
319:
320:            public void testEmptyPostMethod() throws Exception {
321:                this .server.setHttpService(new RequestBodyStatsService());
322:
323:                PostMethod method = new PostMethod("/");
324:                method.setRequestHeader("Content-Type", "text/plain");
325:                this .client.executeMethod(method);
326:                assertEquals(200, method.getStatusLine().getStatusCode());
327:                String response = method.getResponseBodyAsString();
328:                assertNotNull(method.getRequestHeader("Content-Length"));
329:                assertTrue(response.indexOf("No body submitted") >= 0);
330:
331:                method = new PostMethod("/");
332:                method.setRequestHeader("Content-Type", "text/plain");
333:                method
334:                        .setRequestEntity(new StringRequestEntity("", null,
335:                                null));
336:                this .client.executeMethod(method);
337:                assertEquals(200, method.getStatusLine().getStatusCode());
338:                assertNotNull(method.getRequestHeader("Content-Length"));
339:                response = method.getResponseBodyAsString();
340:                assertTrue(response.indexOf("No body submitted") >= 0);
341:
342:                method = new PostMethod("/");
343:                method.setRequestHeader("Content-Type", "text/plain");
344:                method.setContentChunked(true);
345:                this .client.executeMethod(method);
346:                assertEquals(200, method.getStatusLine().getStatusCode());
347:                assertNotNull(method.getRequestHeader("Content-Length"));
348:                response = method.getResponseBodyAsString();
349:                assertTrue(response.indexOf("No body submitted") >= 0);
350:
351:                method = new PostMethod("/");
352:                method.setRequestHeader("Content-Type", "text/plain");
353:                method
354:                        .setRequestEntity(new StringRequestEntity("", null,
355:                                null));
356:                method.setContentChunked(true);
357:                this .client.executeMethod(method);
358:                assertNull(method.getRequestHeader("Content-Length"));
359:                assertNotNull(method.getRequestHeader("Transfer-Encoding"));
360:                assertEquals(200, method.getStatusLine().getStatusCode());
361:                response = method.getResponseBodyAsString();
362:                assertTrue(response.indexOf("No body submitted") >= 0);
363:            }
364:
365:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.