0001: /* ====================================================================
0002: The Jicarilla Software License
0003:
0004: Copyright (c) 2003 Leo Simons.
0005: All rights reserved.
0006:
0007: Permission is hereby granted, free of charge, to any person obtaining
0008: a copy of this software and associated documentation files (the
0009: "Software"), to deal in the Software without restriction, including
0010: without limitation the rights to use, copy, modify, merge, publish,
0011: distribute, sublicense, and/or sell copies of the Software, and to
0012: permit persons to whom the Software is furnished to do so, subject to
0013: the following conditions:
0014:
0015: The above copyright notice and this permission notice shall be
0016: included in all copies or substantial portions of the Software.
0017:
0018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0025: ==================================================================== */
0026: package org.jicarilla.http.test;
0027:
0028: import junit.framework.TestCase;
0029: import org.easymock.MockControl;
0030: import org.jicarilla.http.HTTPEncoding;
0031: import org.jicarilla.http.HTTPErrorHandler;
0032: import org.jicarilla.http.HTTPException;
0033: import org.jicarilla.http.HTTPHandler;
0034: import org.jicarilla.http.HTTPParser;
0035: import org.jicarilla.http.HTTPParserImpl;
0036: import org.jicarilla.http.util.Iso646;
0037: import org.jicarilla.http.util.NioUtil;
0038:
0039: import java.io.File;
0040: import java.io.FileInputStream;
0041: import java.io.InputStream;
0042: import java.io.StringBufferInputStream;
0043: import java.nio.ByteBuffer;
0044: import java.nio.channels.Channels;
0045: import java.nio.channels.FileChannel;
0046: import java.nio.channels.ReadableByteChannel;
0047:
0048: /**
0049: *
0050: *
0051: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
0052: * @version $Id: HTTPParserImplTestCase.java,v 1.7 2004/04/03 10:13:24 lsimons Exp $
0053: */
0054: public class HTTPParserImplTestCase extends TestCase {
0055: // ----------------------------------------------------------------------
0056: // Properties
0057: // ----------------------------------------------------------------------
0058: protected HTTPHandler handler;
0059: protected HTTPErrorHandler errorHandler;
0060: protected HTTPParser parser;
0061:
0062: protected MockControl handlerControl;
0063: protected MockControl errorHandlerControl;
0064:
0065: protected InputStream stream;
0066: protected ReadableByteChannel channel;
0067:
0068: // ----------------------------------------------------------------------
0069: // Set up and tear down
0070: // ----------------------------------------------------------------------
0071:
0072: protected void setUp() throws Exception {
0073: super .setUp();
0074:
0075: handlerControl = MockControl
0076: .createStrictControl(HTTPHandler.class);
0077: handler = (HTTPHandler) handlerControl.getMock();
0078:
0079: errorHandlerControl = MockControl
0080: .createStrictControl(HTTPErrorHandler.class);
0081: errorHandler = (HTTPErrorHandler) errorHandlerControl.getMock();
0082:
0083: parser = new HTTPParserImpl(handler, errorHandler);
0084: }
0085:
0086: protected void tearDown() throws Exception {
0087: super .tearDown();
0088:
0089: if (stream != null)
0090: stream.close();
0091: if (stream != null)
0092: channel.close();
0093: }
0094:
0095: // ----------------------------------------------------------------------
0096: // Basic API Tests
0097: // ----------------------------------------------------------------------
0098: public void testReset() {
0099: handler.endMessage();
0100: handlerControl.replay();
0101: errorHandlerControl.replay();
0102:
0103: parser.reset();
0104:
0105: errorHandlerControl.verify();
0106: handlerControl.verify();
0107: }
0108:
0109: public void testGetAndSetHandler() {
0110: parser.setHandler(handler);
0111: assertEquals(handler, parser.getHandler());
0112: }
0113:
0114: public void testGetAndSetErrorHandler() {
0115: parser.setErrorHandler(errorHandler);
0116: assertEquals(errorHandler, parser.getErrorHandler());
0117: }
0118:
0119: public void testParseBasics() throws HTTPException {
0120: parser.parse(ByteBuffer.allocate(0));
0121:
0122: parser.parse(ByteBuffer.allocate(0), 0);
0123: parser.parse(ByteBuffer.allocate(10), 0);
0124: parser.parse(ByteBuffer.allocate(10), -10);
0125:
0126: int tooBigForSure = 100;
0127: parser.parse(NioUtil.toByteBuffer("GET "), tooBigForSure);
0128: }
0129:
0130: // ----------------------------------------------------------------------
0131: // Basic API Exception Tests
0132: // ----------------------------------------------------------------------
0133: public void testSetHandlerDoesNotAcceptNullArgument() {
0134: try {
0135: parser.setHandler(null);
0136: fail("Expected an exception!");
0137: } catch (AssertionError ae) {
0138: }
0139: }
0140:
0141: public void testSetErrorHandlerDoesNotAcceptNullArgument() {
0142: try {
0143: parser.setErrorHandler(null);
0144: fail("Expected an exception!");
0145: } catch (AssertionError ae) {
0146: }
0147: }
0148:
0149: public void testParseDoesNotAcceptNullArgument()
0150: throws HTTPException {
0151: try {
0152: parser.parse(null);
0153: fail("Expected an exception!");
0154: } catch (AssertionError ae) {
0155: }
0156:
0157: try {
0158: parser.parse(null, 0);
0159: fail("Expected an exception!");
0160: } catch (AssertionError ae) {
0161: }
0162: }
0163:
0164: public void testResetForwardsHandlerException() {
0165: handler.endMessage();
0166: handlerControl.setThrowable(new RuntimeException());
0167: handlerControl.replay();
0168: errorHandlerControl.replay();
0169:
0170: try {
0171: parser.reset();
0172: fail("Expected an exception!");
0173: } catch (RuntimeException th) {
0174: }
0175:
0176: errorHandlerControl.verify();
0177: handlerControl.verify();
0178: }
0179:
0180: public void testParseForwardsHandlerException() throws Exception {
0181: final RuntimeException ex = new RuntimeException();
0182:
0183: handler.newMessage();
0184: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0185: handlerControl.setThrowable(ex);
0186: handlerControl.replay();
0187: errorHandlerControl.replay();
0188:
0189: try {
0190: doParse("GET / HTTP/1.0" + HTTPEncoding.CR
0191: + HTTPEncoding.LF + HTTPEncoding.CR
0192: + HTTPEncoding.LF);
0193: fail("Expected an exception!");
0194: } catch (Throwable th) {
0195: assertEquals(ex, th);
0196: }
0197:
0198: handlerControl.verify();
0199: errorHandlerControl.verify();
0200: }
0201:
0202: // ----------------------------------------------------------------------
0203: // Basic Protocol Tests
0204: // ----------------------------------------------------------------------
0205: public void testParseSimpleRequest() throws Exception {
0206: handler.newMessage();
0207: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0208: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0209: handler.foundStartLineThirdField(NioUtil
0210: .toByteBuffer("HTTP/1.0"));
0211: handler.getBodyType();
0212: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0213: handler.endMessage();
0214: handlerControl.replay();
0215:
0216: errorHandlerControl.replay();
0217:
0218: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0219: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF;
0220: doParse(request);
0221:
0222: errorHandlerControl.verify();
0223: handlerControl.verify();
0224: }
0225:
0226: public void testParseRequest() throws Exception {
0227: handler.newMessage();
0228:
0229: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0230: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0231: handler.foundStartLineThirdField(NioUtil
0232: .toByteBuffer("HTTP/1.1"));
0233:
0234: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0235: handler
0236: .foundHeaderValue(NioUtil
0237: .toByteBuffer("localhost:8080"));
0238: handler.foundHeaderName(NioUtil.toByteBuffer("User-Agent"));
0239: handler
0240: .foundHeaderValue(NioUtil
0241: .toByteBuffer("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"));
0242: handler.foundHeaderName(NioUtil.toByteBuffer("Accept"));
0243: handler
0244: .foundHeaderValue(NioUtil
0245: .toByteBuffer("text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"));
0246: handler
0247: .foundHeaderName(NioUtil
0248: .toByteBuffer("Accept-Language"));
0249: handler
0250: .foundHeaderValue(NioUtil
0251: .toByteBuffer("en-us,en;q=0.5"));
0252: handler
0253: .foundHeaderName(NioUtil
0254: .toByteBuffer("Accept-Encoding"));
0255: handler.foundHeaderValue(NioUtil.toByteBuffer("gzip,deflate"));
0256: handler.foundHeaderName(NioUtil.toByteBuffer("Accept-Charset"));
0257: handler.foundHeaderValue(NioUtil
0258: .toByteBuffer("ISO-8859-1,utf-8;q=0.7,*;q=0.7"));
0259: handler.foundHeaderName(NioUtil.toByteBuffer("Keep-Alive"));
0260: handler.foundHeaderValue(NioUtil.toByteBuffer("300"));
0261: handler.foundHeaderName(NioUtil.toByteBuffer("Connection"));
0262: handler.foundHeaderValue(NioUtil.toByteBuffer("keep-alive"));
0263: handler.foundHeaderName(NioUtil.toByteBuffer("Cache-Control"));
0264: handler.foundHeaderValue(NioUtil.toByteBuffer("m_upto-age=0"));
0265:
0266: handler.getBodyType();
0267: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0268: handler.endMessage();
0269: handlerControl.replay();
0270:
0271: errorHandlerControl.replay();
0272:
0273: final String request = "GET / HTTP/1.1"
0274: + HTTPEncoding.CR
0275: + HTTPEncoding.LF
0276: + "Host: localhost:8080"
0277: + HTTPEncoding.CR
0278: + HTTPEncoding.LF
0279: + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"
0280: + HTTPEncoding.CR
0281: + HTTPEncoding.LF
0282: + "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"
0283: + HTTPEncoding.CR + HTTPEncoding.LF
0284: + "Accept-Language:\t en-us,en;q=0.5"
0285: + HTTPEncoding.CR + HTTPEncoding.LF
0286: + "Accept-Encoding: gzip,deflate" + HTTPEncoding.CR
0287: + HTTPEncoding.LF
0288: + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
0289: + HTTPEncoding.CR + HTTPEncoding.LF + "Keep-Alive: 300"
0290: + HTTPEncoding.CR + HTTPEncoding.LF
0291: + "Connection: keep-alive" + HTTPEncoding.CR
0292: + HTTPEncoding.LF + "Cache-Control: m_upto-age=0"
0293: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0294: + HTTPEncoding.LF;
0295:
0296: doParse(request);
0297:
0298: errorHandlerControl.verify();
0299: handlerControl.verify();
0300: }
0301:
0302: public void testParseWeirdRequest() throws Exception {
0303: handler.newMessage();
0304: handler.foundStartLineFirstField(NioUtil
0305: .toByteBuffer("WEIRDUNKNOWNMETHOD"));
0306: handler
0307: .foundStartLineSecondField(NioUtil
0308: .toByteBuffer("/some=contrived?but/valid%20URLRRLRLR/&thisisFun"));
0309: handler.foundStartLineThirdField(NioUtil
0310: .toByteBuffer("HTTP/10.12"));
0311: handler.getBodyType();
0312: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0313: handler.endMessage();
0314: handlerControl.replay();
0315:
0316: errorHandlerControl.replay();
0317:
0318: final String request = "WEIRDUNKNOWNMETHOD /some=contrived?but/valid%20URLRRLRLR/&thisisFun HTTP/10.12"
0319: + HTTPEncoding.CR
0320: + HTTPEncoding.LF
0321: + HTTPEncoding.CR
0322: + HTTPEncoding.LF;
0323:
0324: doParse(request);
0325:
0326: errorHandlerControl.verify();
0327: handlerControl.verify();
0328: }
0329:
0330: public void testParseResponseLine() throws Exception {
0331: handler.newMessage();
0332: handler.foundStartLineFirstField(NioUtil
0333: .toByteBuffer("HTTP/1.0"));
0334: handler.foundStartLineSecondField(NioUtil.toByteBuffer("200"));
0335: handler.foundStartLineThirdField(NioUtil.toByteBuffer("OK"));
0336: handlerControl.replay();
0337:
0338: errorHandlerControl.replay();
0339:
0340: final String request = "HTTP/1.0 200 OK" + HTTPEncoding.CR
0341: + HTTPEncoding.LF;
0342:
0343: doParse(request);
0344:
0345: errorHandlerControl.verify();
0346: handlerControl.verify();
0347: }
0348:
0349: public void testParseResponseLine2() throws Exception {
0350: handler.newMessage();
0351: handler.foundStartLineFirstField(NioUtil
0352: .toByteBuffer("HTTP/13.140"));
0353: handler.foundStartLineSecondField(NioUtil.toByteBuffer("800"));
0354: handler.foundStartLineThirdField(NioUtil
0355: .toByteBuffer("Custom Status Code"));
0356: handlerControl.replay();
0357:
0358: errorHandlerControl.replay();
0359:
0360: final String request = "HTTP/13.140 800 Custom Status Code"
0361: + HTTPEncoding.CR + HTTPEncoding.LF;
0362: doParse(request);
0363:
0364: errorHandlerControl.verify();
0365: handlerControl.verify();
0366: }
0367:
0368: public void testParseHeaders() throws Exception {
0369: handler.newMessage();
0370: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0371: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0372: handler.foundStartLineThirdField(NioUtil
0373: .toByteBuffer("HTTP/1.0"));
0374:
0375: handler.foundHeaderName(NioUtil.toByteBuffer("A"));
0376: handler.foundHeaderValue(NioUtil.toByteBuffer("b"));
0377: handler
0378: .foundHeaderName(NioUtil
0379: .toByteBuffer("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0380: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0381: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0382: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
0383: handler.foundHeaderValue(NioUtil.toByteBuffer("b"
0384: + HTTPEncoding.CR + HTTPEncoding.LF + " b"));
0385: handler.foundHeaderName(NioUtil.toByteBuffer("---"));
0386: handler.foundHeaderValue(NioUtil.toByteBuffer("xyz"));
0387: handler.foundHeaderName(NioUtil
0388: .toByteBuffer("-a-b-cD12345678-90QWE"));
0389: handler.foundHeaderValue(NioUtil.toByteBuffer(" a"
0390: + HTTPEncoding.CR + HTTPEncoding.LF + " a"
0391: + HTTPEncoding.CR + HTTPEncoding.LF + " a"
0392: + HTTPEncoding.CR + HTTPEncoding.LF + " a"));
0393: handler.foundHeaderName(NioUtil.toByteBuffer("-"));
0394: handler.foundHeaderValue(NioUtil.toByteBuffer("p"));
0395:
0396: handler.getBodyType();
0397: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0398: handler.endMessage();
0399: handlerControl.replay();
0400:
0401: errorHandlerControl.replay();
0402:
0403: final String request = "GET / HTTP/1.0"
0404: + HTTPEncoding.CR
0405: + HTTPEncoding.LF
0406: + "A: b"
0407: + HTTPEncoding.CR
0408: + HTTPEncoding.LF
0409: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0410: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0411: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
0412: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:"
0413: + " " + HTTPEncoding.HT
0414: + " b" + HTTPEncoding.CR + HTTPEncoding.LF
0415: + " b" + HTTPEncoding.CR + HTTPEncoding.LF
0416: + "---:xyz" + HTTPEncoding.CR + HTTPEncoding.LF
0417: + "-a-b-cD12345678-90QWE:" + HTTPEncoding.CR
0418: + HTTPEncoding.LF + " a" + HTTPEncoding.CR
0419: + HTTPEncoding.LF + " a" + HTTPEncoding.CR
0420: + HTTPEncoding.LF + " a" + HTTPEncoding.CR
0421: + HTTPEncoding.LF + " a" + HTTPEncoding.CR
0422: + HTTPEncoding.LF + "-:\tp" + HTTPEncoding.CR
0423: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF;
0424: doParse(request);
0425:
0426: errorHandlerControl.verify();
0427: handlerControl.verify();
0428: }
0429:
0430: public void testParseNoBody() throws Exception {
0431: handler.newMessage();
0432: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0433: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0434: handler.foundStartLineThirdField(NioUtil
0435: .toByteBuffer("HTTP/1.0"));
0436: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0437: handler
0438: .foundHeaderValue(NioUtil
0439: .toByteBuffer("localhost:8080"));
0440: handler.getBodyType();
0441: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0442: handler.endMessage();
0443:
0444: // next request
0445: handler.newMessage();
0446: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0447: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0448: handlerControl.replay();
0449:
0450: errorHandlerControl.replay();
0451:
0452: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0453: + HTTPEncoding.LF + "Host: localhost:8080"
0454: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0455: + HTTPEncoding.LF +
0456:
0457: // next request
0458: "GET / ";
0459: doParse(request);
0460:
0461: errorHandlerControl.verify();
0462: handlerControl.verify();
0463: }
0464:
0465: public void testParseNormalBody() throws Exception {
0466: handler.newMessage();
0467: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0468: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0469: handler.foundStartLineThirdField(NioUtil
0470: .toByteBuffer("HTTP/1.0"));
0471: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0472: handler
0473: .foundHeaderValue(NioUtil
0474: .toByteBuffer("localhost:8080"));
0475: handler.getBodyType();
0476: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NORMAL);
0477: handler.getBodySize();
0478: handlerControl.setReturnValue(5);
0479: handler.foundBody(NioUtil.toByteBuffer("12345"));
0480: handler.endMessage();
0481: handlerControl.replay();
0482:
0483: errorHandlerControl.replay();
0484:
0485: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0486: + HTTPEncoding.LF + "Host: localhost:8080"
0487: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0488: + HTTPEncoding.LF +
0489:
0490: // body
0491: "12345";
0492: doParse(request);
0493:
0494: errorHandlerControl.verify();
0495: handlerControl.verify();
0496: }
0497:
0498: public void testParseNormalBodyContinuesWithNextRequest()
0499: throws Exception {
0500: handler.newMessage();
0501: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0502: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0503: handler.foundStartLineThirdField(NioUtil
0504: .toByteBuffer("HTTP/1.0"));
0505: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0506: handler
0507: .foundHeaderValue(NioUtil
0508: .toByteBuffer("localhost:8080"));
0509: handler.getBodyType();
0510: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NORMAL);
0511: handler.getBodySize();
0512: handlerControl.setReturnValue(5);
0513: handler.foundBody(NioUtil.toByteBuffer("12345"));
0514: handler.endMessage();
0515:
0516: // next request
0517: handler.newMessage();
0518: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0519: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0520: handlerControl.replay();
0521:
0522: errorHandlerControl.replay();
0523:
0524: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0525: + HTTPEncoding.LF + "Host: localhost:8080"
0526: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0527: + HTTPEncoding.LF +
0528:
0529: // body
0530: "12345" +
0531:
0532: // next request
0533: "GET / ";
0534: doParse(request);
0535:
0536: errorHandlerControl.verify();
0537: handlerControl.verify();
0538: }
0539:
0540: public void testParseZeroLengthBody() throws Exception {
0541: handler.newMessage();
0542: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0543: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0544: handler.foundStartLineThirdField(NioUtil
0545: .toByteBuffer("HTTP/1.0"));
0546: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0547: handler
0548: .foundHeaderValue(NioUtil
0549: .toByteBuffer("localhost:8080"));
0550: handler.getBodyType();
0551: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NORMAL);
0552: handler.getBodySize();
0553: handlerControl.setReturnValue(0);
0554: //handler.foundBody( ByteBuffer.allocate( 0 ) );
0555: handler.endMessage();
0556:
0557: // next request
0558: handler.newMessage();
0559: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0560: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0561: handlerControl.replay();
0562:
0563: errorHandlerControl.replay();
0564:
0565: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0566: + HTTPEncoding.LF + "Host: localhost:8080"
0567: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0568: + HTTPEncoding.LF +
0569:
0570: // next request
0571: "GET / H";
0572: doParse(request);
0573:
0574: errorHandlerControl.verify();
0575: handlerControl.verify();
0576: }
0577:
0578: // ----------------------------------------------------------------------
0579: // More Complex Protocol Tests
0580: // ----------------------------------------------------------------------
0581:
0582: public void testParseSkipCRLFBeforeRequest() throws Exception {
0583: handler.newMessage();
0584: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0585: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0586: handler.foundStartLineThirdField(NioUtil
0587: .toByteBuffer("HTTP/1.0"));
0588: handlerControl.replay();
0589:
0590: errorHandlerControl.replay();
0591:
0592: final String request = "" + HTTPEncoding.CR + HTTPEncoding.LF
0593: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0594: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF
0595: + "GET / HTTP/1.0" + HTTPEncoding.CR + HTTPEncoding.LF;
0596: doParse(request);
0597:
0598: errorHandlerControl.verify();
0599: handlerControl.verify();
0600: }
0601:
0602: /** @todo support chunked transfer coding */
0603: public void testParseChunkedBody() throws Exception {
0604: handler.newMessage();
0605: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0606: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0607: handler.foundStartLineThirdField(NioUtil
0608: .toByteBuffer("HTTP/1.0"));
0609: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0610: handler
0611: .foundHeaderValue(NioUtil
0612: .toByteBuffer("localhost:8080"));
0613: handler.getBodyType();
0614: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_CHUNKING);
0615: handlerControl.replay();
0616:
0617: //errorHandler.exceptionOccurred( new HTTPException() );
0618: errorHandlerControl.replay();
0619:
0620: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0621: + HTTPEncoding.LF + "Host: localhost:8080"
0622: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0623: + HTTPEncoding.LF +
0624:
0625: // body
0626: "5" + HTTPEncoding.CR + HTTPEncoding.LF + "12345";
0627:
0628: try {
0629: doParse(request);
0630: fail("Expected an exception!");
0631: } catch (HTTPException th) {
0632: }
0633:
0634: errorHandlerControl.verify();
0635: handlerControl.verify();
0636: }
0637:
0638: // ----------------------------------------------------------------------
0639: // Multi-buffer Tests
0640: // ----------------------------------------------------------------------
0641:
0642: public void testParseSimpleDoubleBufferRequest() throws Exception {
0643: handler.newMessage();
0644: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0645: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0646: handler.foundStartLineThirdField(NioUtil
0647: .toByteBuffer("HTTP/1.0"));
0648: handler.getBodyType();
0649: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0650: handler.endMessage();
0651: handlerControl.replay();
0652:
0653: errorHandlerControl.replay();
0654:
0655: String request = "GET / HTTP";
0656: doParse(request);
0657: request = "/1.0" + HTTPEncoding.CR + HTTPEncoding.LF
0658: + HTTPEncoding.CR + HTTPEncoding.LF;
0659: doParse(request);
0660:
0661: errorHandlerControl.verify();
0662: handlerControl.verify();
0663: }
0664:
0665: public void testParseSimpleMultiBufferRequest() throws Exception {
0666: handler.newMessage();
0667: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0668: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0669: handler.foundStartLineThirdField(NioUtil
0670: .toByteBuffer("HTTP/1.0"));
0671: handler.getBodyType();
0672: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0673: handler.endMessage();
0674: handlerControl.replay();
0675:
0676: errorHandlerControl.replay();
0677:
0678: final String[] request = new String[] {
0679: "GE",
0680: "T /",
0681: " HTT",
0682: "P",
0683: "/1.0",
0684: "" + HTTPEncoding.CR,
0685: "" + HTTPEncoding.LF + HTTPEncoding.CR
0686: + HTTPEncoding.LF };
0687: for (int i = 0; i < request.length; i++) {
0688: final String s = request[i];
0689: doParse(s);
0690: }
0691:
0692: errorHandlerControl.verify();
0693: handlerControl.verify();
0694: }
0695:
0696: public void testParseMultiBufferRequest() throws Exception {
0697: handler.newMessage();
0698:
0699: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0700: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0701: handler.foundStartLineThirdField(NioUtil
0702: .toByteBuffer("HTTP/1.1"));
0703:
0704: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0705: handler
0706: .foundHeaderValue(NioUtil
0707: .toByteBuffer("localhost:8080"));
0708: handler.foundHeaderName(NioUtil.toByteBuffer("User-Agent"));
0709: handler
0710: .foundHeaderValue(NioUtil
0711: .toByteBuffer("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"));
0712: handler.foundHeaderName(NioUtil.toByteBuffer("Accept"));
0713: handler
0714: .foundHeaderValue(NioUtil
0715: .toByteBuffer("text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"));
0716: handler
0717: .foundHeaderName(NioUtil
0718: .toByteBuffer("Accept-Language"));
0719: handler.foundHeaderValue(NioUtil
0720: .toByteBuffer(" en-us,en;q=0.5"));
0721: handler
0722: .foundHeaderName(NioUtil
0723: .toByteBuffer("Accept-Encoding"));
0724: handler.foundHeaderValue(NioUtil.toByteBuffer("gzip,deflate"));
0725: handler.foundHeaderName(NioUtil.toByteBuffer("Accept-Charset"));
0726: handler.foundHeaderValue(NioUtil
0727: .toByteBuffer("ISO-8859-1,utf-8;q=0.7,*;q=0.7"));
0728: handler.foundHeaderName(NioUtil.toByteBuffer("Keep-Alive"));
0729: handler.foundHeaderValue(NioUtil.toByteBuffer("300"));
0730: handler.foundHeaderName(NioUtil.toByteBuffer("Connection"));
0731: handler.foundHeaderValue(NioUtil.toByteBuffer("keep-alive"));
0732: handler.foundHeaderName(NioUtil.toByteBuffer("Cache-Control"));
0733: handler.foundHeaderValue(NioUtil.toByteBuffer("m_upto-age=0"));
0734:
0735: handler.getBodyType();
0736: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NONE);
0737: handler.endMessage();
0738: handlerControl.replay();
0739:
0740: errorHandlerControl.replay();
0741:
0742: final String[] request = new String[] {
0743: "GE",
0744: "T /",
0745: " HTT",
0746: "P",
0747: "/1.1",
0748: "" + HTTPEncoding.CR,
0749: "" + HTTPEncoding.LF,
0750: "Host: localhost:8080"
0751: + HTTPEncoding.CR
0752: + HTTPEncoding.LF
0753: + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"
0754: + HTTPEncoding.CR + HTTPEncoding.LF,
0755: "Acc",
0756: "ept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"
0757: + HTTPEncoding.CR
0758: + HTTPEncoding.LF
0759: + "Accept-Language:\t ",
0760: "",
0761: "" + HTTPEncoding.CR + HTTPEncoding.LF,
0762: " en-us,en;q=0.5" + HTTPEncoding.CR + HTTPEncoding.LF,
0763: //"Accept-Language: ",
0764: //""+HTTPEncoding.CR + HTTPEncoding.LF,
0765: //"en-us,en;q=0.5" + HTTPEncoding.CR + HTTPEncoding.LF +
0766: "Accept-Encoding: gzip,deflate"
0767: + HTTPEncoding.CR
0768: + HTTPEncoding.LF
0769: + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
0770: + HTTPEncoding.CR + HTTPEncoding.LF
0771: + "Keep-Alive: 300" + HTTPEncoding.CR
0772: + HTTPEncoding.LF,
0773: "Connection: keep-alive" + HTTPEncoding.CR
0774: + HTTPEncoding.LF + "Cache-Control: m_",
0775: "upto-age=0" + HTTPEncoding.CR + HTTPEncoding.LF
0776: + HTTPEncoding.CR + HTTPEncoding.LF };
0777: for (int i = 0; i < request.length; i++) {
0778: final String s = request[i];
0779: doParse(s);
0780: }
0781:
0782: errorHandlerControl.verify();
0783: handlerControl.verify();
0784: }
0785:
0786: public void testParseMultiBufferNormalBody() throws Exception {
0787: handler.newMessage();
0788: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0789: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0790: handler.foundStartLineThirdField(NioUtil
0791: .toByteBuffer("HTTP/1.0"));
0792: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0793: handler
0794: .foundHeaderValue(NioUtil
0795: .toByteBuffer("localhost:8080"));
0796: handler.getBodyType();
0797: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NORMAL);
0798: handler.getBodySize();
0799: handlerControl.setReturnValue(5);
0800: handler.foundBody(NioUtil.toByteBuffer("1"));
0801: handler.foundBody(NioUtil.toByteBuffer("2"));
0802: handler.foundBody(NioUtil.toByteBuffer("3"));
0803: handler.foundBody(NioUtil.toByteBuffer("45"));
0804: handler.endMessage();
0805: handlerControl.replay();
0806:
0807: errorHandlerControl.replay();
0808:
0809: final String[] request = new String[] {
0810: "GET / HTTP/1.0" + HTTPEncoding.CR + HTTPEncoding.LF
0811: + "Host: localhost:8080" + HTTPEncoding.CR
0812: + HTTPEncoding.LF + HTTPEncoding.CR
0813: + HTTPEncoding.LF +
0814:
0815: // body
0816: "1", "2", "3", "45" };
0817: for (int i = 0; i < request.length; i++) {
0818: final String s = request[i];
0819: doParse(s);
0820: }
0821:
0822: errorHandlerControl.verify();
0823: handlerControl.verify();
0824: }
0825:
0826: // ----------------------------------------------------------------------
0827: // Bad Request Handling Tests
0828: // ----------------------------------------------------------------------
0829:
0830: public void testControlCharacterInFirstFieldThrowsException()
0831: throws Exception {
0832: handler.newMessage();
0833: handlerControl.replay();
0834:
0835: final HTTPException ex = new HTTPException(
0836: HTTPEncoding.STATUS_400_Bad_Request);
0837: errorHandler.exceptionOccurred(ex);
0838: errorHandlerControl.setThrowable(ex);
0839: errorHandlerControl.replay();
0840:
0841: final String request = "GE" + Iso646.BELL + "T / HTTP/1.0"
0842: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0843: + HTTPEncoding.LF;
0844:
0845: try {
0846: doParse(request);
0847: fail("Expected an exception!");
0848: } catch (HTTPException th) {
0849: }
0850:
0851: errorHandlerControl.verify();
0852: handlerControl.verify();
0853: }
0854:
0855: public void testControlCharacterInSecondFieldThrowsException()
0856: throws Exception {
0857: handler.newMessage();
0858: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0859: handlerControl.replay();
0860:
0861: final HTTPException ex = new HTTPException(
0862: HTTPEncoding.STATUS_400_Bad_Request);
0863: errorHandler.exceptionOccurred(ex);
0864: errorHandlerControl.setThrowable(ex);
0865: errorHandlerControl.replay();
0866:
0867: final String request = "GET /" + Iso646.BELL + "bla HTTP/1.0"
0868: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0869: + HTTPEncoding.LF;
0870:
0871: try {
0872: doParse(request);
0873: fail("Expected an exception!");
0874: } catch (HTTPException th) {
0875: }
0876:
0877: errorHandlerControl.verify();
0878: handlerControl.verify();
0879: }
0880:
0881: public void testControlCharacterInThirdFieldThrowsException()
0882: throws Exception {
0883: handler.newMessage();
0884: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0885: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0886: handlerControl.replay();
0887:
0888: final HTTPException ex = new HTTPException(
0889: HTTPEncoding.STATUS_400_Bad_Request);
0890: errorHandler.exceptionOccurred(ex);
0891: errorHandlerControl.setThrowable(ex);
0892: errorHandlerControl.replay();
0893:
0894: final String request = "GET / HT" + Iso646.BELL + "TP/1.0"
0895: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
0896: + HTTPEncoding.LF;
0897:
0898: try {
0899: doParse(request);
0900: fail("Expected an exception!");
0901: } catch (HTTPException th) {
0902: }
0903:
0904: errorHandlerControl.verify();
0905: handlerControl.verify();
0906: }
0907:
0908: public void testMissingSecondFieldThrowsException()
0909: throws Exception {
0910: handler.newMessage();
0911: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0912: handlerControl.replay();
0913:
0914: final HTTPException ex = new HTTPException(
0915: HTTPEncoding.STATUS_400_Bad_Request);
0916: errorHandler.exceptionOccurred(ex);
0917: errorHandlerControl.setThrowable(ex);
0918: errorHandlerControl.replay();
0919:
0920: final String request = "GET " + HTTPEncoding.CR
0921: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF;
0922:
0923: try {
0924: doParse(request);
0925: fail("Expected an exception!");
0926: } catch (HTTPException th) {
0927: }
0928:
0929: errorHandlerControl.verify();
0930: handlerControl.verify();
0931: }
0932:
0933: public void testControlCharacterInHeaderNameThrowsException()
0934: throws Exception {
0935: handler.newMessage();
0936: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0937: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0938: handler.foundStartLineThirdField(NioUtil
0939: .toByteBuffer("HTTP/1.0"));
0940: handlerControl.replay();
0941:
0942: final HTTPException ex = new HTTPException(
0943: HTTPEncoding.STATUS_400_Bad_Request);
0944: errorHandler.exceptionOccurred(ex);
0945: errorHandlerControl.setThrowable(ex);
0946: errorHandlerControl.replay();
0947:
0948: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0949: + HTTPEncoding.LF + "Ho" + Iso646.CANCEL
0950: + "st: localhost" + HTTPEncoding.CR + HTTPEncoding.LF;
0951:
0952: try {
0953: doParse(request);
0954: fail("Expected an exception!");
0955: } catch (HTTPException th) {
0956: }
0957:
0958: errorHandlerControl.verify();
0959: handlerControl.verify();
0960: }
0961:
0962: public void testControlCharacterInHeaderValueThrowsException()
0963: throws Exception {
0964: handler.newMessage();
0965: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0966: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0967: handler.foundStartLineThirdField(NioUtil
0968: .toByteBuffer("HTTP/1.0"));
0969: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0970: handlerControl.replay();
0971:
0972: final HTTPException ex = new HTTPException(
0973: HTTPEncoding.STATUS_400_Bad_Request);
0974: errorHandler.exceptionOccurred(ex);
0975: errorHandlerControl.setThrowable(ex);
0976: errorHandlerControl.replay();
0977:
0978: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
0979: + HTTPEncoding.LF + "Host: local" + Iso646.BACKSPACE
0980: + "host" + HTTPEncoding.CR + HTTPEncoding.LF;
0981:
0982: try {
0983: doParse(request);
0984: fail("Expected an exception!");
0985: } catch (HTTPException th) {
0986: }
0987:
0988: errorHandlerControl.verify();
0989: handlerControl.verify();
0990: }
0991:
0992: public void testNegativeBodySizeThrowsException() throws Exception {
0993: handler.newMessage();
0994: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
0995: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
0996: handler.foundStartLineThirdField(NioUtil
0997: .toByteBuffer("HTTP/1.0"));
0998: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
0999: handler.foundHeaderValue(NioUtil.toByteBuffer("localhost"));
1000: handler.getBodyType();
1001: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_NORMAL);
1002: handler.getBodySize();
1003: handlerControl.setReturnValue(-10);
1004: handlerControl.replay();
1005:
1006: final HTTPException ex = new HTTPException(
1007: HTTPEncoding.STATUS_411_Length_Required);
1008: errorHandler.exceptionOccurred(ex);
1009: errorHandlerControl.setThrowable(ex);
1010: errorHandlerControl.replay();
1011:
1012: final String request = "GET / HTTP/1.0" + HTTPEncoding.CR
1013: + HTTPEncoding.LF + "Host: localhost" + HTTPEncoding.CR
1014: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF
1015: + "body!";
1016:
1017: try {
1018: doParse(request);
1019: fail("Expected an exception!");
1020: } catch (HTTPException th) {
1021: }
1022:
1023: errorHandlerControl.verify();
1024: handlerControl.verify();
1025: }
1026:
1027: public void testHugeHeadersThrowsException() throws Exception {
1028: handlerControl.replay();
1029:
1030: final HTTPException ex = new HTTPException(
1031: HTTPEncoding.STATUS_413_Request_Entity_Too_Large);
1032: errorHandler.exceptionOccurred(ex);
1033: errorHandlerControl.setThrowable(ex);
1034: errorHandlerControl.replay();
1035:
1036: String request = "GET / HTTP/1.0" + HTTPEncoding.CR
1037: + HTTPEncoding.LF + "Host: localhost";
1038: int garabageSize = 10005;
1039: final byte[] garbage = new byte[garabageSize];
1040: for (int i = 0; i < garbage.length; i++)
1041: garbage[i] = Iso646.LATIN_CAPITAL_LETTER_A;
1042:
1043: request += new String(garbage);
1044:
1045: try {
1046: doParse(request);
1047: fail("Expected an exception!");
1048: } catch (HTTPException th) {
1049: }
1050:
1051: errorHandlerControl.verify();
1052: handlerControl.verify();
1053: }
1054:
1055: // ----------------------------------------------------------------------
1056: // Chunked transfer-coding tests
1057: // ----------------------------------------------------------------------
1058:
1059: public void testBasicChunkEncodedRequest() throws Exception {
1060: String chunk1 = "test";
1061: String chunk2 = "test" + HTTPEncoding.CR + HTTPEncoding.LF
1062: + HTTPEncoding.CR + HTTPEncoding.LF;
1063: String chunk3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1064: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1065: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1066: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1067:
1068: String request = "GET / HTTP/1.1" + HTTPEncoding.CR
1069: + HTTPEncoding.LF + "Host: localhost" + HTTPEncoding.CR
1070: + HTTPEncoding.LF + "Transfer-encoding: chunked"
1071: + HTTPEncoding.CR + HTTPEncoding.LF + HTTPEncoding.CR
1072: + HTTPEncoding.LF
1073: + Integer.toHexString(chunk1.length())
1074: + HTTPEncoding.CR + HTTPEncoding.LF + chunk1
1075: + HTTPEncoding.CR + HTTPEncoding.LF
1076: + Integer.toHexString(chunk2.length())
1077: + HTTPEncoding.CR + HTTPEncoding.LF + chunk2
1078: + HTTPEncoding.CR + HTTPEncoding.LF
1079: + Integer.toHexString(chunk3.length())
1080: + HTTPEncoding.CR + HTTPEncoding.LF + chunk3
1081: + HTTPEncoding.CR + HTTPEncoding.LF + "0"
1082: + HTTPEncoding.CR + HTTPEncoding.LF;
1083:
1084: handler.newMessage();
1085: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
1086: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
1087: handler.foundStartLineThirdField(NioUtil
1088: .toByteBuffer("HTTP/1.1"));
1089: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
1090: handler.foundHeaderValue(NioUtil.toByteBuffer("localhost"));
1091: handler.foundHeaderName(NioUtil
1092: .toByteBuffer("Transfer-encoding"));
1093: handler.foundHeaderValue(NioUtil.toByteBuffer("chunked"));
1094: handler.getBodyType();
1095: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_CHUNKING);
1096: handler.foundBody(NioUtil.toByteBuffer(chunk1));
1097: handler.foundBody(NioUtil.toByteBuffer(chunk2));
1098: handler.foundBody(NioUtil.toByteBuffer(chunk3));
1099: handler.hasTrailers();
1100: handlerControl.setReturnValue(false);
1101: handler.endMessage();
1102:
1103: handlerControl.replay();
1104: errorHandlerControl.replay();
1105:
1106: doParse(request);
1107:
1108: errorHandlerControl.verify();
1109: handlerControl.verify();
1110: }
1111:
1112: public void testMultiBufferChunkEncodedRequest() throws Exception {
1113: String chunk1 = "test";
1114: String chunk2 = "test" + HTTPEncoding.CR + HTTPEncoding.LF
1115: + HTTPEncoding.CR + HTTPEncoding.LF;
1116: String chunk3firsthalf = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1117: String chunk3secondhalf = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1118: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1119: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1120:
1121: handler.newMessage();
1122:
1123: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
1124: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
1125: handler.foundStartLineThirdField(NioUtil
1126: .toByteBuffer("HTTP/1.1"));
1127:
1128: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
1129: handler
1130: .foundHeaderValue(NioUtil
1131: .toByteBuffer("localhost:8080"));
1132: handler.foundHeaderName(NioUtil.toByteBuffer("User-Agent"));
1133: handler
1134: .foundHeaderValue(NioUtil
1135: .toByteBuffer("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"));
1136: handler.foundHeaderName(NioUtil.toByteBuffer("Accept"));
1137: handler
1138: .foundHeaderValue(NioUtil
1139: .toByteBuffer("text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"));
1140: handler
1141: .foundHeaderName(NioUtil
1142: .toByteBuffer("Accept-Language"));
1143: handler.foundHeaderValue(NioUtil
1144: .toByteBuffer(" en-us,en;q=0.5"));
1145: handler
1146: .foundHeaderName(NioUtil
1147: .toByteBuffer("Accept-Encoding"));
1148: handler.foundHeaderValue(NioUtil.toByteBuffer("gzip,deflate"));
1149: handler.foundHeaderName(NioUtil.toByteBuffer("Accept-Charset"));
1150: handler.foundHeaderValue(NioUtil
1151: .toByteBuffer("ISO-8859-1,utf-8;q=0.7,*;q=0.7"));
1152: handler.foundHeaderName(NioUtil.toByteBuffer("Keep-Alive"));
1153: handler.foundHeaderValue(NioUtil.toByteBuffer("300"));
1154: handler.foundHeaderName(NioUtil.toByteBuffer("Connection"));
1155: handler.foundHeaderValue(NioUtil.toByteBuffer("keep-alive"));
1156: handler.foundHeaderName(NioUtil.toByteBuffer("Cache-Control"));
1157: handler.foundHeaderValue(NioUtil.toByteBuffer("m_upto-age=0"));
1158: handler.foundHeaderName(NioUtil
1159: .toByteBuffer("Transfer-encoding"));
1160: handler.foundHeaderValue(NioUtil.toByteBuffer("chunked"));
1161:
1162: handler.getBodyType();
1163: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_CHUNKING);
1164:
1165: handler.foundBody(NioUtil.toByteBuffer(chunk1));
1166: handler.foundBody(NioUtil.toByteBuffer(chunk2));
1167: handler.foundBody(NioUtil.toByteBuffer(chunk3firsthalf));
1168: handler.foundBody(NioUtil.toByteBuffer(chunk3secondhalf));
1169: handler.hasTrailers();
1170: handlerControl.setReturnValue(false);
1171:
1172: handler.endMessage();
1173: handlerControl.replay();
1174:
1175: errorHandlerControl.replay();
1176:
1177: final String[] request = new String[] {
1178: "GE",
1179: "T /",
1180: " HTT",
1181: "P",
1182: "/1.1",
1183: "" + HTTPEncoding.CR,
1184: "" + HTTPEncoding.LF,
1185: "Host: localhost:8080"
1186: + HTTPEncoding.CR
1187: + HTTPEncoding.LF
1188: + "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624"
1189: + HTTPEncoding.CR + HTTPEncoding.LF,
1190: "Acc",
1191: "ept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"
1192: + HTTPEncoding.CR
1193: + HTTPEncoding.LF
1194: + "Accept-Language:\t ",
1195: "",
1196: "" + HTTPEncoding.CR + HTTPEncoding.LF,
1197: " en-us,en;q=0.5" + HTTPEncoding.CR + HTTPEncoding.LF,
1198: //"Accept-Language: ",
1199: //""+HTTPEncoding.CR + HTTPEncoding.LF,
1200: //"en-us,en;q=0.5" + HTTPEncoding.CR + HTTPEncoding.LF +
1201: "Accept-Encoding: gzip,deflate"
1202: + HTTPEncoding.CR
1203: + HTTPEncoding.LF
1204: + "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
1205: + HTTPEncoding.CR + HTTPEncoding.LF
1206: + "Keep-Alive: 300" + HTTPEncoding.CR
1207: + HTTPEncoding.LF,
1208: "Connection: keep-alive" + HTTPEncoding.CR
1209: + HTTPEncoding.LF + "Cache-Control: m_",
1210: "upto-age=0"
1211: + HTTPEncoding.CR
1212: + HTTPEncoding.LF
1213: + "Transfer-encoding: chunked"
1214: + HTTPEncoding.CR
1215: + HTTPEncoding.LF
1216: + HTTPEncoding.CR
1217: + HTTPEncoding.LF
1218: + Integer.toHexString(chunk1.length())
1219: + HTTPEncoding.CR
1220: + HTTPEncoding.LF
1221: + chunk1
1222: + HTTPEncoding.CR
1223: + HTTPEncoding.LF
1224: + Integer.toHexString(chunk2.length())
1225: + HTTPEncoding.CR
1226: + HTTPEncoding.LF
1227: + chunk2
1228: + HTTPEncoding.CR
1229: + HTTPEncoding.LF
1230: + Integer.toHexString(chunk3firsthalf.length()
1231: + chunk3secondhalf.length())
1232: + HTTPEncoding.CR + HTTPEncoding.LF
1233: + chunk3firsthalf,
1234: chunk3secondhalf + HTTPEncoding.CR + HTTPEncoding.LF
1235: + "0" + HTTPEncoding.CR + HTTPEncoding.LF };
1236: for (int i = 0; i < request.length; i++) {
1237: final String s = request[i];
1238: doParse(s);
1239: }
1240:
1241: errorHandlerControl.verify();
1242: handlerControl.verify();
1243: }
1244:
1245: public void testChunkEncodedRequestWithTrailers() throws Exception {
1246: String chunk1 = "test";
1247: String chunk2 = "test" + HTTPEncoding.CR + HTTPEncoding.LF
1248: + HTTPEncoding.CR + HTTPEncoding.LF;
1249: String chunk3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1250: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1251: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1252: + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
1253:
1254: String request = "GET / HTTP/1.1" + HTTPEncoding.CR
1255: + HTTPEncoding.LF + "Host: localhost" + HTTPEncoding.CR
1256: + HTTPEncoding.LF + "Transfer-encoding: chunked"
1257: + HTTPEncoding.CR + HTTPEncoding.LF
1258: + "Trailer: Content-Type" + HTTPEncoding.CR
1259: + HTTPEncoding.LF + HTTPEncoding.CR + HTTPEncoding.LF
1260: + Integer.toHexString(chunk1.length())
1261: + HTTPEncoding.CR + HTTPEncoding.LF + chunk1
1262: + HTTPEncoding.CR + HTTPEncoding.LF
1263: + Integer.toHexString(chunk2.length())
1264: + HTTPEncoding.CR + HTTPEncoding.LF + chunk2
1265: + HTTPEncoding.CR + HTTPEncoding.LF
1266: + Integer.toHexString(chunk3.length())
1267: + HTTPEncoding.CR + HTTPEncoding.LF + chunk3
1268: + HTTPEncoding.CR + HTTPEncoding.LF + "0"
1269: + HTTPEncoding.CR + HTTPEncoding.LF
1270: + "Content-Type: text/html" + HTTPEncoding.CR
1271: + HTTPEncoding.LF;
1272:
1273: handler.newMessage();
1274: handler.foundStartLineFirstField(NioUtil.toByteBuffer("GET"));
1275: handler.foundStartLineSecondField(NioUtil.toByteBuffer("/"));
1276: handler.foundStartLineThirdField(NioUtil
1277: .toByteBuffer("HTTP/1.1"));
1278: handler.foundHeaderName(NioUtil.toByteBuffer("Host"));
1279: handler.foundHeaderValue(NioUtil.toByteBuffer("localhost"));
1280: handler.foundHeaderName(NioUtil
1281: .toByteBuffer("Transfer-encoding"));
1282: handler.foundHeaderValue(NioUtil.toByteBuffer("chunked"));
1283: handler.foundHeaderName(NioUtil.toByteBuffer("Trailer"));
1284: handler.foundHeaderValue(NioUtil.toByteBuffer("Content-Type"));
1285: handler.getBodyType();
1286: handlerControl.setReturnValue(HTTPParser.BODY_TYPE_CHUNKING);
1287: handler.foundBody(NioUtil.toByteBuffer(chunk1));
1288: handler.foundBody(NioUtil.toByteBuffer(chunk2));
1289: handler.foundBody(NioUtil.toByteBuffer(chunk3));
1290: handler.hasTrailers();
1291: handlerControl.setReturnValue(true);
1292: handler.getTrailerNames();
1293: handlerControl.setReturnValue(new String[] { "Content-Type" });
1294: handler.foundTrailerName(NioUtil.toByteBuffer("Content-Type"));
1295: handler.foundTrailerValue(NioUtil.toByteBuffer("text/html"));
1296: handler.endMessage();
1297:
1298: handlerControl.replay();
1299: errorHandlerControl.replay();
1300:
1301: doParse(request);
1302:
1303: errorHandlerControl.verify();
1304: handlerControl.verify();
1305: }
1306:
1307: // ----------------------------------------------------------------------
1308: // Helper Methods
1309: // ----------------------------------------------------------------------
1310:
1311: protected ByteBuffer createBuffer(final String request)
1312: throws Exception {
1313: stream = new StringBufferInputStream(request);
1314: channel = Channels.newChannel(stream);
1315:
1316: final ByteBuffer buf = ByteBuffer.allocate(request.length());
1317:
1318: final int read = channel.read(buf);
1319: buf.limit(read);
1320: buf.rewind();
1321:
1322: return buf;
1323: }
1324:
1325: protected void doParse(final String request) throws Exception {
1326: parser.parse(createBuffer(request));
1327: }
1328:
1329: private final static int BUFFER_SIZE = 2 ^ 14;
1330:
1331: protected void doParseFile(final String fileName) throws Exception {
1332: final File f = new File("");
1333: final FileInputStream stream = new FileInputStream(f
1334: .getAbsolutePath()
1335: + "/src/test/com/leosimons/http/test/request.log");
1336: final FileChannel streamChannel = stream.getChannel();
1337:
1338: ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
1339: int read = 0;
1340:
1341: while ((read = streamChannel.read(buf)) != -1) {
1342: buf.rewind();
1343: parser.parse(buf, read);
1344: buf = ByteBuffer.allocate(BUFFER_SIZE);
1345: }
1346: }
1347: }
|