1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.test;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import org.eclipse.jetty.http.HttpSchemes;
import org.eclipse.jetty.test.support.TestableJettyServer;
import org.eclipse.jetty.test.support.rawhttp.HttpRequestTester;
import org.eclipse.jetty.test.support.rawhttp.HttpResponseTester;
import org.eclipse.jetty.test.support.rawhttp.HttpSocketImpl;
import org.eclipse.jetty.test.support.rawhttp.HttpTesting;
import org.eclipse.jetty.util.IO;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* Tests against the facilities within the TestSuite to ensure that the various
* org.eclipse.jetty.test.support.* classes do what they are supposed to.
*/
public class DefaultHandlerTest
{
private boolean debug = false;
private static TestableJettyServer server;
private int serverPort;
@BeforeClass
public static void setUpServer() throws Exception
{
server = new TestableJettyServer();
server.setScheme(HttpSchemes.HTTP);
server.addConfiguration("DefaultHandler.xml");
server.load();
server.start();
}
@Before
public void testInit() {
serverPort = server.getServerPort();
}
@AfterClass
public static void tearDownServer() throws Exception
{
server.stop();
}
@Test
public void testGET_URL() throws Exception
{
URL url = new URL("http://localhost:" + serverPort + "/tests/alpha.txt");
URLConnection conn = url.openConnection();
conn.connect();
InputStream in = conn.getInputStream();
String response = IO.toString(in);
String expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
Assert.assertEquals("Response",expected,response);
}
@Test
public void testGET_Raw() throws Exception
{
StringBuffer rawRequest = new StringBuffer();
rawRequest.append("GET /tests/alpha.txt HTTP/1.1\r\n");
rawRequest.append("Host: localhost\r\n");
rawRequest.append("Connection: close\r\n");
rawRequest.append("\r\n");
Socket sock = new Socket(InetAddress.getLocalHost(),serverPort);
sock.setSoTimeout(5000); // 5 second timeout;
DEBUG("--raw-request--\n" + rawRequest);
InputStream in = new ByteArrayInputStream(rawRequest.toString().getBytes());
// Send request
IO.copy(in,sock.getOutputStream());
// Collect response
String rawResponse = IO.toString(sock.getInputStream());
DEBUG("--raw-response--\n" + rawResponse);
HttpResponseTester response = new HttpResponseTester();
response.parse(rawResponse);
response.assertStatusOK();
response.assertBody("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
}
@Test
public void testMultiGET_Raw() throws Exception
{
StringBuffer rawRequests = new StringBuffer();
rawRequests.append("GET /tests/alpha.txt HTTP/1.1\r\n");
rawRequests.append("Host: localhost\r\n");
rawRequests.append("\r\n");
rawRequests.append("GET /tests/R1.txt HTTP/1.1\r\n");
rawRequests.append("Host: localhost\r\n");
rawRequests.append("\r\n");
rawRequests.append("GET /tests/R1.txt HTTP/1.1\r\n");
rawRequests.append("Host: localhost\r\n");
rawRequests.append("Connection: close\r\n");
rawRequests.append("\r\n");
HttpTesting http = new HttpTesting(new HttpSocketImpl(),serverPort);
List<HttpResponseTester> responses = http.requests(rawRequests);
HttpResponseTester response = responses.get(0);
response.assertStatusOK();
response.assertBody("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
response = responses.get(1);
response.assertStatusOK();
response.assertBody("Host=Default\nResource=R1\n");
response = responses.get(2);
response.assertStatusOK();
response.assertBody("Host=Default\nResource=R1\n");
}
@Test
public void testGET_HttpTesting() throws Exception
{
HttpRequestTester request = new HttpRequestTester();
request.setMethod("GET");
request.setURI("/tests/alpha.txt");
request.addHeader("Host","localhost");
request.addHeader("Connection","close");
// request.setContent(null);
HttpTesting testing = new HttpTesting(new HttpSocketImpl(),serverPort);
HttpResponseTester response = testing.request(request);
response.assertStatusOK();
response.assertContentType("text/plain");
response.assertBody("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
}
private void DEBUG(String msg)
{
if (debug)
{
System.out.println(msg);
}
}
}
|