File: NavigationTest.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 (184 lines) | stat: -rw-r--r-- 7,415 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
package com.meterware.servletunit;
/********************************************************************************************************************
* $Id: NavigationTest.java 502 2003-03-17 01:35:45Z 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 java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

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

import com.meterware.httpunit.*;

/**
 * Tests support for navigating among servlets.
 **/
public class NavigationTest extends TestCase {

    public static void main(String args[]) {
        junit.textui.TestRunner.run( suite() );
    }
    
    
    public static Test suite() {
        return new TestSuite( NavigationTest.class );
    }


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


    public void testRedirect() throws Exception {
        ServletRunner sr = new ServletRunner();
        sr.registerServlet( "target", TargetServlet.class.getName() );
        sr.registerServlet( "origin", OriginServlet.class.getName() );

        WebClient wc = sr.newClient();
        WebResponse response = wc.getResponse( "http://localhost/origin?color=green" );
        assertNotNull( "No response received", response );
        assertEquals( "Expected response", "color=null: path=/target", response.getText() );
        assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
    }


    public void testForward() throws Exception {
        WebXMLString wxs = new WebXMLString();
        wxs.addServlet( "/target", TargetServlet.class );
        wxs.addServlet( "/origin", FowarderServlet.class );

        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );

        WebClient wc = sr.newClient();
        WebResponse response = wc.getResponse( "http://localhost/context/origin?color=green" );
        assertNotNull( "No response received", response );
        assertEquals( "Expected response", "color=green: path=/context/target", response.getText() );
        assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
    }


    public void testInclude() throws Exception {
        WebXMLString wxs = new WebXMLString();
        wxs.addServlet( "/target", TargetServlet.class );
        wxs.addServlet( "/origin", IncluderServlet.class );

        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );

        WebClient wc = sr.newClient();
        WebResponse response = wc.getResponse( "http://localhost/context/origin?color=green" );
        assertNotNull( "No response received", response );
        assertEquals( "Expected response", "expecting: color=blue: path=/context/origin", response.getText() );
        assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
    }


    public void testForwardViaHttpServletRequest() throws Exception {
        WebXMLString wxs = new WebXMLString();
        wxs.addServlet( "/target", TargetServlet.class );
        wxs.addServlet( "/origin", FowarderServlet2.class );

        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );

        WebClient wc = sr.newClient();
        WebResponse response = wc.getResponse( "http://localhost/context/origin?color=green" );
        assertNotNull( "No response received", response );
        assertEquals( "Expected response", "color=green: path=/context/target", response.getText() );
        assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
    }


    public void testForwardViaRelativePath() throws Exception {
        WebXMLString wxs = new WebXMLString();
        wxs.addServlet( "/some/target", TargetServlet.class );
        wxs.addServlet( "/some/origin", FowarderServlet3.class );

        ServletRunner sr = new ServletRunner( wxs.asInputStream(), "/context" );

        WebClient wc = sr.newClient();
        WebResponse response = wc.getResponse( "http://localhost/context/some/origin?color=green" );
        assertNotNull( "No response received", response );
        assertEquals( "Expected response", "color=green: path=/context/some/target", response.getText() );
        assertEquals( "Returned cookie count", 0, response.getNewCookieNames().length );
    }


    static class OriginServlet extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            resp.setContentType( "text/plain" );
            resp.sendRedirect( "http://localhost/target" );
        }

    }


    static class FowarderServlet extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            getServletContext().getRequestDispatcher( "/target" ).forward( req, resp );
        }

    }


    static class FowarderServlet2 extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            req.getRequestDispatcher( "/target" ).forward( req, resp );
        }

    }


    static class FowarderServlet3 extends HttpServlet {

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            req.getRequestDispatcher( "target" ).forward( req, resp );
        }

    }


    static class IncluderServlet extends HttpServlet {
        static final String PREFIX = "expecting: ";

        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            resp.getWriter().print( PREFIX );
            getServletContext().getRequestDispatcher( "/target?color=blue" ).include( req, resp );
        }
    }


    static class TargetServlet extends HttpServlet {
        protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException,IOException {
            resp.setContentType( "text/plain" );
            PrintWriter pw = resp.getWriter();
            pw.print( "color=" + req.getParameter( "color" ) );
            pw.print( ": path=" + req.getRequestURI() );
            pw.close();
        }

    }
}