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 fitnesse.testutil.*;
006: import fitnesse.http.*;
007: import fitnesse.wiki.InMemoryPage;
008: import fitnesse.responders.*;
009: import fitnesse.authentication.Authenticator;
010: import java.io.*;
011:
012: public class FitNesseExpediterTest extends AbstractRegex {
013: private FitNesseExpediter expediter;
014:
015: private MockSocket socket;
016:
017: private FitNesseContext context;
018:
019: private InMemoryPage root;
020:
021: PipedInputStream clientInput;
022:
023: private PipedOutputStream clientOutput;
024:
025: ResponseParser response;
026:
027: public void setUp() throws Exception {
028: root = (InMemoryPage) InMemoryPage.makeRoot("RooT");
029: root.addChildPage("FrontPage");
030: socket = new MockSocket();
031: context = new FitNesseContext(root);
032: context.responderFactory = new ResponderFactory(".");
033: expediter = new FitNesseExpediter(socket, context);
034: }
035:
036: public void tearDown() throws Exception {
037: }
038:
039: public void testAuthenticationGetsCalled() throws Exception {
040: context.authenticator = new StoneWallAuthenticator();
041: MockRequest request = new MockRequest();
042: Response response = expediter.createGoodResponse(request);
043: assertEquals(401, response.getStatus());
044: }
045:
046: public void testClosedSocketMidResponse() throws Exception {
047: try {
048: MockRequest request = new MockRequest();
049: Response response = expediter.createGoodResponse(request);
050: socket.close();
051: response.readyToSend(expediter);
052: } catch (IOException e) {
053: fail("no IOException should be thrown");
054: }
055: }
056:
057: public void testIncompleteRequestsTimeOut() throws Exception {
058: final FitNesseExpediter sender = preparePipedFitNesseExpediter();
059:
060: Thread senderThread = makeSendingThread(sender);
061: senderThread.start();
062: Thread parseResponseThread = makeParsingThread();
063: parseResponseThread.start();
064: Thread.sleep(sender.requestParsingTimeLimit + 100);
065:
066: parseResponseThread.join();
067:
068: assertEquals(408, response.getStatus());
069: }
070:
071: private FitNesseExpediter preparePipedFitNesseExpediter()
072: throws Exception {
073: PipedInputStream socketInput = new PipedInputStream();
074: clientOutput = new PipedOutputStream(socketInput);
075: clientInput = new PipedInputStream();
076: PipedOutputStream socketOutput = new PipedOutputStream(
077: clientInput);
078: MockSocket socket = new MockSocket(socketInput, socketOutput);
079: final FitNesseExpediter sender = new FitNesseExpediter(socket,
080: context);
081: sender.requestParsingTimeLimit = 200;
082: return sender;
083: }
084:
085: public void testCompleteRequest() throws Exception {
086: final FitNesseExpediter sender = preparePipedFitNesseExpediter();
087:
088: Thread senderThread = makeSendingThread(sender);
089: senderThread.start();
090: Thread parseResponseThread = makeParsingThread();
091: parseResponseThread.start();
092:
093: clientOutput.write("GET /root HTTP/1.1\r\n\r\n".getBytes());
094: clientOutput.flush();
095:
096: parseResponseThread.join();
097:
098: assertEquals(200, response.getStatus());
099: }
100:
101: public void testSlowButCompleteRequest() throws Exception {
102: final FitNesseExpediter sender = preparePipedFitNesseExpediter();
103:
104: Thread senderThread = makeSendingThread(sender);
105: senderThread.start();
106: Thread parseResponseThread = makeParsingThread();
107: parseResponseThread.start();
108:
109: byte[] bytes = "GET /root HTTP/1.1\r\n\r\n".getBytes();
110: try {
111: for (int i = 0; i < bytes.length; i++) {
112: byte aByte = bytes[i];
113: clientOutput.write(aByte);
114: clientOutput.flush();
115: Thread.sleep(20);
116: }
117: } catch (IOException pipedClosed) {
118: }
119:
120: parseResponseThread.join();
121:
122: assertEquals(200, response.getStatus());
123: }
124:
125: private Thread makeSendingThread(final FitNesseExpediter sender) {
126: Thread senderThread = new Thread(new Runnable() {
127: public void run() {
128: try {
129: sender.start();
130: } catch (Exception e) {
131: e.printStackTrace();
132: }
133: }
134: });
135: return senderThread;
136: }
137:
138: private Thread makeParsingThread() {
139: Thread parseResponseThread = new Thread(new Runnable() {
140: public void run() {
141: try {
142: response = new ResponseParser(clientInput);
143: } catch (Exception e) {
144: e.printStackTrace();
145: }
146: }
147: });
148: return parseResponseThread;
149: }
150:
151: class StoneWallAuthenticator extends Authenticator {
152: public Responder authenticate(FitNesseContext context,
153: Request request, Responder privilegedResponder)
154: throws Exception {
155: return new UnauthorizedResponder();
156: }
157:
158: public boolean isAuthenticated(String username, String password) {
159: return false;
160: }
161: }
162:
163: }
|