1
//========================================================================
2
//Copyright 2006 Mort Bay Consulting Pty. Ltd.
3
//------------------------------------------------------------------------
4
//Licensed under the Apache License, Version 2.0 (the "License");
5
//you may not use this file except in compliance with the License.
6
//You may obtain a copy of the License at
7
//http://www.apache.org/licenses/LICENSE-2.0
8
//Unless required by applicable law or agreed to in writing, software
9
//distributed under the License is distributed on an "AS IS" BASIS,
10
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
//See the License for the specific language governing permissions and
12
//limitations under the License.
13
//========================================================================
15
package org.mortbay.jetty;
18
import java.io.FileNotFoundException;
19
import java.io.IOException;
20
import java.net.HttpURLConnection;
21
import java.net.MalformedURLException;
24
import junit.framework.TestCase;
26
import org.mortbay.jetty.Connector;
27
import org.mortbay.jetty.Handler;
28
import org.mortbay.jetty.HttpHeaders;
29
import org.mortbay.jetty.MimeTypes;
30
import org.mortbay.jetty.NCSARequestLog;
31
import org.mortbay.jetty.Server;
32
import org.mortbay.jetty.handler.ContextHandlerCollection;
33
import org.mortbay.jetty.handler.DefaultHandler;
34
import org.mortbay.jetty.handler.HandlerCollection;
35
import org.mortbay.jetty.handler.RequestLogHandler;
36
import org.mortbay.jetty.nio.SelectChannelConnector;
37
import org.mortbay.jetty.security.HashUserRealm;
38
import org.mortbay.jetty.security.UserRealm;
39
import org.mortbay.jetty.webapp.WebAppContext;
40
import org.mortbay.thread.BoundedThreadPool;
41
import org.mortbay.util.IO;
43
public class WebAppTest extends TestCase
45
Server server = new Server();
46
BoundedThreadPool threadPool = new BoundedThreadPool();
47
Connector connector=new SelectChannelConnector();
48
HandlerCollection handlers = new HandlerCollection();
49
ContextHandlerCollection contexts = new ContextHandlerCollection();
50
HashUserRealm userRealm = new HashUserRealm();
51
RequestLogHandler requestLogHandler = new RequestLogHandler();
52
WebAppContext context;
54
protected void setUp() throws Exception
56
File dir = new File(".").getAbsoluteFile();
57
while (!new File(dir,"webapps").exists())
59
dir=dir.getParentFile();
62
threadPool.setMaxThreads(100);
63
server.setThreadPool(threadPool);
65
server.setConnectors(new Connector[]{connector});
67
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
68
server.setHandler(handlers);
70
context=new WebAppContext(contexts,dir.getAbsolutePath()+"/webapps/test","/test");
72
userRealm.setName("Test Realm");
73
userRealm.setConfig(dir.getAbsolutePath()+"/etc/realm.properties");
74
server.setUserRealms(new UserRealm[]{userRealm});
76
File file = File.createTempFile("test",".log");
77
NCSARequestLog requestLog = new NCSARequestLog(file.getAbsolutePath());
79
requestLog.setExtended(false);
80
requestLogHandler.setRequestLog(requestLog);
82
server.setSendServerVersion(true);
89
protected void tearDown() throws Exception
97
public void testDoGet() throws Exception
101
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?query=foo");
102
assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
104
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/");
105
try{IO.toString(url.openStream()); assertTrue(false); } catch(FileNotFoundException e) { assertTrue(true); }
107
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test");
108
IO.toString(url.openStream());
110
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/");
111
String s1=IO.toString(url.openStream());
112
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/index.html");
113
String s2=IO.toString(url.openStream());
116
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/d.txt");
117
assertTrue(IO.toString(url.openStream()).startsWith("0000"));
118
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/data.txt");
120
String result = IO.toString(url.openStream());
121
if (result.endsWith("\r\n")) {
123
result = result.substring(0,result.length() - 2);
124
} else if (result.endsWith("\n")) {
126
result = result.substring(0,result.length() - 1);
128
//Error: Unexpected end of stream data encountered
131
assertTrue(result.endsWith("9999 3333333333333333333333333333333333333333333333333333333"));
133
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dispatch/forward/dump/info?query=foo");
134
assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
136
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dispatch/includeW/dump/info?query=foo");
137
assertTrue(IO.toString(url.openStream()).startsWith("<H1>"));
139
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dispatch/includeS/dump/info?query=foo");
140
assertTrue(IO.toString(url.openStream()).startsWith("<H1>"));
142
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?continue=1000");
143
assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
146
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?lines=100");
148
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
149
connection.addRequestProperty("accept-encoding","gzip");
150
connection.connect();
151
assertEquals("gzip",connection.getHeaderField("Content-Encoding"));
156
public void testRequestListener() throws Exception
161
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info/");
162
result=IO.toString(url.openStream());
163
assertTrue(result.startsWith("<html>"));
164
assertTrue(result.indexOf("requestInitialized")>0);
165
assertTrue(result.indexOf("'/test'")>0);
169
public void testSecurity() throws Exception
176
"/WEB-INF/web.xml%00",
177
"/WEB-INF/web.xml\00",
178
"/WEB-INF/web.xml\u0000",
182
"//WEB-INF//web.xml",
186
String[] forbidden = {
198
for (int i=0;i<hidden.length;i++)
202
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+hidden[i]);
203
IO.toString(url.openStream());
206
catch(FileNotFoundException e)
208
System.err.println(e);
213
for (int i=0;i<forbidden.length;i++)
217
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+forbidden[i]);
218
IO.toString(url.openStream());
223
System.err.println(e);
224
assertTrue(e.toString().indexOf("403")>=0);
229
for (int i=0;i<ok.length;i++)
233
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+ok[i]);
234
IO.toString(url.openStream());
237
catch(FileNotFoundException e)
239
System.err.println(e);
246
public void testDoPost() throws Exception
249
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?query=foo");
250
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
251
connection.setRequestMethod("POST");
252
connection.setDoOutput(true);
253
connection.setDoInput(true);
254
connection.addRequestProperty(HttpHeaders.CONTENT_TYPE,MimeTypes.FORM_ENCODED);
255
connection.addRequestProperty(HttpHeaders.CONTENT_LENGTH, "10");
256
connection.getOutputStream().write("abcd=1234\n".getBytes());
257
connection.getOutputStream().flush();
259
connection.connect();
260
String s0=IO.toString(connection.getInputStream());
261
assertTrue(s0.startsWith("<html>"));
262
assertTrue(s0.indexOf("<td>POST</td>")>0);
263
assertTrue(s0.indexOf("abcd: </th><td>1234")>0);
267
public void testWebInfAccess() throws Exception
269
assertNotFound("WEB-INF/foo");
270
assertNotFound("web-inf");
271
assertNotFound("web-inf/");
272
assertNotFound("./web-inf/");
273
assertNotFound("web-inf/jetty-web.xml");
274
assertNotFound("Web-Inf/web.xml");
275
assertNotFound("./WEB-INF/web.xml");
276
assertNotFound("META-INF");
277
assertNotFound("meta-inf/manifest.mf");
278
assertNotFound("Meta-Inf/foo");
279
assertFound("index.html");
283
public void testUnavailable() throws Exception
287
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?query=foo");
288
assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
289
assertTrue(context.getServletHandler().isAvailable());
290
url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/ex3/2");
291
try{IO.toString(url.openStream());} catch(IOException e){}
292
assertFalse(context.getServletHandler().isAvailable());
294
assertTrue(context.getServletHandler().isAvailable());
298
private void assertNotFound(String resource) throws MalformedURLException, IOException
302
getResource(resource);
304
catch (FileNotFoundException e)
308
fail("Expected 404 for resource: " + resource);
311
private void assertFound(String resource) throws MalformedURLException, IOException
315
getResource(resource);
317
catch (FileNotFoundException e)
319
fail("Expected 200 for resource: " + resource);
325
private void getResource(String resource) throws MalformedURLException, IOException
328
url = new URL("http://127.0.0.1:" + connector.getLocalPort() + "/test/" + resource);