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;
004:
005: import java.util.regex.Pattern;
006:
007: import fitnesse.components.LogData;
008: import fitnesse.http.MockRequest;
009: import fitnesse.http.SimpleResponse;
010: import fitnesse.responders.ResponderFactory;
011: import fitnesse.responders.files.SampleFileUtility;
012: import fitnesse.testutil.AbstractRegex;
013: import fitnesse.testutil.MockSocket;
014: import fitnesse.util.FileUtil;
015: import fitnesse.wiki.InMemoryPage;
016: import fitnesse.wiki.MockWikiPage;
017: import fitnesse.wiki.PageCrawler;
018: import fitnesse.wiki.PathParser;
019: import fitnesse.wiki.WikiPage;
020: import fitnesse.wiki.WikiPagePath;
021:
022: public class FitNesseServerTest extends AbstractRegex {
023: private PageCrawler crawler;
024:
025: private WikiPage root;
026:
027: private WikiPagePath pageOnePath;
028:
029: private WikiPagePath pageOneTwoPath;
030:
031: public FitNesseServerTest() {
032: }
033:
034: public void setUp() throws Exception {
035: SampleFileUtility.makeSampleFiles();
036: root = InMemoryPage.makeRoot("RootPage");
037: crawler = root.getPageCrawler();
038: pageOnePath = PathParser.parse("PageOne");
039: pageOneTwoPath = PathParser.parse("PageOne.PageTwo");
040: }
041:
042: public void tearDown() throws Exception {
043: FileUtil
044: .deleteFileSystemDirectory(SampleFileUtility.TEMP_TEST_DIR);
045: }
046:
047: public void testSimple() throws Exception {
048: crawler.addPage(root, PathParser.parse("SomePage"),
049: "some string");
050: String output = getSocketOutput(
051: "GET /SomePage HTTP/1.1\r\n\r\n", root);
052: String statusLine = "HTTP/1.1 200 OK\r\n";
053: assertTrue("Should have statusLine", Pattern.compile(
054: statusLine, Pattern.MULTILINE).matcher(output).find());
055: assertTrue("Should have canned Content", hasSubString(
056: "some string", output));
057: }
058:
059: public void testNotFound() throws Exception {
060: String output = getSocketOutput(
061: "GET /WikiWord HTTP/1.1\r\n\r\n", new MockWikiPage());
062:
063: assertSubString("404 Not Found", output);
064: }
065:
066: public void testBadRequest() throws Exception {
067: String output = getSocketOutput("Bad Request \r\n\r\n",
068: new MockWikiPage());
069:
070: assertSubString("400 Bad Request", output);
071: assertSubString(
072: "The request string is malformed and can not be parsed",
073: output);
074: }
075:
076: public void testFrontPageRequest() throws Exception {
077: crawler.addPage(root, PathParser.parse("FrontPage"),
078: "This is the FrontPage content");
079: String output = getSocketOutput("GET / HTTP/1.1\r\n\r\n", root);
080: String expected = "This is the .* content";
081: assertTrue("Should have content",
082: hasSubString(expected, output));
083: }
084:
085: public void testSomeOtherPage() throws Exception {
086: crawler.addPage(root, pageOnePath, "Page One Content");
087: String output = getSocketOutput(
088: "GET /PageOne HTTP/1.1\r\n\r\n", root);
089: String expected = "Page One Content";
090: assertTrue("Should have page one", hasSubString(expected,
091: output));
092: }
093:
094: public void testSecondLevelPage() throws Exception {
095: crawler.addPage(root, pageOnePath, "Page One Content");
096: crawler.addPage(root, pageOneTwoPath, "Page Two Content");
097: String output = getSocketOutput(
098: "GET /PageOne.PageTwo HTTP/1.1\r\n\r\n", root);
099:
100: String expected = "Page Two Content";
101: assertTrue("Should have page Two", hasSubString(expected,
102: output));
103: }
104:
105: public void testRelativeAndAbsoluteLinks() throws Exception {
106: WikiPage root = InMemoryPage.makeRoot("RootPage");
107: crawler.addPage(root, pageOnePath, "PageOne");
108: crawler.addPage(root, pageOneTwoPath, "PageTwo");
109: String output = getSocketOutput(
110: "GET /PageOne.PageTwo HTTP/1.1\r\n\r\n", root);
111: String expected = "href=\"PageOne.PageTwo\".*PageTwo";
112: assertTrue("Should have relative link", hasSubString(expected,
113: output));
114:
115: crawler.addPage(root, PathParser.parse("PageTwo"),
116: "PageTwo at root");
117: crawler.addPage(root, PathParser.parse("PageOne.PageThree"),
118: "PageThree has link to .PageTwo at the root");
119: output = getSocketOutput(
120: "GET /PageOne.PageThree HTTP/1.1\r\n\r\n", root);
121: expected = "href=\"PageTwo\".*[.]PageTwo";
122: assertTrue("Should have absolute link", hasSubString(expected,
123: output));
124: }
125:
126: // Removed since /files is now served from jar on classpath
127: public void XtestServingRegularFiles() throws Exception {
128: String output = getSocketOutput(
129: "GET /files/testDir/testFile2 HTTP/1.1\r\n\r\n",
130: new MockWikiPage());
131: assertHasRegexp("file2 content", output);
132: }
133:
134: public void testLoggingDataCreation() throws Exception {
135: MockRequest request = new MockRequest();
136: SimpleResponse response = new SimpleResponse(200);
137: MockSocket socket = new MockSocket("something");
138:
139: socket.setHost("1.2.3.4");
140: request.setRequestLine("GET / HTTP/1.1");
141: response.setContent("abc");
142:
143: LogData data = FitNesseExpediter.makeLogData(socket, request,
144: response);
145:
146: assertEquals("1.2.3.4", data.host);
147: assertNotNull(data.time);
148: assertEquals("GET / HTTP/1.1", data.requestLine);
149: assertEquals(200, data.status);
150: assertEquals(3, data.size);
151: }
152:
153: public void xtestVirtualDirectoryCreation() {
154: String output = "";
155: try {
156: output = getSocketFitnessiumOutput("GET /fitnessium/TestRunner.hta HTTP/1.1\r\n\r\n");
157: } catch (Exception e) {
158: // TODO Auto-generated catch block
159: e.printStackTrace();
160: }
161: assertHasRegexp("STIQ", output);
162: }
163:
164: private String getSocketFitnessiumOutput(String requestLine)
165: throws Exception {
166: MockSocket s = new MockSocket(requestLine);
167: FitNesseContext context = new FitNesseContext();
168: context.rootPagePath = SampleFileUtility.TEMP_TEST_DIR;
169: context.responderFactory = new ResponderFactory(
170: SampleFileUtility.TEMP_TEST_DIR);
171: FitNesseServer server = new FitNesseServer(context);
172: server.serve(s, 1000);
173: String output = s.getOutput();
174: return output;
175: }
176:
177: private String getSocketOutput(String requestLine, WikiPage page)
178: throws Exception {
179: MockSocket s = new MockSocket(requestLine);
180: FitNesseContext context = new FitNesseContext();
181: context.rootPagePath = SampleFileUtility.TEMP_TEST_DIR;
182: context.responderFactory = new ResponderFactory(
183: SampleFileUtility.TEMP_TEST_DIR);
184: context.root = page;
185: FitNesseServer server = new FitNesseServer(context);
186: server.serve(s, 1000);
187: String output = s.getOutput();
188: return output;
189: }
190:
191: private static boolean hasSubString(String expected, String output) {
192: return Pattern.compile(expected, Pattern.MULTILINE).matcher(
193: output).find();
194: }
195: }
|