Source Code Cross Referenced for HttpParserTest.java in  » Sevlet-Container » jetty-modules » org » mortbay » jetty » 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 » Sevlet Container » jetty modules » org.mortbay.jetty 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // ========================================================================
002:        // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003:        // ------------------------------------------------------------------------
004:        // Licensed under the Apache License, Version 2.0 (the "License");
005:        // you may not use this file except in compliance with the License.
006:        // You may obtain a copy of the License at 
007:        // http://www.apache.org/licenses/LICENSE-2.0
008:        // Unless required by applicable law or agreed to in writing, software
009:        // distributed under the License is distributed on an "AS IS" BASIS,
010:        // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011:        // See the License for the specific language governing permissions and
012:        // limitations under the License.
013:        // ========================================================================
014:
015:        package org.mortbay.jetty;
016:
017:        import java.io.UnsupportedEncodingException;
018:
019:        import junit.framework.TestCase;
020:
021:        import org.mortbay.io.Buffer;
022:        import org.mortbay.io.ByteArrayBuffer;
023:        import org.mortbay.io.SimpleBuffers;
024:        import org.mortbay.io.bio.StringEndPoint;
025:        import org.mortbay.util.StringUtil;
026:
027:        /**
028:         * @author gregw
029:         *
030:         * To change this generated comment edit the template variable "typecomment":
031:         * Window>Preferences>Java>Templates.
032:         * To enable and disable the creation of type comments go to
033:         * Window>Preferences>Java>Code Generation.
034:         */
035:        public class HttpParserTest extends TestCase {
036:            /**
037:             * Constructor for HttpParserTest.
038:             * @param arg0
039:             */
040:            public HttpParserTest(String arg0) {
041:                super (arg0);
042:            }
043:
044:            public static void main(String[] args) {
045:                junit.textui.TestRunner.run(HttpParserTest.class);
046:            }
047:
048:            /**
049:             * @see TestCase#setUp()
050:             */
051:            protected void setUp() throws Exception {
052:                super .setUp();
053:            }
054:
055:            /**
056:             * @see TestCase#tearDown()
057:             */
058:            protected void tearDown() throws Exception {
059:                super .tearDown();
060:            }
061:
062:            public void testLineParse0() throws Exception {
063:                StringEndPoint io = new StringEndPoint();
064:                io.setInput("POST /foo HTTP/1.0\015\012" + "\015\012");
065:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
066:                SimpleBuffers buffers = new SimpleBuffers(
067:                        new Buffer[] { buffer });
068:
069:                Handler handler = new Handler();
070:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
071:                        .capacity(), 0);
072:                parser.parse();
073:                assertEquals("POST", f0);
074:                assertEquals("/foo", f1);
075:                assertEquals("HTTP/1.0", f2);
076:                assertEquals(-1, h);
077:            }
078:
079:            public void testLineParse1() throws Exception {
080:                StringEndPoint io = new StringEndPoint();
081:                io.setInput("GET /999\015\012");
082:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
083:                SimpleBuffers buffers = new SimpleBuffers(
084:                        new Buffer[] { buffer });
085:
086:                f2 = null;
087:                Handler handler = new Handler();
088:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
089:                        .capacity(), 0);
090:                parser.parse();
091:                assertEquals("GET", f0);
092:                assertEquals("/999", f1);
093:                assertEquals(null, f2);
094:                assertEquals(-1, h);
095:            }
096:
097:            public void testLineParse2() throws Exception {
098:                StringEndPoint io = new StringEndPoint();
099:                io.setInput("POST /222  \015\012");
100:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
101:                SimpleBuffers buffers = new SimpleBuffers(
102:                        new Buffer[] { buffer });
103:
104:                f2 = null;
105:                Handler handler = new Handler();
106:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
107:                        .capacity(), 0);
108:                parser.parse();
109:                assertEquals("POST", f0);
110:                assertEquals("/222", f1);
111:                assertEquals(null, f2);
112:                assertEquals(-1, h);
113:            }
114:
115:            public void testLineParse3() throws Exception {
116:                StringEndPoint io = new StringEndPoint();
117:                io.setInput("POST /fo\u0690 HTTP/1.0\015\012" + "\015\012");
118:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
119:                SimpleBuffers buffers = new SimpleBuffers(
120:                        new Buffer[] { buffer });
121:
122:                Handler handler = new Handler();
123:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
124:                        .capacity(), 0);
125:                parser.parse();
126:                assertEquals("POST", f0);
127:                assertEquals("/fo\u0690", f1);
128:                assertEquals("HTTP/1.0", f2);
129:                assertEquals(-1, h);
130:            }
131:
132:            public void testLineParse4() throws Exception {
133:                StringEndPoint io = new StringEndPoint();
134:                io.setInput("POST /foo?param=\u0690 HTTP/1.0\015\012"
135:                        + "\015\012");
136:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
137:                SimpleBuffers buffers = new SimpleBuffers(
138:                        new Buffer[] { buffer });
139:
140:                Handler handler = new Handler();
141:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
142:                        .capacity(), 0);
143:                parser.parse();
144:                assertEquals("POST", f0);
145:                assertEquals("/foo?param=\u0690", f1);
146:                assertEquals("HTTP/1.0", f2);
147:                assertEquals(-1, h);
148:            }
149:
150:            public void testHeaderParse() throws Exception {
151:                StringEndPoint io = new StringEndPoint();
152:                io.setInput("GET / HTTP/1.0\015\012"
153:                        + "Header1: value1\015\012"
154:                        + "Header2  :   value 2a  \015\012"
155:                        + "                    value 2b  \015\012"
156:                        + "Header3: \015\012" + "Header4 \015\012"
157:                        + "  value4\015\012" + "\015\012");
158:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
159:                SimpleBuffers buffers = new SimpleBuffers(
160:                        new Buffer[] { buffer });
161:
162:                Handler handler = new Handler();
163:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
164:                        .capacity(), 0);
165:                parser.parse();
166:                assertEquals("GET", f0);
167:                assertEquals("/", f1);
168:                assertEquals("HTTP/1.0", f2);
169:                assertEquals("Header1", hdr[0]);
170:                assertEquals("value1", val[0]);
171:                assertEquals("Header2", hdr[1]);
172:                assertEquals("value 2a value 2b", val[1]);
173:                assertEquals("Header3", hdr[2]);
174:                assertEquals("", val[2]);
175:                assertEquals("Header4", hdr[3]);
176:                assertEquals("value4", val[3]);
177:                assertEquals(3, h);
178:            }
179:
180:            public void testChunkParse() throws Exception {
181:                StringEndPoint io = new StringEndPoint();
182:                io.setInput("GET /chunk HTTP/1.0\015\012"
183:                        + "Header1: value1\015\012"
184:                        + "Transfer-Encoding: chunked\015\012" + "\015\012"
185:                        + "a;\015\012" + "0123456789\015\012" + "1a\015\012"
186:                        + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012" + "0\015\012");
187:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
188:                ByteArrayBuffer content = new ByteArrayBuffer(8192);
189:                SimpleBuffers buffers = new SimpleBuffers(new Buffer[] {
190:                        buffer, content });
191:
192:                Handler handler = new Handler();
193:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
194:                        .capacity(), content.capacity());
195:                parser.parse();
196:                assertEquals("GET", f0);
197:                assertEquals("/chunk", f1);
198:                assertEquals("HTTP/1.0", f2);
199:                assertEquals(1, h);
200:                assertEquals("Header1", hdr[0]);
201:                assertEquals("value1", val[0]);
202:                assertEquals("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", _content);
203:            }
204:
205:            public void testMultiParse() throws Exception {
206:                StringEndPoint io = new StringEndPoint();
207:                io.setInput("GET /mp HTTP/1.0\015\012"
208:                        + "Header1: value1\015\012"
209:                        + "Transfer-Encoding: chunked\015\012" + "\015\012"
210:                        + "a;\015\012" + "0123456789\015\012" + "1a\015\012"
211:                        + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012" + "0\015\012"
212:                        + "POST /foo HTTP/1.0\015\012"
213:                        + "Header2: value2\015\012"
214:                        + "Content-Length: 0\015\012" + "\015\012"
215:                        + "PUT /doodle HTTP/1.0\015\012"
216:                        + "Header3: value3\015\012"
217:                        + "Content-Length: 10\015\012" + "\015\012"
218:                        + "0123456789\015\012");
219:
220:                ByteArrayBuffer buffer = new ByteArrayBuffer(4096);
221:                ByteArrayBuffer content = new ByteArrayBuffer(8192);
222:                SimpleBuffers buffers = new SimpleBuffers(new Buffer[] {
223:                        buffer, content });
224:
225:                Handler handler = new Handler();
226:                HttpParser parser = new HttpParser(buffers, io, handler, buffer
227:                        .capacity(), content.capacity());
228:                parser.parse();
229:                assertEquals("GET", f0);
230:                assertEquals("/mp", f1);
231:                assertEquals("HTTP/1.0", f2);
232:                assertEquals(1, h);
233:                assertEquals("Header1", hdr[0]);
234:                assertEquals("value1", val[0]);
235:                assertEquals("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", _content);
236:
237:                parser.parse();
238:                assertEquals("POST", f0);
239:                assertEquals("/foo", f1);
240:                assertEquals("HTTP/1.0", f2);
241:                assertEquals(1, h);
242:                assertEquals("Header2", hdr[0]);
243:                assertEquals("value2", val[0]);
244:                assertEquals(null, _content);
245:
246:                parser.parse();
247:                assertEquals("PUT", f0);
248:                assertEquals("/doodle", f1);
249:                assertEquals("HTTP/1.0", f2);
250:                assertEquals(1, h);
251:                assertEquals("Header3", hdr[0]);
252:                assertEquals("value3", val[0]);
253:                assertEquals("0123456789", _content);
254:            }
255:
256:            public void testStreamParse() throws Exception {
257:                StringEndPoint io = new StringEndPoint();
258:                String http = "GET / HTTP/1.0\015\012"
259:                        + "Header1: value1\015\012"
260:                        + "Transfer-Encoding: chunked\015\012" + "\015\012"
261:                        + "a;\015\012" + "0123456789\015\012" + "1a\015\012"
262:                        + "ABCDEFGHIJKLMNOPQRSTUVWXYZ\015\012" + "0\015\012"
263:                        + "POST /foo HTTP/1.0\015\012"
264:                        + "Header2: value2\015\012"
265:                        + "Content-Length: 0\015\012" + "\015\012"
266:                        + "PUT /doodle HTTP/1.0\015\012"
267:                        + "Header3: value3\015\012"
268:                        + "Content-Length: 10\015\012" + "\015\012"
269:                        + "0123456789\015\012";
270:
271:                int[] tests = { 1024, http.length() + 3, http.length() + 2,
272:                        http.length() + 1, http.length() + 0,
273:                        http.length() - 1, http.length() - 2,
274:                        http.length() / 2, http.length() / 3, 64, 32 };
275:
276:                for (int t = 0; t < tests.length; t++) {
277:                    String tst = "t" + tests[t];
278:                    try {
279:                        ByteArrayBuffer buffer = new ByteArrayBuffer(tests[t]);
280:                        ByteArrayBuffer content = new ByteArrayBuffer(8192);
281:                        SimpleBuffers buffers = new SimpleBuffers(new Buffer[] {
282:                                buffer, content });
283:
284:                        Handler handler = new Handler();
285:                        HttpParser parser = new HttpParser(buffers, io,
286:                                handler, buffer.capacity(), content.capacity());
287:
288:                        io.setInput(http);
289:
290:                        parser.parse();
291:                        assertEquals(tst, "GET", f0);
292:                        assertEquals(tst, "/", f1);
293:                        assertEquals(tst, "HTTP/1.0", f2);
294:                        assertEquals(tst, 1, h);
295:                        assertEquals(tst, "Header1", hdr[0]);
296:                        assertEquals(tst, "value1", val[0]);
297:                        assertEquals(tst,
298:                                "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
299:                                _content);
300:
301:                        parser.parse();
302:                        assertEquals(tst, "POST", f0);
303:                        assertEquals(tst, "/foo", f1);
304:                        assertEquals(tst, "HTTP/1.0", f2);
305:                        assertEquals(tst, 1, h);
306:                        assertEquals(tst, "Header2", hdr[0]);
307:                        assertEquals(tst, "value2", val[0]);
308:                        assertEquals(tst, null, _content);
309:
310:                        parser.parse();
311:                        assertEquals(tst, "PUT", f0);
312:                        assertEquals(tst, "/doodle", f1);
313:                        assertEquals(tst, "HTTP/1.0", f2);
314:                        assertEquals(tst, 1, h);
315:                        assertEquals(tst, "Header3", hdr[0]);
316:                        assertEquals(tst, "value3", val[0]);
317:                        assertEquals(tst, "0123456789", _content);
318:                    } catch (Exception e) {
319:                        if (t + 1 < tests.length)
320:                            throw e;
321:                        assertTrue(e.toString().indexOf("FULL") >= 0);
322:                    }
323:                }
324:            }
325:
326:            String _content;
327:            String f0;
328:            String f1;
329:            String f2;
330:            String[] hdr;
331:            String[] val;
332:            int h;
333:
334:            class Handler extends HttpParser.EventHandler {
335:                HttpFields fields;
336:
337:                public void content(Buffer ref) {
338:                    if (_content == null)
339:                        _content = "";
340:                    _content = _content + ref;
341:                }
342:
343:                public void startRequest(Buffer tok0, Buffer tok1, Buffer tok2) {
344:                    try {
345:                        h = -1;
346:                        hdr = new String[9];
347:                        val = new String[9];
348:                        f0 = tok0.toString();
349:                        f1 = new String(tok1.array(), tok1.getIndex(), tok1
350:                                .length(), StringUtil.__UTF8);
351:                        if (tok2 != null)
352:                            f2 = tok2.toString();
353:                        else
354:                            f2 = null;
355:
356:                        fields = new HttpFields();
357:                    } catch (UnsupportedEncodingException e) {
358:                        // TODO Auto-generated catch block
359:                        e.printStackTrace();
360:                    }
361:                }
362:
363:                public void parsedHeader(Buffer name, Buffer value) {
364:                    fields.add(name, value);
365:                    hdr[++h] = name.toString();
366:                    val[h] = value.toString();
367:                }
368:
369:                public void headerComplete() {
370:                    _content = null;
371:                    String s0 = fields.toString();
372:                    String s1 = fields.toString();
373:                    if (!s0.equals(s1)) {
374:                        //System.err.println(s0);
375:                        //System.err.println(s1);
376:                        throw new IllegalStateException();
377:                    }
378:                }
379:
380:                public void messageComplete(long contentLength) {
381:                }
382:
383:                /* (non-Javadoc)
384:                 * @see org.mortbay.jetty.EventHandler#startResponse(org.mortbay.io.Buffer, int, org.mortbay.io.Buffer)
385:                 */
386:                public void startResponse(Buffer version, int status,
387:                        Buffer reason) {
388:                    // TODO Auto-generated method stub
389:
390:                }
391:            }
392:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.