001: /*
002: * $Id: HTTPServerTest.java 6825 2006-04-04 03:00:44Z dfs $
003: *
004: * Copyright 2006 Daniel F. Savarese
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.savarese.org/software/ApacheLicense-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: package org.savarese.barehttp;
020:
021: import java.io.*;
022: import java.net.*;
023: import java.util.concurrent.TimeUnit;
024:
025: import junit.framework.*;
026:
027: /**
028: * Tests the HTTPServer class by making use of {@link HTTPTestCase}
029: * fixtures and issuing requests over a socket connection.
030: *
031: * @author <a href="http://www.savarese.org/">Daniel F. Savarese</a>
032: */
033: public class HTTPServerTest extends HTTPTestCase {
034:
035: HTTPServer server;
036:
037: public HTTPServerTest() throws IOException {
038: server = null;
039: }
040:
041: void startServer() throws IOException {
042: server = new HTTPServer(docroot, 0, 1);
043: server.start();
044: }
045:
046: void stopServer() throws IOException {
047: server.stop(0, TimeUnit.SECONDS);
048: server = null;
049: }
050:
051: byte[] issueRequest(String request) throws IOException {
052: try {
053: startServer();
054:
055: Socket socket = new Socket();
056: socket.connect(new InetSocketAddress(server
057: .getBoundAddress(), server.getBoundPort()));
058:
059: InputStream in = socket.getInputStream();
060: OutputStream out = socket.getOutputStream();
061:
062: out.write(request.getBytes("US-ASCII"));
063:
064: ByteArrayOutputStream reply = new ByteArrayOutputStream(
065: MAX_FILE_SIZE << 1);
066: byte[] buffer = new byte[MAX_FILE_SIZE << 1];
067: int len;
068:
069: // We can get away with this only because HTTPSession closes
070: // the connection. Otherwise, we have to pay attention to
071: // the content length. In fact we should write tests to test
072: // the headers are correct, but we won't be that rigorous for
073: // what amounts to a toy program.
074: do {
075: len = in.read(buffer);
076: if (len > 0)
077: reply.write(buffer, 0, len);
078: } while (len >= 0);
079:
080: socket.close();
081:
082: return reply.toByteArray();
083: } finally {
084: stopServer();
085: }
086: }
087:
088: /**
089: * Verifies constructor arguments.
090: */
091: public void testHTTPServer() {
092: HTTPServer server = new HTTPServer(docroot, 0, 1);
093: assertNull(server.getBindAddress());
094: assertEquals(0, server.getPort());
095: assertEquals(1, server.getMaxConnections());
096: assertEquals(docroot, server.getDocumentRoot());
097: }
098:
099: /**
100: * Starts a server instance and stops it.
101: */
102: public void testStop() throws IOException {
103: HTTPServer server = new HTTPServer(docroot, 0, 1);
104: server.start();
105: assertTrue(server.isRunning());
106: assertTrue(server.stop(10, TimeUnit.SECONDS));
107: }
108:
109: }
|