001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.http;
004:
005: import fitnesse.testutil.AbstractRegex;
006: import java.net.Socket;
007:
008: public class ChunkedResponseTest extends AbstractRegex implements
009: ResponseSender {
010: private ChunkedResponse response;
011:
012: private boolean closed = false;
013:
014: public StringBuffer buffer;
015:
016: public void send(byte[] bytes) throws Exception {
017: buffer.append(new String(bytes, "UTF-8"));
018: }
019:
020: public void close() {
021: closed = true;
022: }
023:
024: public Socket getSocket() throws Exception {
025: return null;
026: }
027:
028: public void setUp() throws Exception {
029: buffer = new StringBuffer();
030:
031: response = new ChunkedResponse();
032: response.readyToSend(this );
033: }
034:
035: public void tearDown() throws Exception {
036: response.closeAll();
037: }
038:
039: public void testHeaders() throws Exception {
040: assertTrue(response.isReadyToSend());
041: String text = buffer.toString();
042: assertHasRegexp("Transfer-Encoding: chunked", text);
043: assertTrue(text.startsWith("HTTP/1.1 200 OK\r\n"));
044: assertHasRegexp("Content-Type: text/html", text);
045: }
046:
047: public void testOneChunk() throws Exception {
048: buffer = new StringBuffer();
049: response.add("some more text");
050:
051: String text = buffer.toString();
052: assertEquals("e\r\nsome more text\r\n", text);
053: }
054:
055: public void testTwoChunks() throws Exception {
056: buffer = new StringBuffer();
057: response.add("one");
058: response.add("two");
059:
060: String text = buffer.toString();
061: assertEquals("3\r\none\r\n3\r\ntwo\r\n", text);
062: }
063:
064: public void testSimpleClosing() throws Exception {
065: assertFalse(closed);
066: buffer = new StringBuffer();
067: response.closeAll();
068: String text = buffer.toString();
069: assertEquals("0\r\n\r\n", text);
070: assertTrue(closed);
071: }
072:
073: public void testClosingInSteps() throws Exception {
074: assertFalse(closed);
075: buffer = new StringBuffer();
076: response.closeChunks();
077: assertEquals("0\r\n", buffer.toString());
078: assertFalse(closed);
079: buffer = new StringBuffer();
080: response.closeTrailer();
081: assertEquals("\r\n", buffer.toString());
082: assertFalse(closed);
083: response.close();
084: assertTrue(closed);
085: }
086:
087: public void testContentSize() throws Exception {
088: response.add("12345");
089: response.closeAll();
090: assertEquals(5, response.getContentSize());
091: }
092:
093: public void testNoNullPointerException() throws Exception {
094: String s = null;
095: try {
096: response.add(s);
097: } catch (Exception e) {
098: fail("should not throw exception");
099: }
100: }
101:
102: public void testTrailingHeaders() throws Exception {
103: response.closeChunks();
104: buffer = new StringBuffer();
105: response.addTrailingHeader("Some-Header", "someValue");
106: assertEquals("Some-Header: someValue\r\n", buffer.toString());
107: response.closeTrailer();
108: response.close();
109: assertTrue(closed);
110: }
111:
112: public void testCantAddZeroLengthBytes() throws Exception {
113: int originalLength = buffer.length();
114: response.add("");
115: assertEquals(originalLength, buffer.length());
116: response.closeAll();
117: }
118:
119: public void testUnicodeCharacters() throws Exception {
120: response.add("\uba80\uba81\uba82\uba83");
121: response.closeAll();
122:
123: assertSubString("\uba80\uba81\uba82\uba83", buffer.toString());
124: }
125: }
|