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.responders.files;
004:
005: import fitnesse.*;
006: import fitnesse.testutil.*;
007: import fitnesse.http.*;
008: import java.util.*;
009:
010: public class FileResponderTest extends AbstractRegex {
011: private MockRequest request;
012:
013: // Example: "Tue, 02 Apr 2003 22:18:49 GMT"
014: private final String HTTP_DATE_REGEXP = "[SMTWF][a-z]{2}\\,\\s[0-9]{2}\\s[JFMASOND][a-z]{2}\\s[0-9]{4}\\s[0-9]{2}\\:[0-9]{2}\\:[0-9]{2}\\sGMT";
015:
016: private Response response;
017:
018: private FitNesseContext context;
019:
020: private FileResponder responder;
021:
022: public void setUp() throws Exception {
023: request = new MockRequest();
024: context = new FitNesseContext();
025: context.rootPagePath = SampleFileUtility.TEMP_TEST_DIR;
026: SampleFileUtility.makeSampleFiles();
027: response = null;
028: }
029:
030: public void tearDown() throws Exception {
031: if (response != null)
032: response.readyToSend(new MockResponseSender());
033: SampleFileUtility.deleteSampleFiles();
034:
035: }
036:
037: public void testFileContent() throws Exception {
038: request.setResource("files/testFile1");
039: responder = (FileResponder) FileResponder.makeResponder(
040: request, SampleFileUtility.TEMP_TEST_DIR);
041: response = responder.makeResponse(context, request);
042: assertEquals(InputStreamResponse.class, response.getClass());
043: String responseString = new MockResponseSender(response)
044: .sentData();
045: assertSubString("file1 content", responseString);
046: }
047:
048: public void testSpacesInFileName() throws Exception {
049: String restoredPath = FileResponder
050: .restoreRealSpacesInFileName("files/test%20File%20With%20Spaces%20In%20Name");
051: assertEquals("files/test File With Spaces In Name",
052: restoredPath);
053:
054: request.setResource("files/file4%20with%20spaces.txt");
055: responder = (FileResponder) FileResponder.makeResponder(
056: request, SampleFileUtility.TEMP_TEST_DIR);
057: assertEquals("files/file4 with spaces.txt", responder.resource);
058: }
059:
060: public void testLastModifiedHeader() throws Exception {
061: request.setResource("files/testFile1");
062: responder = (FileResponder) FileResponder.makeResponder(
063: request, SampleFileUtility.TEMP_TEST_DIR);
064: response = responder.makeResponse(context, request);
065: String lastModifiedHeader = response.getHeader("Last-Modified");
066: assertMatches(HTTP_DATE_REGEXP, lastModifiedHeader);
067: }
068:
069: public void test304IfNotModified() throws Exception {
070: Calendar now = new GregorianCalendar();
071: now.add(Calendar.DATE, -1);
072: String yesterday = Response.makeStandardHttpDateFormat()
073: .format(now.getTime());
074: now.add(Calendar.DATE, 2);
075: String tomorrow = Response.makeStandardHttpDateFormat().format(
076: now.getTime());
077:
078: request.setResource("files/testFile1");
079: request.addHeader("If-Modified-Since", yesterday);
080: responder = (FileResponder) FileResponder.makeResponder(
081: request, SampleFileUtility.TEMP_TEST_DIR);
082: response = responder.makeResponse(context, request);
083: assertEquals(200, response.getStatus());
084:
085: request.setResource("files/testFile1");
086: request.addHeader("If-Modified-Since", tomorrow);
087: responder = (FileResponder) FileResponder.makeResponder(
088: request, SampleFileUtility.TEMP_TEST_DIR);
089: responder = (FileResponder) FileResponder.makeResponder(
090: request, SampleFileUtility.TEMP_TEST_DIR);
091: SimpleResponse notModifiedResponse = (SimpleResponse) responder
092: .makeResponse(context, request);
093: assertEquals(304, notModifiedResponse.getStatus());
094: assertEquals("", notModifiedResponse.getContent());
095: assertMatches(HTTP_DATE_REGEXP, notModifiedResponse
096: .getHeader("Date"));
097: assertNotNull(notModifiedResponse.getHeader("Cache-Control"));
098: }
099:
100: public void testNotFoundFile() throws Exception {
101: request.setResource("files/something/that/aint/there");
102: Responder notFoundResponder = FileResponder.makeResponder(
103: request, SampleFileUtility.TEMP_TEST_DIR);
104: SimpleResponse response = (SimpleResponse) notFoundResponder
105: .makeResponse(context, request);
106: assertEquals(404, response.getStatus());
107: assertHasRegexp("files/something/that/aint/there", response
108: .getContent());
109: }
110:
111: public void testCssMimeType() throws Exception {
112: SampleFileUtility.addFile("/files/fitnesse.css",
113: "body{color: red;}");
114: request.setResource("files/fitnesse.css");
115: responder = (FileResponder) FileResponder.makeResponder(
116: request, SampleFileUtility.TEMP_TEST_DIR);
117: response = responder.makeResponse(context, request);
118: assertEquals("text/css", response.getContentType());
119: }
120:
121: }
|