Source Code Cross Referenced for TestResponseHeaders.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:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestResponseHeaders.java,v 1.18 2004/11/07 12:31:42 olegk Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         * ====================================================================
006:         *
007:         *  Licensed to the Apache Software Foundation (ASF) under one or more
008:         *  contributor license agreements.  See the NOTICE file distributed with
009:         *  this work for additional information regarding copyright ownership.
010:         *  The ASF licenses this file to You under the Apache License, Version 2.0
011:         *  (the "License"); you may not use this file except in compliance with
012:         *  the License.  You may obtain a copy of the License at
013:         *
014:         *      http://www.apache.org/licenses/LICENSE-2.0
015:         *
016:         *  Unless required by applicable law or agreed to in writing, software
017:         *  distributed under the License is distributed on an "AS IS" BASIS,
018:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019:         *  See the License for the specific language governing permissions and
020:         *  limitations under the License.
021:         * ====================================================================
022:         *
023:         * This software consists of voluntary contributions made by many
024:         * individuals on behalf of the Apache Software Foundation.  For more
025:         * information on the Apache Software Foundation, please see
026:         * <http://www.apache.org/>.
027:         *
028:         * [Additional notices, if required by prior licensing conditions]
029:         *
030:         */
031:
032:        package org.apache.commons.httpclient;
033:
034:        import java.io.IOException;
035:
036:        import junit.framework.Test;
037:        import junit.framework.TestSuite;
038:
039:        import org.apache.commons.httpclient.methods.GetMethod;
040:        import org.apache.commons.httpclient.server.HttpRequestHandler;
041:        import org.apache.commons.httpclient.server.HttpService;
042:        import org.apache.commons.httpclient.server.ResponseWriter;
043:        import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
044:        import org.apache.commons.httpclient.server.SimpleRequest;
045:        import org.apache.commons.httpclient.server.SimpleResponse;
046:
047:        /**
048:         * Tests for reading response headers.
049:         *
050:         * @author <a href="mailto:dims@apache.org">Davanum Srinivas</a>
051:         * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
052:         * @author <a href="mailto:adrian@intencha.com">Adrian Sutton</a>
053:         * @version $Id: TestResponseHeaders.java 480424 2006-11-29 05:56:49Z bayard $
054:         */
055:        public class TestResponseHeaders extends HttpClientTestBase {
056:
057:            private AccessibleHttpConnectionManager connectionManager;
058:
059:            // ------------------------------------------------------------ Constructor
060:            public TestResponseHeaders(final String testName)
061:                    throws IOException {
062:                super (testName);
063:            }
064:
065:            public void setUp() throws IOException {
066:                super .setUp();
067:                this .connectionManager = new AccessibleHttpConnectionManager();
068:                this .client.setHttpConnectionManager(connectionManager);
069:            }
070:
071:            // ------------------------------------------------------------------- Main
072:            public static void main(String args[]) {
073:                String[] testCaseName = { TestResponseHeaders.class.getName() };
074:                junit.textui.TestRunner.main(testCaseName);
075:            }
076:
077:            // ------------------------------------------------------- TestCase Methods
078:            public static Test suite() {
079:                return new TestSuite(TestResponseHeaders.class);
080:            }
081:
082:            // ----------------------------------------------------------- Test Methods
083:            public void testHeaders() throws Exception {
084:                final String body = "XXX\r\nYYY\r\nZZZ";
085:                this .server.setHttpService(new HttpService() {
086:                    public boolean process(SimpleRequest request,
087:                            SimpleResponse response) throws IOException {
088:                        response.setStatusLine(request.getRequestLine()
089:                                .getHttpVersion(), 200);
090:                        response.addHeader(new Header("Connection", "close"));
091:                        response.addHeader(new Header("Content-Length", Integer
092:                                .toString(body.length())));
093:                        response.addHeader(new Header("Content-Type",
094:                                "text/xml; charset=utf-8"));
095:                        response.addHeader(new Header("Date",
096:                                "Wed, 28 Mar 2001 05:05:04 GMT"));
097:                        response.addHeader(new Header("Server",
098:                                "UserLand Frontier/7.0-WinNT"));
099:                        response.setBodyString(body);
100:                        return true;
101:                    }
102:                });
103:
104:                HttpMethod method = new GetMethod();
105:                client.executeMethod(method);
106:                assertEquals("close", method.getResponseHeader("Connection")
107:                        .getValue());
108:                assertEquals(body.length(), Integer.parseInt(method
109:                        .getResponseHeader("Content-Length").getValue()));
110:                assertEquals("text/xml; charset=utf-8", method
111:                        .getResponseHeader("Content-Type").getValue());
112:                assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method
113:                        .getResponseHeader("Date").getValue());
114:                assertEquals("UserLand Frontier/7.0-WinNT", method
115:                        .getResponseHeader("Server").getValue());
116:            }
117:
118:            /**
119:             * Tests that having a duplicate content length causes no problems.
120:             */
121:            public void testDuplicateContentLength() throws Exception {
122:
123:                final String body = "XXX\r\nYYY\r\nZZZ";
124:                this .server.setHttpService(new HttpService() {
125:                    public boolean process(SimpleRequest request,
126:                            SimpleResponse response) throws IOException {
127:                        response.setStatusLine(request.getRequestLine()
128:                                .getHttpVersion(), 200);
129:                        response.addHeader(new Header("Content-Length", Integer
130:                                .toString(body.length())));
131:                        response.addHeader(new Header("Content-Length", Integer
132:                                .toString(body.length())));
133:                        response.setBodyString(body);
134:                        return true;
135:                    }
136:                });
137:                HttpMethod method = new GetMethod();
138:                client.executeMethod(method);
139:                assertNotNull("Response body is null.", method
140:                        .getResponseBodyAsStream());
141:            }
142:
143:            public void testDuplicateConnection() throws Exception {
144:
145:                this .server.setHttpService(new HttpService() {
146:                    public boolean process(SimpleRequest request,
147:                            SimpleResponse response) throws IOException {
148:                        response.setStatusLine(request.getRequestLine()
149:                                .getHttpVersion(), 200);
150:                        response.addHeader(new Header("Connection", "close"));
151:                        response.addHeader(new Header("Connection", "close"));
152:                        return true;
153:                    }
154:                });
155:
156:                GetMethod method = new GetMethod("/");
157:                client.executeMethod(method);
158:                method.getResponseBodyAsString();
159:
160:                assertFalse(connectionManager.getConection().isOpen());
161:
162:                this .server.setHttpService(new HttpService() {
163:                    public boolean process(SimpleRequest request,
164:                            SimpleResponse response) throws IOException {
165:                        response.setStatusLine(HttpVersion.HTTP_1_0, 200);
166:                        response.addHeader(new Header("Connection",
167:                                "keep-alive"));
168:                        response.addHeader(new Header("Connection",
169:                                "keep-alive"));
170:                        response.setBodyString("aa");
171:                        return true;
172:                    }
173:                });
174:
175:                method = new GetMethod("/");
176:                client.executeMethod(method);
177:                method.getResponseBodyAsString();
178:
179:                assertTrue(connectionManager.getConection().isOpen());
180:            }
181:
182:            public void testNoContentLength() throws Exception {
183:                // test with connection header
184:                this .server.setRequestHandler(new HttpRequestHandler() {
185:                    public boolean processRequest(
186:                            SimpleHttpServerConnection conn,
187:                            SimpleRequest request) throws IOException {
188:                        ResponseWriter out = conn.getWriter();
189:                        out.println("HTTP/1.1 200 OK");
190:                        out.println("Connection: keep-alive");
191:                        out.println();
192:                        out.println("12345");
193:                        out.flush();
194:                        return true;
195:                    }
196:                });
197:
198:                GetMethod method = new GetMethod("/");
199:                client.executeMethod(method);
200:                method.getResponseBodyAsString();
201:
202:                assertFalse(connectionManager.getConection().isOpen());
203:
204:                // test without connection header
205:                this .server.setRequestHandler(new HttpRequestHandler() {
206:                    public boolean processRequest(
207:                            SimpleHttpServerConnection conn,
208:                            SimpleRequest request) throws IOException {
209:                        ResponseWriter out = conn.getWriter();
210:                        out.println("HTTP/1.1 200 OK");
211:                        out.println();
212:                        out.println("12345");
213:                        out.flush();
214:                        return true;
215:                    }
216:                });
217:
218:                // test with connection header
219:                method = new GetMethod("/");
220:                client.executeMethod(method);
221:                method.getResponseBodyAsString();
222:
223:                assertFalse(connectionManager.getConection().isOpen());
224:            }
225:
226:            public void testInvalidContentLength1() throws Exception {
227:                this .server.setHttpService(new HttpService() {
228:                    public boolean process(SimpleRequest request,
229:                            SimpleResponse response) throws IOException {
230:                        response.setStatusLine(request.getRequestLine()
231:                                .getHttpVersion(), 200);
232:                        response.addHeader(new Header("Content-Length", "5"));
233:                        response
234:                                .addHeader(new Header("Content-Length", "stuff"));
235:                        response.setBodyString("12345");
236:                        return true;
237:                    }
238:                });
239:                GetMethod method = new GetMethod("/");
240:                client.executeMethod(method);
241:                assertEquals(5, method.getResponseContentLength());
242:            }
243:
244:            public void testInvalidContentLength2() throws Exception {
245:                this .server.setHttpService(new HttpService() {
246:                    public boolean process(SimpleRequest request,
247:                            SimpleResponse response) throws IOException {
248:                        response.setStatusLine(request.getRequestLine()
249:                                .getHttpVersion(), 200);
250:                        response
251:                                .addHeader(new Header("Content-Length", "stuff"));
252:                        response.addHeader(new Header("Content-Length", "5"));
253:                        response.setBodyString("12345");
254:                        return true;
255:                    }
256:                });
257:                GetMethod method = new GetMethod("/");
258:                client.executeMethod(method);
259:                assertEquals(5, method.getResponseContentLength());
260:            }
261:
262:            public void testProxyNoContentLength() throws Exception {
263:                // test with proxy-connection header
264:                this .server.setRequestHandler(new HttpRequestHandler() {
265:                    public boolean processRequest(
266:                            SimpleHttpServerConnection conn,
267:                            SimpleRequest request) throws IOException {
268:                        ResponseWriter out = conn.getWriter();
269:                        out.println("HTTP/1.1 200 OK");
270:                        out.println("proxy-connection: keep-alive");
271:                        out.println();
272:                        out.println("12345");
273:                        out.flush();
274:                        return true;
275:                    }
276:                });
277:
278:                client.getHostConfiguration().setProxy(
279:                        server.getLocalAddress(), server.getLocalPort());
280:                GetMethod method = new GetMethod("/");
281:                client.executeMethod(method);
282:                method.getResponseBodyAsString();
283:
284:                assertFalse(connectionManager.getConection().isOpen());
285:
286:                // test without proxy-connection header
287:                this .server.setRequestHandler(new HttpRequestHandler() {
288:                    public boolean processRequest(
289:                            SimpleHttpServerConnection conn,
290:                            SimpleRequest request) throws IOException {
291:                        ResponseWriter out = conn.getWriter();
292:                        out.println("HTTP/1.1 200 OK");
293:                        out.println();
294:                        out.println("12345");
295:                        out.flush();
296:                        return true;
297:                    }
298:                });
299:
300:                method = new GetMethod("/");
301:                client.executeMethod(method);
302:                method.getResponseBodyAsString();
303:
304:                assertFalse(connectionManager.getConection().isOpen());
305:            }
306:
307:            public void testNullHeaders() throws Exception {
308:                this .server.setHttpService(new HttpService() {
309:                    public boolean process(SimpleRequest request,
310:                            SimpleResponse response) throws IOException {
311:                        response.setStatusLine(request.getRequestLine()
312:                                .getHttpVersion(), 200);
313:                        response.addHeader(new Header("Connection", "close"));
314:                        response.setBodyString("XXX\r\nYYY\r\nZZZ");
315:                        return true;
316:                    }
317:                });
318:                HttpMethod method = new GetMethod("/");
319:                client.executeMethod(method);
320:                assertEquals(null, method.getResponseHeader(null));
321:                assertEquals(null, method.getResponseHeader("bogus"));
322:            }
323:
324:            public void testFoldedHeaders() throws Exception {
325:                final String body = "XXX\r\nYYY\r\nZZZ";
326:                this .server.setRequestHandler(new HttpRequestHandler() {
327:                    public boolean processRequest(
328:                            SimpleHttpServerConnection conn,
329:                            SimpleRequest request) throws IOException {
330:                        ResponseWriter out = conn.getWriter();
331:                        out.println("HTTP/1.1 200 OK");
332:                        out.println("Connection: close");
333:                        out.println("Content-Length: " + body.length());
334:                        out.println("Content-Type: text/xml; charset=utf-8");
335:                        out.println("\tboundary=XXXX");
336:                        out.println("Date: Wed, 28 Mar 2001");
337:                        out.println(" 05:05:04 GMT");
338:                        out.println("Server: UserLand Frontier/7.0-WinNT");
339:                        out.println();
340:                        out.println(body);
341:                        out.flush();
342:                        return true;
343:                    }
344:                });
345:                HttpMethod method = new GetMethod("/");
346:                client.executeMethod(method);
347:                assertEquals("close", method.getResponseHeader("Connection")
348:                        .getValue());
349:                assertEquals(body.length(), Integer.parseInt(method
350:                        .getResponseHeader("Content-Length").getValue()));
351:                assertEquals("text/xml; charset=utf-8 boundary=XXXX", method
352:                        .getResponseHeader("Content-Type").getValue());
353:                assertEquals("Wed, 28 Mar 2001 05:05:04 GMT", method
354:                        .getResponseHeader("Date").getValue());
355:                assertEquals("UserLand Frontier/7.0-WinNT", method
356:                        .getResponseHeader("Server").getValue());
357:                assertTrue(method.getResponseHeader("Content-Type").toString()
358:                        .indexOf("boundary") != -1);
359:            }
360:
361:            public void testForceCloseConnection() throws Exception {
362:                this .server.setRequestHandler(new HttpRequestHandler() {
363:                    public boolean processRequest(
364:                            SimpleHttpServerConnection conn,
365:                            SimpleRequest request) throws IOException {
366:                        ResponseWriter out = conn.getWriter();
367:                        out.println("HTTP/1.1 200 OK");
368:                        out.println("Content-Type: garbage");
369:                        out.println();
370:                        out.println("stuff");
371:                        out.flush();
372:                        return true;
373:                    }
374:                });
375:                FakeHttpMethod method = new FakeHttpMethod();
376:                client.executeMethod(method);
377:                assertTrue("Connection should be closed",
378:                        method.shouldCloseConnection(connectionManager
379:                                .getConection()));
380:                assertTrue("Connection should be force-closed", method
381:                        .isConnectionCloseForced());
382:            }
383:
384:            public void testForceCloseConnection2() throws Exception {
385:                this .server.setRequestHandler(new HttpRequestHandler() {
386:                    public boolean processRequest(
387:                            SimpleHttpServerConnection conn,
388:                            SimpleRequest request) throws IOException {
389:                        ResponseWriter out = conn.getWriter();
390:                        out.println("HTTP/1.1 200 OK");
391:                        out.println("Content-Type: garbage");
392:                        out.println("Connection: close");
393:                        out.println();
394:                        out.println("stuff");
395:                        out.flush();
396:                        return true;
397:                    }
398:                });
399:                FakeHttpMethod method = new FakeHttpMethod();
400:                client.executeMethod(method);
401:                assertTrue("Connection should be closed",
402:                        method.shouldCloseConnection(connectionManager
403:                                .getConection()));
404:                assertFalse("Connection should NOT be closed", method
405:                        .isConnectionCloseForced());
406:            }
407:
408:            public void testNoContent() throws Exception {
409:                // test with connection header
410:                this .server.setRequestHandler(new HttpRequestHandler() {
411:                    public boolean processRequest(
412:                            SimpleHttpServerConnection conn,
413:                            SimpleRequest request) throws IOException {
414:                        ResponseWriter out = conn.getWriter();
415:                        out.println("HTTP/1.1 204 NO CONTENT");
416:                        out.println();
417:                        out.flush();
418:                        return true;
419:                    }
420:                });
421:
422:                GetMethod method = new GetMethod("/");
423:                client.executeMethod(method);
424:                method.getResponseBodyAsString();
425:
426:                assertTrue(connectionManager.getConection().isOpen());
427:
428:                // test without connection header
429:                this .server.setRequestHandler(new HttpRequestHandler() {
430:                    public boolean processRequest(
431:                            SimpleHttpServerConnection conn,
432:                            SimpleRequest request) throws IOException {
433:                        ResponseWriter out = conn.getWriter();
434:                        out.println("HTTP/1.1 204 NO CONTENT");
435:                        out.println("Connection: keep-alive");
436:                        out.println();
437:                        out.flush();
438:                        return true;
439:                    }
440:                });
441:
442:                // test with connection header
443:                method = new GetMethod("/");
444:                client.executeMethod(method);
445:                method.getResponseBodyAsString();
446:
447:                assertTrue(connectionManager.getConection().isOpen());
448:            }
449:
450:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.