File: StatelessTest.java

package info (click to toggle)
httpunit 1.7%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,412 kB
  • sloc: java: 33,504; xml: 483; sh: 68; makefile: 11
file content (376 lines) | stat: -rw-r--r-- 15,562 bytes parent folder | download | duplicates (6)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package com.meterware.servletunit;
/********************************************************************************************************************
 * $Id: StatelessTest.java 570 2003-07-22 01:49:09Z russgold $
 *
 * Copyright (c) 2000-2003, Russell Gold
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 *******************************************************************************************************************/
import com.meterware.httpunit.*;
import com.meterware.pseudoserver.HttpUserAgentTest;

import java.io.*;
import java.net.HttpURLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import junit.framework.TestCase;
import junit.framework.TestSuite;


/**
 * Tests support for stateless HttpServlets.
 *
 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
 **/
public class StatelessTest extends TestCase {

    public static void main( String args[] ) {
        junit.textui.TestRunner.run( suite() );
    }


    public static TestSuite suite() {
        return new TestSuite( StatelessTest.class );
    }


    public StatelessTest( String name ) {
        super( name );
    }


    public void testNotFound() throws Exception {
        ServletRunner sr = new ServletRunner();

        WebRequest request = new GetMethodWebRequest( "http://localhost/nothing" );
        try {
            sr.getResponse( request );
            fail( "Should have rejected the request" );
        } catch (HttpNotFoundException e) {
            assertEquals( "Response code", HttpURLConnection.HTTP_NOT_FOUND, e.getResponseCode() );
        }
    }


    public void testServletCaching() throws Exception {
        final String resourceName = "something/interesting";

        assertEquals( "Initial instances of servlet class", 0, AccessCountServlet.getNumInstances() );
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( resourceName, AccessCountServlet.class.getName() );

        WebRequest request = new GetMethodWebRequest( "http://localhost/" + resourceName );
        assertEquals( "First reply", "1", sr.getResponse( request ).getText().trim() );
        assertEquals( "Instances of servlet class after first call", 1, AccessCountServlet.getNumInstances() );
        assertEquals( "Second reply", "2", sr.getResponse( request ).getText().trim() );
        assertEquals( "Instances of servlet class after first call", 1, AccessCountServlet.getNumInstances() );
        sr.shutDown();
        assertEquals( "Instances of servlet class after shutdown", 0, AccessCountServlet.getNumInstances() );
    }


    public void testServletAccessByClassName() throws Exception {
        ServletRunner sr = new ServletRunner();

        WebRequest request = new GetMethodWebRequest( "http://localhost/servlet/" + SimpleGetServlet.class.getName() );
        WebResponse response = sr.getResponse( request );
        assertNotNull( "No response received", response );
        assertEquals( "content type", "text/html", response.getContentType() );
        assertEquals( "requested resource", SimpleGetServlet.RESPONSE_TEXT, response.getText() );
    }


    public void testSimpleGet() throws Exception {
        final String resourceName = "something/interesting";

        ServletRunner sr = new ServletRunner();
        sr.registerServlet( resourceName, SimpleGetServlet.class.getName() );

        WebRequest request = new GetMethodWebRequest( "http://localhost/" + resourceName );
        WebResponse response = sr.getResponse( request );
        assertNotNull( "No response received", response );
        assertEquals( "content type", "text/html", response.getContentType() );
        assertEquals( "requested resource", SimpleGetServlet.RESPONSE_TEXT, response.getText() );
    }


    public void testGetWithSetParams() throws Exception {
        final String resourceName = "something/interesting";

        ServletRunner sr = new ServletRunner();
        sr.registerServlet( resourceName, ParameterServlet.class.getName() );

        WebRequest request = new GetMethodWebRequest( "http://localhost/" + resourceName );
        request.setParameter( "color", "red" );
        WebResponse response = sr.getResponse( request );
        assertNotNull( "No response received", response );
        assertEquals( "content type", "text/plain", response.getContentType() );
        assertEquals( "requested resource", "You selected red", response.getText() );
        String[] headers = response.getHeaderFields( "MyHeader" );
        assertEquals( "Number of MyHeaders returned", 2, headers.length );
        assertEquals( "MyHeader #1", "value1", headers[ 0 ] );
        assertEquals( "MyHeader #2", "value2", headers[ 1 ] );
    }


    public void testGetWithInlineParams() throws Exception {
        final String resourceName = "something/interesting";

        ServletRunner sr = new ServletRunner();
        sr.registerServlet( resourceName, ParameterServlet.class.getName() );

        WebRequest request = new GetMethodWebRequest( "http://localhost/" + resourceName + "?color=dark+red" );
        WebResponse response = sr.getResponse( request );
        assertNotNull( "No response received", response );
        assertEquals( "content type", "text/plain", response.getContentType() );
        assertEquals( "requested resource", "You selected dark red", response.getText() );
    }


    public void testHeaderRetrieval() throws Exception {
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( "/Parameters", ParameterServlet.class.getName() );

        ServletUnitClient client = sr.newClient();
        client.setHeaderField( "Sample", "Value" );
        client.setHeaderField( "Request", "Client" );
        WebRequest request = new GetMethodWebRequest( "http://localhost/Parameters?color=dark+red" );
        request.setHeaderField( "request", "Caller" );
        InvocationContext ic = client.newInvocation( request );
        assertEquals( "Sample header", "Value", ic.getRequest().getHeader( "sample" ) );
        assertEquals( "Request header", "Caller", ic.getRequest().getHeader( "Request" ) );
    }


    public void testParameterHandling() throws Exception {
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( "/testForm", FormSubmissionServlet.class.getName() );

        ServletUnitClient client = sr.newClient();
        WebResponse wr = client.getResponse( "http://localhost/testForm" );
        WebForm form = wr.getForms()[0];
        form.setParameter( "login", "me" );
        form.setParameter( "password", "haha" );
        form.submit();
        assertEquals( "Resultant response", "You posted me,haha", client.getCurrentPage().getText() );
    }


    public void testSimplePost() throws Exception {
        final String resourceName = "something/interesting";

        ServletRunner sr = new ServletRunner();
        sr.registerServlet( resourceName, ParameterServlet.class.getName() );

        WebRequest request = new PostMethodWebRequest( "http://localhost/" + resourceName );
        request.setParameter( "color", "red" );
        WebResponse response = sr.getResponse( request );
        assertNotNull( "No response received", response );
        assertEquals( "content type", "text/plain", response.getContentType() );
        assertEquals( "requested resource", "You posted red", response.getText() );
    }


    public void testStreamBasedPost() throws Exception {
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( "ReportData", BodyEcho.class.getName() );

        String sourceData = "This is an interesting test\nWith two lines";
        InputStream source = new ByteArrayInputStream( sourceData.getBytes( "iso-8859-1" ) );

        WebClient wc = sr.newClient();
        WebRequest wr = new PostMethodWebRequest( "http://localhost/ReportData", source, "text/sample" );
        WebResponse response = wc.getResponse( wr );
        assertEquals( "Body response", sourceData.length() + "\n" + sourceData, response.getText() );
        assertEquals( "Content-type", "text/sample", response.getContentType() );
    }


    public void testRequestInputStream() throws Exception {
        ServletRunner sr = new ServletRunner();
        WebRequest request = new PostMethodWebRequest( "http://localhost/servlet/" + ParameterServlet.class.getName() );
        request.setParameter( "color", "green" );
        final String expectedBody = "color=green";
        InvocationContext ic = sr.newClient().newInvocation( request );
        assertEquals( "Message body type", "application/x-www-form-urlencoded", ic.getRequest().getContentType() );
        InputStream is = ic.getRequest().getInputStream();
        byte[] buffer = new byte[ expectedBody.length() ];
        assertEquals( "Input stream length", buffer.length, is.read( buffer ) );
        assertEquals( "Message body", expectedBody, new String( buffer ) );
    }


    public void testFrameAccess() throws Exception {
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( "Frames", FrameTopServlet.class.getName() );
        sr.registerServlet( "RedFrame", SimpleGetServlet.class.getName() );
        sr.registerServlet( "BlueFrame", AccessCountServlet.class.getName() );

        WebClient client = sr.newClient();
        WebRequest request = new GetMethodWebRequest( "http://host/Frames" );
        WebResponse page = client.getResponse( request );
        HttpUserAgentTest.assertMatchingSet( "Frames defined for the conversation", new String[] { "_top", "red", "blue" }, client.getFrameNames() );
        WebResponse response = client.getFrameContents( "red" );
        assertEquals( "Frame contents", SimpleGetServlet.RESPONSE_TEXT, response.getText() );

        page.getSubframeContents( page.getFrameNames()[0] );
    }


    static class SimpleGetServlet extends HttpServlet {

        static String RESPONSE_TEXT = "the desired content\r\n";


        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/html" );
            PrintWriter pw = resp.getWriter();
            pw.print( RESPONSE_TEXT );
            pw.close();
        }
    }


    static class AccessCountServlet extends HttpServlet {

        private int _numAccesses;

        private static int _numInstances = 0;


        public void init() throws ServletException {
            super.init();
            _numInstances++;
        }


        public void destroy() {
            super.destroy();
            _numInstances--;
        }


        public static int getNumInstances() {
            return _numInstances;
        }


        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/plain" );
            PrintWriter pw = resp.getWriter();
            pw.print( String.valueOf( ++_numAccesses ) );
            pw.close();
        }
    }


    static class ParameterServlet extends HttpServlet {

        static String RESPONSE_TEXT = "the desired content\r\n";


        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/plain" );
            resp.addHeader( "MyHeader", "value1" );
            resp.addHeader( "MyHeader", "value2" );

            PrintWriter pw = resp.getWriter();
            pw.print( "You selected " + req.getParameter( "color" ) );
            pw.close();
        }


        protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/plain" );
            PrintWriter pw = resp.getWriter();
            pw.print( "You posted " + req.getParameter( "color" ) );
            pw.close();
        }

    }


    static class BodyEcho extends HttpServlet {
        /**
         * Returns a resource object as a result of a get request.
         **/
        protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            int length = req.getIntHeader( "Content-length" );
            String contentType = req.getHeader( "Content-type" );
            resp.setContentType( contentType );

            InputStreamReader isr = new InputStreamReader( req.getInputStream() );
            BufferedReader br = new BufferedReader( isr );
            resp.getWriter().print( length );

            String line = br.readLine();
            while (line != null) {
                resp.getWriter().print( "\n" );
                resp.getWriter().print( line );
                line = br.readLine();
            }
            resp.getWriter().flush();
            resp.getWriter().close();
        }
    }

    static class FormSubmissionServlet extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/html" );

            PrintWriter pw = resp.getWriter();
            pw.println( "<html><head></head><body>" );
            pw.println( "<FORM ACTION='/testForm?submission=act' METHOD='POST'>" );
            pw.println( "<INPUT NAME='login' TYPE='TEXT'>" );
            pw.println( "<INPUT NAME='password' TYPE='PASSWORD'>" );
            pw.println( "<INPUT TYPE='SUBMIT'>" );
            pw.println( "</FORM></body></html>" );
            pw.close();
        }


        protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/plain" );
            PrintWriter pw = resp.getWriter();
            pw.print( "You posted " + req.getParameter( "login" ) + "," + req.getParameter( "password" ) );
            pw.close();
        }

    }


    static class FrameTopServlet extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException {
            resp.setContentType( "text/html" );

            PrintWriter pw = resp.getWriter();
            pw.println( "<html><head></head><frameset cols='20%,80&'>" );
            pw.println( "<frame src='RedFrame' name='red'>" );
            pw.println( "<frame src='BlueFrame' name='blue'>" );
            pw.println( "</frameset></html>" );
            pw.close();
        }

    }
}