001: /*
002: * $Id: HTTPTestCase.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.util.*;
023:
024: import junit.framework.*;
025:
026: /**
027: * An abstract class that provides support for testing HTTP requests
028: * by creating a temporary file, filling it with random data, issuing
029: * requests for the file, and comparing the retrieved data with the
030: * original data. Subclasses must override the package-scoped
031: * issueRequest method.
032: *
033: * @author <a href="http://www.savarese.org/">Daniel F. Savarese</a>
034: */
035: public abstract class HTTPTestCase extends TestCase {
036:
037: static final int MAX_FILE_SIZE = 1024;
038: static final String EOL = "\r\n";
039:
040: File file;
041: String docroot;
042: byte[] data;
043: Random random;
044:
045: public HTTPTestCase() throws IOException {
046: random = new Random();
047:
048: // There's no point in doing this for tests that don't need it, so
049: // we don't put it in setUp
050: file = File.createTempFile("barehttp", null);
051: docroot = file.getCanonicalFile().getParent();
052:
053: data = new byte[random.nextInt(MAX_FILE_SIZE) + 1];
054: random.nextBytes(data);
055:
056: FileOutputStream output = new FileOutputStream(file);
057: output.write(data);
058: output.close();
059:
060: file.deleteOnExit();
061: }
062:
063: abstract byte[] issueRequest(String request) throws IOException;
064:
065: void validateOutput(byte[] output, boolean skipHeaders) {
066: int skip = 0;
067:
068: if (skipHeaders) {
069: while (skip < output.length) {
070: if (output[skip++] == '\r' && skip < output.length
071: && output[skip++] == '\n'
072: && skip < output.length
073: && output[skip++] == '\r'
074: && skip < output.length
075: && output[skip++] == '\n')
076: break;
077: }
078: }
079:
080: assertEquals(
081: "Expected data length differs from output data length.",
082: data.length, output.length - skip);
083:
084: int i;
085: for (i = 0; skip < output.length && output[skip] == data[i]; ++skip, ++i)
086: ;
087:
088: assertEquals(
089: "Expected data and output data differ at data offset "
090: + i + " and output offset " + skip,
091: output.length, skip);
092: }
093:
094: /**
095: * Tests an HTTP 0.9 simple request.
096: */
097: public void testGetRequest09() throws IOException {
098: String get = "GET /" + file.getName() + EOL;
099: validateOutput(issueRequest(get), false);
100: }
101:
102: /**
103: * Tests an HTTP 1.1 GET request.
104: */
105: public void testGetRequest11() throws IOException {
106: String get = "GET /" + file.getName() + " HTTP/1.1" + EOL
107: + "Host: localhost" + EOL + EOL;
108: validateOutput(issueRequest(get), true);
109: }
110:
111: }
|