~ubuntu-branches/ubuntu/utopic/jetty/utopic-proposed

« back to all changes in this revision

Viewing changes to examples/tests/src/test/java/org/mortbay/jetty/WebAppTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-08-09 08:48:10 UTC
  • Revision ID: james.westby@ubuntu.com-20090809084810-k522b97ind2robyd
ImportĀ upstreamĀ versionĀ 6.1.19

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
//========================================================================
 
14
 
 
15
package org.mortbay.jetty;
 
16
 
 
17
import java.io.File;
 
18
import java.io.FileNotFoundException;
 
19
import java.io.IOException;
 
20
import java.net.HttpURLConnection;
 
21
import java.net.MalformedURLException;
 
22
import java.net.URL;
 
23
 
 
24
import junit.framework.TestCase;
 
25
 
 
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;
 
42
 
 
43
public class WebAppTest extends TestCase
 
44
{
 
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;
 
53
    
 
54
    protected void setUp() throws Exception
 
55
    {
 
56
        File dir = new File(".").getAbsoluteFile();
 
57
        while (!new File(dir,"webapps").exists())
 
58
        {
 
59
            dir=dir.getParentFile();
 
60
        }
 
61
        
 
62
        threadPool.setMaxThreads(100);
 
63
        server.setThreadPool(threadPool);
 
64
        
 
65
        server.setConnectors(new Connector[]{connector});
 
66
        
 
67
        handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler});
 
68
        server.setHandler(handlers);
 
69
        
 
70
        context=new WebAppContext(contexts,dir.getAbsolutePath()+"/webapps/test","/test");
 
71
        
 
72
        userRealm.setName("Test Realm");
 
73
        userRealm.setConfig(dir.getAbsolutePath()+"/etc/realm.properties");
 
74
        server.setUserRealms(new UserRealm[]{userRealm});
 
75
        
 
76
        File file = File.createTempFile("test",".log");
 
77
        NCSARequestLog requestLog = new NCSARequestLog(file.getAbsolutePath());
 
78
        
 
79
        requestLog.setExtended(false);
 
80
        requestLogHandler.setRequestLog(requestLog);
 
81
        
 
82
        server.setSendServerVersion(true);
 
83
        
 
84
        server.start();
 
85
        Thread.sleep(1000);
 
86
    }
 
87
    
 
88
    
 
89
    protected void tearDown() throws Exception
 
90
    {
 
91
        Thread.sleep(100);
 
92
        server.stop();
 
93
        Thread.sleep(100);
 
94
    }
 
95
    
 
96
 
 
97
    public void testDoGet() throws Exception
 
98
    {
 
99
        URL url = null;
 
100
        
 
101
        url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?query=foo");
 
102
        assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
 
103
        
 
104
        url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/");
 
105
        try{IO.toString(url.openStream()); assertTrue(false); } catch(FileNotFoundException e) { assertTrue(true); } 
 
106
 
 
107
        url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test");
 
108
        IO.toString(url.openStream());
 
109
        
 
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());
 
114
        assertEquals(s1,s2);
 
115
 
 
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");
 
119
        
 
120
        String result = IO.toString(url.openStream());
 
121
        if (result.endsWith("\r\n")) {
 
122
            //windows
 
123
            result = result.substring(0,result.length() - 2);
 
124
        } else if (result.endsWith("\n")) {
 
125
            //*nix
 
126
            result = result.substring(0,result.length() - 1);
 
127
        } else {
 
128
            //Error: Unexpected end of stream data encountered
 
129
            assertTrue(false);
 
130
        }
 
131
        assertTrue(result.endsWith("9999 3333333333333333333333333333333333333333333333333333333"));
 
132
 
 
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>"));
 
135
        
 
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>"));
 
138
        
 
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>"));
 
141
 
 
142
        url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?continue=1000");
 
143
        assertTrue(IO.toString(url.openStream()).startsWith("<html>"));
 
144
        
 
145
        
 
146
        url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test/dump/info?lines=100");
 
147
        
 
148
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
 
149
        connection.addRequestProperty("accept-encoding","gzip");
 
150
        connection.connect();
 
151
        assertEquals("gzip",connection.getHeaderField("Content-Encoding"));
 
152
 
 
153
    }
 
154
 
 
155
 
 
156
    public void testRequestListener() throws Exception
 
157
    {
 
158
        URL url = null;
 
159
        String result;
 
160
        
 
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);
 
166
    }
 
167
    
 
168
 
 
169
    public void testSecurity() throws Exception
 
170
    {
 
171
        URL url = null;
 
172
        
 
173
        String[] hidden = {
 
174
           "/WEB-INF/web.xml",
 
175
           "/WEB-INF/wEb.xml",
 
176
           "/WEB-INF/web.xml%00",
 
177
           "/WEB-INF/web.xml\00",
 
178
           "/WEB-INF/web.xml\u0000",
 
179
           "/WEB-INF//web.xml",
 
180
           "//WEB-INF/web.xml",
 
181
           "/WEB-INF//web.xml",
 
182
           "//WEB-INF//web.xml",
 
183
           "//auth/file.txt"
 
184
        };
 
185
        
 
186
        String[] forbidden = {
 
187
                "/auth/",
 
188
                "/auth/file.txt",
 
189
                "/auth//file.txt",
 
190
             };
 
191
        
 
192
        String[] ok = 
 
193
        {
 
194
            "/auth/relax.txt",
 
195
        };
 
196
        
 
197
        
 
198
        for (int i=0;i<hidden.length;i++)
 
199
        {
 
200
            try 
 
201
            {
 
202
                url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+hidden[i]);
 
203
                IO.toString(url.openStream());
 
204
                assertTrue(false);
 
205
            }
 
206
            catch(FileNotFoundException e)
 
207
            {
 
208
                System.err.println(e);
 
209
                assertTrue(true);
 
210
            }
 
211
        }
 
212
 
 
213
        for (int i=0;i<forbidden.length;i++)
 
214
        {
 
215
            try 
 
216
            {
 
217
                url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+forbidden[i]);
 
218
                IO.toString(url.openStream());
 
219
                assertTrue(false);
 
220
            }
 
221
            catch(IOException e)
 
222
            {
 
223
                System.err.println(e);
 
224
                assertTrue(e.toString().indexOf("403")>=0);
 
225
            }
 
226
        }
 
227
 
 
228
        
 
229
        for (int i=0;i<ok.length;i++)
 
230
        {
 
231
            try 
 
232
            {
 
233
                url=new URL("http://127.0.0.1:"+connector.getLocalPort()+"/test"+ok[i]);
 
234
                IO.toString(url.openStream());
 
235
                assertTrue(true);
 
236
            }
 
237
            catch(FileNotFoundException e)
 
238
            {
 
239
                System.err.println(e);
 
240
                assertTrue(false);
 
241
            }
 
242
        }
 
243
        
 
244
    }
 
245
    
 
246
    public void testDoPost() throws Exception
 
247
    {
 
248
        URL url = null;
 
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();
 
258
        
 
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:&nbsp;</th><td>1234")>0);
 
264
    }       
 
265
    
 
266
    
 
267
    public void testWebInfAccess() throws Exception
 
268
    {
 
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"); 
 
280
    }
 
281
    
 
282
 
 
283
    public void testUnavailable() throws Exception
 
284
    {
 
285
        URL url = null;
 
286
        
 
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());
 
293
        Thread.sleep(5000);
 
294
        assertTrue(context.getServletHandler().isAvailable());
 
295
    }
 
296
    
 
297
 
 
298
    private void assertNotFound(String resource) throws MalformedURLException, IOException
 
299
    {
 
300
        try
 
301
        {
 
302
            getResource(resource);
 
303
        }
 
304
        catch (FileNotFoundException e)
 
305
        {
 
306
            return;
 
307
        }
 
308
        fail("Expected 404 for resource: " + resource);
 
309
    }
 
310
 
 
311
    private void assertFound(String resource) throws MalformedURLException, IOException
 
312
    {
 
313
        try
 
314
        {
 
315
            getResource(resource);
 
316
        }
 
317
        catch (FileNotFoundException e)
 
318
        {
 
319
            fail("Expected 200 for resource: " + resource);
 
320
        }
 
321
        // Pass
 
322
        return;
 
323
    }
 
324
 
 
325
    private void getResource(String resource) throws MalformedURLException, IOException
 
326
    {
 
327
        URL url;
 
328
        url = new URL("http://127.0.0.1:" + connector.getLocalPort() + "/test/" + resource);
 
329
        url.openStream();
 
330
    }   
 
331
    
 
332
    
 
333
}