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 java.io.*;
006: import java.net.Socket;
007: import fitnesse.testutil.AbstractRegex;
008: import fitnesse.util.FileUtil;
009:
010: public class InputStreamResponseTest extends AbstractRegex implements
011: ResponseSender {
012: private InputStreamResponse response;
013:
014: private boolean closed = false;
015:
016: private ByteArrayOutputStream output;
017:
018: private File testFile = new File("testFile.test");
019:
020: private long bytesSent = 0;
021:
022: public void setUp() throws Exception {
023: response = new InputStreamResponse();
024: output = new ByteArrayOutputStream();
025: }
026:
027: public void tearDown() throws Exception {
028: FileUtil.deleteFile(testFile);
029: }
030:
031: public void testSimpleUsage() throws Exception {
032: ByteArrayInputStream input = new ByteArrayInputStream("content"
033: .getBytes());
034: response.setBody(input, 7);
035: response.readyToSend(this );
036: assertTrue(closed);
037:
038: ResponseParser result = new ResponseParser(
039: new ByteArrayInputStream(output.toByteArray()));
040: assertEquals(200, result.getStatus());
041: assertEquals(7 + "", result.getHeader("Content-Length"));
042: assertEquals("content", result.getBody());
043: }
044:
045: public void testWithFile() throws Exception {
046: FileUtil.createFile(testFile, "content");
047: response.setBody(testFile);
048: response.readyToSend(this );
049: assertTrue(closed);
050:
051: ResponseParser result = new ResponseParser(
052: new ByteArrayInputStream(output.toByteArray()));
053: assertEquals(200, result.getStatus());
054: assertEquals(7 + "", result.getHeader("Content-Length"));
055: assertEquals("content", result.getBody());
056: }
057:
058: public void testWithLargeFile() throws Exception {
059: writeLinesToFile(1000);
060:
061: response.setBody(testFile);
062: response.readyToSend(this );
063: String responseString = output.toString();
064: assertSubString("Content-Length: 100000", responseString);
065: assertTrue(bytesSent > 100000);
066: }
067:
068: public void testWithLargerFile() throws Exception {
069: writeLinesToFile(100000);
070:
071: response.setBody(testFile);
072: response.readyToSend(this );
073: String responseString = output.toString();
074: assertSubString("Content-Length: 10000000", responseString);
075: assertTrue(bytesSent > 10000000);
076: }
077:
078: // Don't run unless you have some time to kill.
079: public void _testWithReallyBigFile() throws Exception {
080: writeLinesToFile(10000000);
081:
082: response.setBody(testFile);
083: response.readyToSend(this );
084: String responseString = output.toString();
085: assertSubString("Content-Length: 1000000000", responseString);
086: assertTrue(bytesSent > 1000000000);
087: }
088:
089: private void writeLinesToFile(int lines) throws IOException {
090: String sampleLine = "This is a sample line of a large file that's created for the large file tests. It's 100 bytes long.\n";
091: byte[] bytes = sampleLine.getBytes();
092: FileOutputStream testFileOutput = new FileOutputStream(testFile);
093: for (int i = 0; i < lines; i++)
094: testFileOutput.write(bytes);
095: testFileOutput.close();
096: }
097:
098: public void send(byte[] bytes) throws Exception {
099: if (bytesSent < 500)
100: output.write(bytes);
101: bytesSent += bytes.length;
102: }
103:
104: public void close() throws Exception {
105: closed = true;
106: }
107:
108: public Socket getSocket() throws Exception // TODO-MdM maybe get rid of
109: // this method.
110: {
111: return null;
112: }
113: }
|