001: //========================================================================
002: //Copyright 2006 Mort Bay Consulting Pty. Ltd.
003: //------------------------------------------------------------------------
004: //Licensed under the Apache License, Version 2.0 (the "License");
005: //you may not use this file except in compliance with the License.
006: //You may obtain a copy of the License at
007: //http://www.apache.org/licenses/LICENSE-2.0
008: //Unless required by applicable law or agreed to in writing, software
009: //distributed under the License is distributed on an "AS IS" BASIS,
010: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: //See the License for the specific language governing permissions and
012: //limitations under the License.
013: //========================================================================
014:
015: package org.mortbay.jetty;
016:
017: import java.io.File;
018: import java.io.FileNotFoundException;
019: import java.io.IOException;
020: import java.net.HttpURLConnection;
021: import java.net.MalformedURLException;
022: import java.net.URL;
023:
024: import junit.framework.TestCase;
025:
026: import org.mortbay.jetty.Connector;
027: import org.mortbay.jetty.Handler;
028: import org.mortbay.jetty.HttpHeaders;
029: import org.mortbay.jetty.MimeTypes;
030: import org.mortbay.jetty.NCSARequestLog;
031: import org.mortbay.jetty.Server;
032: import org.mortbay.jetty.handler.ContextHandlerCollection;
033: import org.mortbay.jetty.handler.DefaultHandler;
034: import org.mortbay.jetty.handler.HandlerCollection;
035: import org.mortbay.jetty.handler.RequestLogHandler;
036: import org.mortbay.jetty.nio.SelectChannelConnector;
037: import org.mortbay.jetty.security.HashUserRealm;
038: import org.mortbay.jetty.security.UserRealm;
039: import org.mortbay.jetty.webapp.WebAppContext;
040: import org.mortbay.thread.BoundedThreadPool;
041: import org.mortbay.util.IO;
042:
043: public class WebAppTest extends TestCase {
044: Server server = new Server();
045: BoundedThreadPool threadPool = new BoundedThreadPool();
046: Connector connector = new SelectChannelConnector();
047: HandlerCollection handlers = new HandlerCollection();
048: ContextHandlerCollection contexts = new ContextHandlerCollection();
049: HashUserRealm userRealm = new HashUserRealm();
050: RequestLogHandler requestLogHandler = new RequestLogHandler();
051:
052: protected void setUp() throws Exception {
053: File dir = new File(".").getAbsoluteFile();
054: while (!new File(dir, "webapps").exists()) {
055: dir = dir.getParentFile();
056: }
057:
058: threadPool.setMaxThreads(100);
059: server.setThreadPool(threadPool);
060:
061: server.setConnectors(new Connector[] { connector });
062:
063: handlers.setHandlers(new Handler[] { contexts,
064: new DefaultHandler(), requestLogHandler });
065: server.setHandler(handlers);
066:
067: // TODO add javadoc context to contexts
068: WebAppContext.addWebApplications(server, dir.getAbsolutePath()
069: + "/webapps",
070: "org/mortbay/jetty/webapp/webdefault.xml", true, false);
071:
072: userRealm.setName("Test Realm");
073: userRealm.setConfig(dir.getAbsolutePath()
074: + "/etc/realm.properties");
075: server.setUserRealms(new UserRealm[] { userRealm });
076:
077: File file = File.createTempFile("test", ".log");
078: NCSARequestLog requestLog = new NCSARequestLog(file
079: .getAbsolutePath());
080:
081: requestLog.setExtended(false);
082: requestLogHandler.setRequestLog(requestLog);
083:
084: server.setSendServerVersion(true);
085:
086: server.start();
087: }
088:
089: protected void tearDown() throws Exception {
090: Thread.sleep(1000);
091: server.stop();
092: Thread.sleep(1000);
093: }
094:
095: public void testDoGet() throws Exception {
096: URL url = null;
097:
098: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
099: + "/test/dump/info?query=foo");
100: assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
101:
102: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
103: + "/");
104: try {
105: IO.toString(url.openStream());
106: assertTrue(false);
107: } catch (FileNotFoundException e) {
108: assertTrue(true);
109: }
110:
111: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
112: + "/test");
113: IO.toString(url.openStream());
114:
115: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
116: + "/test/");
117: String s1 = IO.toString(url.openStream());
118: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
119: + "/test/index.html");
120: String s2 = IO.toString(url.openStream());
121: assertEquals(s1, s2);
122:
123: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
124: + "/test/d.txt");
125: assertTrue(IO.toString(url.openStream()).startsWith("0000"));
126: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
127: + "/test/data.txt");
128: System.err
129: .println("9999 3333333333333333333333333333333333333333333333333333333\n");
130: String result = IO.toString(url.openStream());
131: if (result.endsWith("\r\n")) {
132: //windows
133: result = result.substring(0, result.length() - 2);
134: } else if (result.endsWith("\n")) {
135: //*nix
136: result = result.substring(0, result.length() - 1);
137: } else {
138: //Error: Unexpected end of stream data encountered
139: assertTrue(false);
140: }
141: assertTrue(result
142: .endsWith("9999 3333333333333333333333333333333333333333333333333333333"));
143:
144: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
145: + "/test/dispatch/forward/dump/info?query=foo");
146: assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
147: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
148: + "/test/dispatch/includeW/dump/info?query=foo");
149: assertTrue(IO.toString(url.openStream()).startsWith("<H1>"));
150: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
151: + "/test/dispatch/includeS/dump/info?query=foo");
152: assertTrue(IO.toString(url.openStream()).startsWith("<H1>"));
153:
154: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
155: + "/test/dump/info?continue=1000");
156: assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
157: }
158:
159: public void testDoPost() throws Exception {
160: URL url = null;
161: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
162: + "/test/dump/info?query=foo");
163: HttpURLConnection connection = (HttpURLConnection) url
164: .openConnection();
165: connection.setRequestMethod("POST");
166: connection.setDoOutput(true);
167: connection.setDoInput(true);
168: connection.addRequestProperty(HttpHeaders.CONTENT_TYPE,
169: MimeTypes.FORM_ENCODED);
170: connection.addRequestProperty(HttpHeaders.CONTENT_LENGTH, "10");
171: connection.getOutputStream().write("abcd=1234\n".getBytes());
172: connection.getOutputStream().flush();
173:
174: connection.connect();
175: String s0 = IO.toString(connection.getInputStream());
176: assertTrue(s0.startsWith("<html>"));
177: assertTrue(s0.indexOf("<td>POST</td>") > 0);
178: assertTrue(s0.indexOf("abcd: </th><td>1234") > 0);
179: }
180:
181: public void testWebInfAccess() throws Exception {
182: assertNotFound("WEB-INF/foo");
183: assertNotFound("web-inf");
184: assertNotFound("web-inf/");
185: assertNotFound("./web-inf/");
186: assertNotFound("web-inf/jetty-web.xml");
187: assertNotFound("Web-Inf/web.xml");
188: assertNotFound("./WEB-INF/web.xml");
189: assertNotFound("META-INF");
190: assertNotFound("meta-inf/manifest.mf");
191: assertNotFound("Meta-Inf/foo");
192: assertFound("index.html");
193: }
194:
195: private void assertNotFound(String resource)
196: throws MalformedURLException, IOException {
197: try {
198: getResource(resource);
199: } catch (FileNotFoundException e) {
200: return;
201: }
202: fail("Expected 404 for resource: " + resource);
203: }
204:
205: private void assertFound(String resource)
206: throws MalformedURLException, IOException {
207: try {
208: getResource(resource);
209: } catch (FileNotFoundException e) {
210: fail("Expected 200 for resource: " + resource);
211: }
212: // Pass
213: return;
214: }
215:
216: private void getResource(String resource)
217: throws MalformedURLException, IOException {
218: URL url;
219: url = new URL("http://127.0.0.1:" + connector.getLocalPort()
220: + "/test/" + resource);
221: url.openStream();
222: }
223: }
|