01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.geronimo.testsuite.jetty;
19:
20: import java.io.BufferedReader;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.InputStreamReader;
24: import java.io.OutputStream;
25: import java.net.HttpURLConnection;
26: import java.net.URL;
27:
28: import org.apache.geronimo.testsupport.TestSupport;
29: import org.testng.annotations.Test;
30:
31: public class TestJetty extends TestSupport {
32: @Test
33: public void testJettyHost() throws Exception {
34: URL url = new URL("http://localhost:8080/JettyWeb/");
35: HttpURLConnection conn = (HttpURLConnection) url
36: .openConnection();
37: try {
38: String reply = doGET(conn, "testhost.com");
39:
40: assertEquals("responseCode", 200, conn.getResponseCode());
41:
42: assertTrue(reply.indexOf("Testing Jetty.") != -1);
43: } finally {
44: conn.disconnect();
45: }
46: }
47:
48: @Test
49: public void testJettyNoHost() throws Exception {
50: URL url = new URL("http://localhost:8080/JettyWeb/");
51: HttpURLConnection conn = (HttpURLConnection) url
52: .openConnection();
53: try {
54: String reply = doGET(conn, null);
55:
56: assertEquals("responseCode", 404, conn.getResponseCode());
57: } finally {
58: conn.disconnect();
59: }
60: }
61:
62: private String doGET(HttpURLConnection conn, String host)
63: throws IOException {
64: conn.setConnectTimeout(1000 * 30);
65: conn.setReadTimeout(1000 * 30);
66: conn.setDoOutput(true);
67: conn.setUseCaches(false);
68: if (host != null) {
69: conn.setRequestProperty("Host", host);
70: }
71:
72: InputStream is = null;
73:
74: try {
75: is = conn.getInputStream();
76: } catch (IOException e) {
77: is = conn.getErrorStream();
78: }
79:
80: StringBuffer buf = new StringBuffer();
81: BufferedReader in = new BufferedReader(
82: new InputStreamReader(is));
83: String inputLine;
84: while ((inputLine = in.readLine()) != null) {
85: System.out.println(inputLine);
86: buf.append(inputLine);
87: }
88: in.close();
89:
90: return buf.toString();
91: }
92:
93: }
|