1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus;
|
3
|
|
import java.io.InputStream;
|
4
|
|
import java.util.Enumeration;
|
5
|
|
import java.util.Hashtable;
|
6
|
|
import java.util.StringTokenizer;
|
7
|
|
import java.util.Vector;
|
8
|
|
import org.apache.cactus.client.authentication.AbstractAuthentication;
|
9
|
|
import org.apache.cactus.util.ChainedRuntimeException;
|
10
|
|
|
11
|
|
/**
|
12
|
|
* Contains all HTTP request data for a test case. It is the data that
|
13
|
|
* will be sent to the server redirector and that will be available to the test
|
14
|
|
* methods through the <code>HttpServletRequest</code> object.
|
15
|
|
* <br><br>
|
16
|
|
* Namely, it is :
|
17
|
|
* <ul>
|
18
|
|
* <li>Request parameters that the test case can retrieve using
|
19
|
|
* <code>HttpServletRequest.getParameters()</code>,</li>
|
20
|
|
* <li>Cookies that the test case can retrieve using
|
21
|
|
* <code>HttpServletRequest.getCookies()</code>,</li>
|
22
|
|
* <li>HTTP headers that the test case can retrieve using the
|
23
|
|
* <code>HttpServletRequest.getHeader(), getHeaders(),
|
24
|
|
* ...</code> APIs,</li>
|
25
|
|
* <li>URL data the the test case can retrieve using
|
26
|
|
* <code>HttpServletRequest.getRequestURI(), ...</code></li>
|
27
|
|
* <li>Whether you want the server redirector to automatically create a
|
28
|
|
* session for you or not,</li>
|
29
|
|
* <li>Whether you want the HTTP connection to the server redirector to
|
30
|
|
* use a POST or GET method. Default is POST</li>
|
31
|
|
* <li>Authentication to use (optional)</li>
|
32
|
|
* <li>Content type (optional)</li>
|
33
|
|
* </ul>
|
34
|
|
*
|
35
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
36
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
37
|
|
*
|
38
|
|
* @version $Id: WebRequest.java,v 1.5 2002/07/26 18:50:29 vmassol Exp $
|
39
|
|
*/
|
40
|
|
public class WebRequest implements Request {
|
41
|
|
/**
|
42
|
|
* The request parameters that need to be sent in the body (POST)
|
43
|
|
*/
|
44
|
|
private Hashtable parametersPost;
|
45
|
|
/**
|
46
|
|
* The request parameters that need to be sent in the URL (GET)
|
47
|
|
*/
|
48
|
|
private Hashtable parametersGet;
|
49
|
|
/**
|
50
|
|
* GET Method identifier.
|
51
|
|
*/
|
52
|
|
public static final String GET_METHOD = "GET";
|
53
|
|
/**
|
54
|
|
* POST Method identifier.
|
55
|
|
*/
|
56
|
|
public static final String POST_METHOD = "POST";
|
57
|
|
/**
|
58
|
|
* The Cookies
|
59
|
|
*/
|
60
|
|
private Vector cookies;
|
61
|
|
/**
|
62
|
|
* HTTP Headers.
|
63
|
|
*/
|
64
|
|
private Hashtable headers;
|
65
|
|
/**
|
66
|
|
* The URL to simulate
|
67
|
|
*/
|
68
|
|
private ServletURL url;
|
69
|
|
/**
|
70
|
|
* Automatic session creation flag (default is true).
|
71
|
|
*/
|
72
|
|
private boolean isAutomaticSession;
|
73
|
|
/**
|
74
|
|
* Binary data to send in the request body (if any)
|
75
|
|
*/
|
76
|
|
private InputStream dataStream;
|
77
|
|
/**
|
78
|
|
* The content type to set in the http request
|
79
|
|
*/
|
80
|
|
private String contentType;
|
81
|
|
/**
|
82
|
|
* The Authentication Object that will configure the http request
|
83
|
|
*/
|
84
|
|
private AbstractAuthentication authentication;
|
85
|
|
/**
|
86
|
|
* Redirector Name. This is to let the user the possibility to override
|
87
|
|
* the default Redirector Name specified in <code>cactus.properties</code>.
|
88
|
|
*/
|
89
|
|
private String redirectorName;
|
90
|
|
/**
|
91
|
|
* Override the redirector Name defined in <code>cactus.properties</code>.
|
92
|
|
* This is useful to define a per test case Name (for example, if some
|
93
|
|
* test case need to have authentication turned on and not other tests,
|
94
|
|
* etc).
|
95
|
|
*
|
96
|
|
* @param theRedirectorName the new redirector Name to use
|
97
|
|
*/
|
98
|
3
|
public void setRedirectorName(String theRedirectorName) {
|
99
|
3
|
this.redirectorName = theRedirectorName;
|
100
|
|
}
|
101
|
|
|
102
|
|
/**
|
103
|
|
* @return the overriden redirector Name or null if none has been defined
|
104
|
|
*/
|
105
|
131
|
public String getRedirectorName() {
|
106
|
131
|
return this.redirectorName;
|
107
|
|
}
|
108
|
|
|
109
|
|
/**
|
110
|
|
* Sets the authentication object that will configure the http request
|
111
|
|
*
|
112
|
|
* @param theAuthenticationObject the authentication object
|
113
|
|
*/
|
114
|
65
|
public void setAuthentication(AbstractAuthentication theAuthenticationObject) {
|
115
|
65
|
this.authentication = theAuthenticationObject;
|
116
|
|
}
|
117
|
|
|
118
|
|
/**
|
119
|
|
* @return the authentication object that will configure the http request
|
120
|
|
*/
|
121
|
192
|
public AbstractAuthentication getAuthentication() {
|
122
|
192
|
return this.authentication;
|
123
|
|
}
|
124
|
|
|
125
|
|
/**
|
126
|
|
* Sets the content type that will be set in the http request
|
127
|
|
*
|
128
|
|
* @param theContentType the content type
|
129
|
|
*/
|
130
|
2
|
public void setContentType(String theContentType) {
|
131
|
2
|
this.contentType = theContentType;
|
132
|
|
}
|
133
|
|
|
134
|
|
/**
|
135
|
|
* @return the content type that will be set in the http request
|
136
|
|
*/
|
137
|
128
|
public String getContentType() {
|
138
|
128
|
return this.contentType;
|
139
|
|
}
|
140
|
|
|
141
|
|
/**
|
142
|
|
* Allow the user to send arbitrary data in the request body
|
143
|
|
*
|
144
|
|
* @param theDataStream the stream on which the data are put by the user
|
145
|
|
*/
|
146
|
1
|
public void setUserData(InputStream theDataStream) {
|
147
|
1
|
this.dataStream = theDataStream;
|
148
|
|
}
|
149
|
|
|
150
|
|
/**
|
151
|
|
* @return the data stream set up by the user
|
152
|
|
*/
|
153
|
256
|
public InputStream getUserData() {
|
154
|
256
|
return this.dataStream;
|
155
|
|
}
|
156
|
|
|
157
|
|
/**
|
158
|
|
* @param isAutomaticSession whether the redirector servlet will
|
159
|
|
* automatically create the HTTP session or not. Default is true.
|
160
|
|
*/
|
161
|
2
|
public void setAutomaticSession(boolean isAutomaticSession) {
|
162
|
2
|
this.isAutomaticSession = isAutomaticSession;
|
163
|
|
}
|
164
|
|
|
165
|
|
/**
|
166
|
|
* @return true if session will be automatically created for the user or
|
167
|
|
* false otherwise.
|
168
|
|
*/
|
169
|
65
|
public boolean getAutomaticSession() {
|
170
|
65
|
return this.isAutomaticSession;
|
171
|
|
}
|
172
|
|
|
173
|
|
/**
|
174
|
|
* Sets the simulated URL. A URL is of the form :<br>
|
175
|
|
* <code><pre><b>
|
176
|
|
* URL = "http://" + serverName (including port) + requestURI ? queryString
|
177
|
|
* <br>requestURI = contextPath + servletPath + pathInfo
|
178
|
|
* </b></pre></code>
|
179
|
|
* From the Servlet 2.2 specification :<br>
|
180
|
|
* <code><pre><ul>
|
181
|
|
* <li><b>Context Path</b>: The path prefix associated with the
|
182
|
|
* ServletContext that this servlet is a part of. If this context is the
|
183
|
|
* default context rooted at the base of the web server's URL namespace,
|
184
|
|
* this path will be an empty string. Otherwise, this path starts with a
|
185
|
|
* character but does not end with a character.</li>
|
186
|
|
* <li><b>Servlet Path</b>: The path section that directly corresponds to
|
187
|
|
* the mapping which activated this request. This path starts with a
|
188
|
|
* character.</li>
|
189
|
|
* <li><b>PathInfo</b>: The part of the request path that is not part of the
|
190
|
|
* Context Path or the Servlet Path.</li>
|
191
|
|
* </ul></pre></code>
|
192
|
|
*
|
193
|
|
* @param theServerName the server name (and port) in the URL to simulate,
|
194
|
|
* i.e. this is the name that will be returned by the
|
195
|
|
* <code>HttpServletRequest.getServerName()</code> and
|
196
|
|
* <code>HttpServletRequest.getServerPort()</code>.
|
197
|
|
* @param theContextPath the webapp context path in the URL to simulate,
|
198
|
|
* i.e. this is the name that will be returned by the
|
199
|
|
* <code>HttpServletRequest.getContextPath()</code>.
|
200
|
|
* Can be null. Format: "/" + name or an empty string
|
201
|
|
* for the default context.
|
202
|
|
* @param theServletPath the servlet path in the URL to simulate,
|
203
|
|
* i.e. this is the name that will be returned by the
|
204
|
|
* <code>HttpServletRequest.getServletPath()</code>.
|
205
|
|
* Can be null. Format : "/" + name.
|
206
|
|
* @param thePathInfo the path info in the URL to simulate, i.e. this is
|
207
|
|
* the name that will be returned by the
|
208
|
|
* <code>HttpServletRequest.getPathInfo()</code>. Can
|
209
|
|
* be null. Format : "/" + name.
|
210
|
|
* @param theQueryString the Query string in the URL to simulate, i.e. this
|
211
|
|
* is the string that will be returned by the
|
212
|
|
* <code>HttpServletResquest.getQueryString()</code>.
|
213
|
|
* Can be null.
|
214
|
|
*/
|
215
|
11
|
public void setURL(String theServerName, String theContextPath, String theServletPath,
|
216
|
|
String thePathInfo, String theQueryString) {
|
217
|
11
|
this.url = new ServletURL(theServerName, theContextPath, theServletPath, thePathInfo,
|
218
|
|
theQueryString);
|
219
|
11
|
this.addQueryStringParameters(theQueryString);
|
220
|
|
}
|
221
|
|
|
222
|
|
/**
|
223
|
|
* @return the simulated URL
|
224
|
|
*/
|
225
|
87
|
public ServletURL getURL() {
|
226
|
87
|
return this.url;
|
227
|
|
}
|
228
|
|
|
229
|
|
/**
|
230
|
|
* Adds a parameter to the request. It is possible to add several times the
|
231
|
|
* the same parameter name, but with different value (the same as for the
|
232
|
|
* <code>HttpServletRequest</code>).
|
233
|
|
*
|
234
|
|
* @param theName the parameter's name
|
235
|
|
* @param theValue the parameter's value
|
236
|
|
* @param theMethod GET_METHOD or POST_METHOD. If GET_METHOD then the
|
237
|
|
* parameter will be sent in the query string of the URL. If
|
238
|
|
* POST_METHOD, it will be sent as a parameter in the request body.
|
239
|
|
*/
|
240
|
380
|
public void addParameter(String theName, String theValue, String theMethod) {
|
241
|
380
|
Hashtable parameters;
|
242
|
380
|
if (theMethod.equalsIgnoreCase("POST")) {
|
243
|
6
|
parameters = this.parametersPost;
|
244
|
374
|
} else if (theMethod.equalsIgnoreCase("GET")) {
|
245
|
373
|
parameters = this.parametersGet;
|
246
|
|
} else {
|
247
|
1
|
throw new ChainedRuntimeException("The method need to be either \"POST\" or \"GET\"");
|
248
|
|
}
|
249
|
379
|
if (parameters.containsKey(theName)) {
|
250
|
4
|
Vector v = (Vector)parameters.get(theName);
|
251
|
4
|
v.addElement(theValue);
|
252
|
|
} else {
|
253
|
375
|
Vector v = new Vector();
|
254
|
375
|
v.addElement(theValue);
|
255
|
375
|
parameters.put(theName, v);
|
256
|
|
}
|
257
|
|
}
|
258
|
|
|
259
|
|
/**
|
260
|
|
* Adds a parameter to the request. The parameter is added to the query
|
261
|
|
* string of the URL.
|
262
|
|
*
|
263
|
|
* @param theName the parameter's name
|
264
|
|
* @param theValue the parameter's value
|
265
|
|
*
|
266
|
|
* @see #addParameter(String, String, String)
|
267
|
|
*/
|
268
|
11
|
public void addParameter(String theName, String theValue) {
|
269
|
11
|
this.addParameter(theName, theValue, "GET");
|
270
|
|
}
|
271
|
|
|
272
|
|
/**
|
273
|
|
* @return the parameter names that will be passed in the request body
|
274
|
|
* (POST)
|
275
|
|
*/
|
276
|
257
|
public Enumeration getParameterNamesPost() {
|
277
|
257
|
return this.getParameterNames(this.parametersPost);
|
278
|
|
}
|
279
|
|
|
280
|
|
/**
|
281
|
|
* @return the parameter names that will be passed in the URL (GET)
|
282
|
|
*/
|
283
|
256
|
public Enumeration getParameterNamesGet() {
|
284
|
256
|
return this.getParameterNames(this.parametersGet);
|
285
|
|
}
|
286
|
|
|
287
|
|
/**
|
288
|
|
* Returns all the values in the passed hashtable of parameters.
|
289
|
|
*
|
290
|
|
* @param theParameters the hashtable of parameters
|
291
|
|
* @return the parameter names
|
292
|
|
*/
|
293
|
515
|
private Enumeration getParameterNames(Hashtable theParameters) {
|
294
|
515
|
return theParameters.keys();
|
295
|
|
}
|
296
|
|
|
297
|
|
/**
|
298
|
|
* Returns the first value corresponding to this parameter's name (provided
|
299
|
|
* this parameter is passed in the URL).
|
300
|
|
*
|
301
|
|
* @param theName the parameter's name
|
302
|
|
* @return the first value corresponding to this parameter's name or null
|
303
|
|
* if not found in the list of parameters to be sent in the URL
|
304
|
|
*/
|
305
|
2
|
public String getParameterGet(String theName) {
|
306
|
2
|
String[] values = this.getParameterValuesGet(theName);
|
307
|
2
|
if (values != null) {
|
308
|
1
|
return values[0];
|
309
|
|
}
|
310
|
1
|
return ((String)(null));
|
311
|
|
}
|
312
|
|
|
313
|
|
/**
|
314
|
|
* Returns the first value corresponding to this parameter's name (provided
|
315
|
|
* this parameter is passed in the request body - POST).
|
316
|
|
*
|
317
|
|
* @param theName the parameter's name
|
318
|
|
* @return the first value corresponding to this parameter's name or null
|
319
|
|
* if not found in the list of parameters to be sent in the request
|
320
|
|
* body
|
321
|
|
*/
|
322
|
2
|
public String getParameterPost(String theName) {
|
323
|
2
|
String[] values = this.getParameterValuesPost(theName);
|
324
|
2
|
if (values != null) {
|
325
|
1
|
return values[0];
|
326
|
|
}
|
327
|
1
|
return ((String)(null));
|
328
|
|
}
|
329
|
|
|
330
|
|
/**
|
331
|
|
* Returns all the values corresponding to this parameter's name (provided
|
332
|
|
* this parameter is passed in the URL).
|
333
|
|
*
|
334
|
|
* @param theName the parameter's name
|
335
|
|
* @return the first value corresponding to this parameter's name or null
|
336
|
|
* if not found in the list of parameters to be sent in the URL
|
337
|
|
*/
|
338
|
367
|
public String[] getParameterValuesGet(String theName) {
|
339
|
367
|
return this.getParameterValues(theName, this.parametersGet);
|
340
|
|
}
|
341
|
|
|
342
|
|
/**
|
343
|
|
* Returns all the values corresponding to this parameter's name (provided
|
344
|
|
* this parameter is passed in the request body - POST).
|
345
|
|
*
|
346
|
|
* @param theName the parameter's name
|
347
|
|
* @return the first value corresponding to this parameter's name or null
|
348
|
|
* if not found in the list of parameters to be sent in the request
|
349
|
|
* body
|
350
|
|
*/
|
351
|
4
|
public String[] getParameterValuesPost(String theName) {
|
352
|
4
|
return this.getParameterValues(theName, this.parametersPost);
|
353
|
|
}
|
354
|
|
|
355
|
|
/**
|
356
|
|
* Returns all the values corresponding to this parameter's name in the
|
357
|
|
* provided hashtable.
|
358
|
|
*
|
359
|
|
* @param theName the parameter's name
|
360
|
|
* @param theParameters the hashtable containing the parameters
|
361
|
|
* @return the first value corresponding to this parameter's name or null
|
362
|
|
* if not found in the passed hashtable
|
363
|
|
*/
|
364
|
375
|
private String[] getParameterValues(String theName, Hashtable theParameters) {
|
365
|
375
|
if (theParameters.containsKey(theName)) {
|
366
|
373
|
Vector v = (Vector)theParameters.get(theName);
|
367
|
373
|
Object[] objs = new java.lang.Object[v.size()];
|
368
|
373
|
v.copyInto(objs);
|
369
|
373
|
String[] result = new java.lang.String[objs.length];
|
370
|
373
|
for (int i = 0; i < objs.length; i++) {
|
371
|
377
|
result[i] = (String)objs[i];
|
372
|
|
}
|
373
|
373
|
return result;
|
374
|
|
}
|
375
|
2
|
return ((java.lang.String[])(null));
|
376
|
|
}
|
377
|
|
|
378
|
|
/**
|
379
|
|
* Adds a cookie to the request. The cookie will be created with a
|
380
|
|
* default localhost domain. Use the
|
381
|
|
* <code>addCookie(String theDomain, String theName,
|
382
|
|
* String theValue)</code> method or the
|
383
|
|
* <code>addCookie(Cookie theCookie)</code> if you wish to specify a
|
384
|
|
* domain.
|
385
|
|
*
|
386
|
|
* Note that the domain must match either the redirector host
|
387
|
|
* (specified in <code>cactus.properties</code>) or the host set
|
388
|
|
* using <code>setURL()</code>.
|
389
|
|
*
|
390
|
|
* @param theName the cookie's name
|
391
|
|
* @param theValue the cookie's value
|
392
|
|
*/
|
393
|
5
|
public void addCookie(String theName, String theValue) {
|
394
|
5
|
this.addCookie("localhost", theName, theValue);
|
395
|
|
}
|
396
|
|
|
397
|
|
/**
|
398
|
|
* Adds a cookie to the request. The cookie will be created with the
|
399
|
|
* domain passed as parameter (i.e. the cookie will get sent only to
|
400
|
|
* requests to that domain).
|
401
|
|
*
|
402
|
|
* Note that the domain must match either the redirector host
|
403
|
|
* (specified in <code>cactus.properties</code>) or the host set
|
404
|
|
* using <code>setURL()</code>.
|
405
|
|
*
|
406
|
|
* @param theDomain the cookie domain
|
407
|
|
* @param theName the cookie name
|
408
|
|
* @param theValue the cookie value
|
409
|
|
*/
|
410
|
5
|
public void addCookie(String theDomain, String theName, String theValue) {
|
411
|
5
|
this.addCookie(new Cookie(theDomain, theName, theValue));
|
412
|
|
}
|
413
|
|
|
414
|
|
/**
|
415
|
|
* Adds a cookie to the request.
|
416
|
|
*
|
417
|
|
* Note that the domain must match either the redirector host
|
418
|
|
* (specified in <code>cactus.properties</code>) or the host set
|
419
|
|
* using <code>setURL()</code>.
|
420
|
|
*
|
421
|
|
* @param theCookie the cookie to add
|
422
|
|
*/
|
423
|
5
|
public void addCookie(Cookie theCookie) {
|
424
|
5
|
this.cookies.addElement(theCookie);
|
425
|
|
}
|
426
|
|
|
427
|
|
/**
|
428
|
|
* @return the cookies (vector of <code>Cookie</code> objects)
|
429
|
|
*/
|
430
|
129
|
public Vector getCookies() {
|
431
|
129
|
return this.cookies;
|
432
|
|
}
|
433
|
|
|
434
|
|
/**
|
435
|
|
* Adds a header to the request. Supports adding several values for the
|
436
|
|
* same header name.
|
437
|
|
*
|
438
|
|
* @param theName the header's name
|
439
|
|
* @param theValue the header's value
|
440
|
|
*/
|
441
|
10
|
public void addHeader(String theName, String theValue) {
|
442
|
10
|
if (theName.equalsIgnoreCase("Content-type")) {
|
443
|
1
|
this.setContentType(theValue);
|
444
|
1
|
return;
|
445
|
|
}
|
446
|
9
|
if (this.headers.containsKey(theName)) {
|
447
|
2
|
Vector v = (Vector)this.headers.get(theName);
|
448
|
2
|
v.addElement(theValue);
|
449
|
|
} else {
|
450
|
7
|
Vector v = new Vector();
|
451
|
7
|
v.addElement(theValue);
|
452
|
7
|
this.headers.put(theName, v);
|
453
|
|
}
|
454
|
|
}
|
455
|
|
|
456
|
|
/**
|
457
|
|
* @return the header names
|
458
|
|
*/
|
459
|
129
|
public Enumeration getHeaderNames() {
|
460
|
129
|
return this.headers.keys();
|
461
|
|
}
|
462
|
|
|
463
|
|
/**
|
464
|
|
* Returns the first value corresponding to this header's name.
|
465
|
|
*
|
466
|
|
* @param theName the header's name
|
467
|
|
* @return the first value corresponding to this header's name or null if
|
468
|
|
* not found
|
469
|
|
*/
|
470
|
2
|
public String getHeader(String theName) {
|
471
|
2
|
String[] values = this.getHeaderValues(theName);
|
472
|
2
|
if (values != null) {
|
473
|
1
|
return values[0];
|
474
|
|
}
|
475
|
1
|
return ((String)(null));
|
476
|
|
}
|
477
|
|
|
478
|
|
/**
|
479
|
|
* Returns all the values associated with this header's name.
|
480
|
|
*
|
481
|
|
* @param theName the header's name
|
482
|
|
* @return the values corresponding to this header's name or null if not
|
483
|
|
* found
|
484
|
|
*/
|
485
|
7
|
public String[] getHeaderValues(String theName) {
|
486
|
7
|
if (this.headers.containsKey(theName)) {
|
487
|
6
|
Vector v = (Vector)this.headers.get(theName);
|
488
|
6
|
Object[] objs = new java.lang.Object[v.size()];
|
489
|
6
|
v.copyInto(objs);
|
490
|
6
|
String[] result = new java.lang.String[objs.length];
|
491
|
6
|
for (int i = 0; i < objs.length; i++) {
|
492
|
8
|
result[i] = (String)objs[i];
|
493
|
|
}
|
494
|
6
|
return result;
|
495
|
|
}
|
496
|
1
|
return ((java.lang.String[])(null));
|
497
|
|
}
|
498
|
|
|
499
|
|
/**
|
500
|
|
* Extract the HTTP parameters that might have been specified on the
|
501
|
|
* query string and add them to the list of parameters to pass to the
|
502
|
|
* servlet redirector.
|
503
|
|
*
|
504
|
|
* @param theQueryString the Query string in the URL to simulate, i.e. this
|
505
|
|
* is the string that will be returned by the
|
506
|
|
* <code>HttpServletResquest.getQueryString()</code>.
|
507
|
|
* Can be null.
|
508
|
|
*/
|
509
|
11
|
private void addQueryStringParameters(String theQueryString) {
|
510
|
11
|
if (theQueryString == null) {
|
511
|
7
|
return;
|
512
|
|
}
|
513
|
4
|
String nameValue = null;
|
514
|
4
|
StringTokenizer tokenizer = new StringTokenizer(theQueryString, "&");
|
515
|
4
|
int breakParam = -1;
|
516
|
4
|
while (tokenizer.hasMoreTokens()){
|
517
|
8
|
nameValue = tokenizer.nextToken();
|
518
|
8
|
breakParam = nameValue.indexOf("=");
|
519
|
8
|
if (breakParam != -1) {
|
520
|
7
|
this.addParameter(nameValue.substring(0, breakParam), nameValue.substring(breakParam + 1));
|
521
|
|
} else {
|
522
|
1
|
throw new RuntimeException("Bad QueryString [" + theQueryString + "] NameValue pair: [" +
|
523
|
|
nameValue + "]");
|
524
|
|
}
|
525
|
|
}
|
526
|
|
}
|
527
|
|
|
528
|
|
/**
|
529
|
|
* @return a string representation of the request
|
530
|
|
*/
|
531
|
1
|
public String toString() {
|
532
|
1
|
StringBuffer buffer = new StringBuffer();
|
533
|
1
|
buffer.append("simulation URL = [" + this.getURL() + "], ");
|
534
|
1
|
buffer.append("automatic session = [" + this.getAutomaticSession() + "], ");
|
535
|
1
|
buffer.append("cookies = [");
|
536
|
1
|
buffer.append(this.toStringAppendCookies());
|
537
|
1
|
buffer.append("], ");
|
538
|
1
|
buffer.append("headers = [");
|
539
|
1
|
buffer.append(this.toStringAppendHeaders());
|
540
|
1
|
buffer.append("], ");
|
541
|
1
|
buffer.append("GET parameters = [");
|
542
|
1
|
buffer.append(this.toStringAppendParametersGet());
|
543
|
1
|
buffer.append("], ");
|
544
|
1
|
buffer.append("POST parameters = [");
|
545
|
1
|
buffer.append(this.toStringAppendParametersPost());
|
546
|
1
|
buffer.append("]");
|
547
|
1
|
return buffer.toString();
|
548
|
|
}
|
549
|
|
|
550
|
|
/**
|
551
|
|
* @return a string representation of the headers
|
552
|
|
*/
|
553
|
1
|
private String toStringAppendHeaders() {
|
554
|
1
|
StringBuffer buffer = new StringBuffer();
|
555
|
1
|
Enumeration headers = this.getHeaderNames();
|
556
|
1
|
while (headers.hasMoreElements()){
|
557
|
1
|
buffer.append("[");
|
558
|
1
|
String headerName = (String)headers.nextElement();
|
559
|
1
|
String[] headerValues = this.getHeaderValues(headerName);
|
560
|
1
|
buffer.append("[" + headerName + "] = [");
|
561
|
1
|
for (int i = 0; i < headerValues.length - 1; i++) {
|
562
|
1
|
buffer.append("[" + headerValues[i] + "], ");
|
563
|
|
}
|
564
|
1
|
buffer.append("[" + headerValues[headerValues.length - 1] + "]]");
|
565
|
1
|
buffer.append("]");
|
566
|
|
}
|
567
|
1
|
return buffer.toString();
|
568
|
|
}
|
569
|
|
|
570
|
|
/**
|
571
|
|
* @return a string representation of the cookies
|
572
|
|
*/
|
573
|
1
|
private String toStringAppendCookies() {
|
574
|
1
|
StringBuffer buffer = new StringBuffer();
|
575
|
1
|
Enumeration cookies = this.getCookies().elements();
|
576
|
1
|
while (cookies.hasMoreElements()){
|
577
|
1
|
Cookie cookie = (Cookie)cookies.nextElement();
|
578
|
1
|
buffer.append("[" + cookie + "]");
|
579
|
|
}
|
580
|
1
|
return buffer.toString();
|
581
|
|
}
|
582
|
|
|
583
|
|
/**
|
584
|
|
* @return a string representation of the parameters to be added in the
|
585
|
|
* request body
|
586
|
|
*/
|
587
|
1
|
private String toStringAppendParametersPost() {
|
588
|
1
|
return this.toStringAppendParameters(this.parametersPost);
|
589
|
|
}
|
590
|
|
|
591
|
|
/**
|
592
|
|
* @return a string representation of the parameters to be added in the
|
593
|
|
* URL
|
594
|
|
*/
|
595
|
1
|
private String toStringAppendParametersGet() {
|
596
|
1
|
return this.toStringAppendParameters(this.parametersGet);
|
597
|
|
}
|
598
|
|
|
599
|
|
/**
|
600
|
|
* @param theParameters the HTTP parameters
|
601
|
|
* @return a string representation of the HTTP parameters passed as
|
602
|
|
* parameters
|
603
|
|
*/
|
604
|
2
|
private String toStringAppendParameters(Hashtable theParameters) {
|
605
|
2
|
StringBuffer buffer = new StringBuffer();
|
606
|
2
|
Enumeration parameters = this.getParameterNames(theParameters);
|
607
|
2
|
while (parameters.hasMoreElements()){
|
608
|
4
|
buffer.append("[");
|
609
|
4
|
String parameterName = (String)parameters.nextElement();
|
610
|
4
|
String[] parameterValues = this.getParameterValues(parameterName, theParameters);
|
611
|
4
|
buffer.append("[" + parameterName + "] = [");
|
612
|
4
|
for (int i = 0; i < parameterValues.length - 1; i++) {
|
613
|
1
|
buffer.append("[" + parameterValues[i] + "], ");
|
614
|
|
}
|
615
|
4
|
buffer.append("[" + parameterValues[parameterValues.length - 1] + "]]");
|
616
|
4
|
buffer.append("]");
|
617
|
|
}
|
618
|
2
|
return buffer.toString();
|
619
|
|
}
|
620
|
|
|
621
|
|
/**
|
622
|
|
* Contains all HTTP request data for a test case. It is the data that
|
623
|
|
* will be sent to the server redirector and that will be available to the test
|
624
|
|
* methods through the <code>HttpServletRequest</code> object.
|
625
|
|
* <br><br>
|
626
|
|
* Namely, it is :
|
627
|
|
* <ul>
|
628
|
|
* <li>Request parameters that the test case can retrieve using
|
629
|
|
* <code>HttpServletRequest.getParameters()</code>,</li>
|
630
|
|
* <li>Cookies that the test case can retrieve using
|
631
|
|
* <code>HttpServletRequest.getCookies()</code>,</li>
|
632
|
|
* <li>HTTP headers that the test case can retrieve using the
|
633
|
|
* <code>HttpServletRequest.getHeader(), getHeaders(),
|
634
|
|
* ...</code> APIs,</li>
|
635
|
|
* <li>URL data the the test case can retrieve using
|
636
|
|
* <code>HttpServletRequest.getRequestURI(), ...</code></li>
|
637
|
|
* <li>Whether you want the server redirector to automatically create a
|
638
|
|
* session for you or not,</li>
|
639
|
|
* <li>Whether you want the HTTP connection to the server redirector to
|
640
|
|
* use a POST or GET method. Default is POST</li>
|
641
|
|
* <li>Authentication to use (optional)</li>
|
642
|
|
* <li>Content type (optional)</li>
|
643
|
|
* </ul>
|
644
|
|
*
|
645
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
646
|
|
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
647
|
|
*
|
648
|
|
* @version $Id: WebRequest.java,v 1.5 2002/07/26 18:50:29 vmassol Exp $
|
649
|
|
*/
|
650
|
149
|
public WebRequest() {
|
651
|
149
|
super();
|
652
|
|
{
|
653
|
|
/**
|
654
|
|
* The request parameters that need to be sent in the body (POST)
|
655
|
|
*/
|
656
|
149
|
this.parametersPost = new Hashtable();
|
657
|
|
/**
|
658
|
|
* The request parameters that need to be sent in the URL (GET)
|
659
|
|
*/
|
660
|
149
|
this.parametersGet = new Hashtable();
|
661
|
|
/**
|
662
|
|
* The Cookies
|
663
|
|
*/
|
664
|
149
|
this.cookies = new Vector();
|
665
|
|
/**
|
666
|
|
* HTTP Headers.
|
667
|
|
*/
|
668
|
149
|
this.headers = new Hashtable();
|
669
|
|
/**
|
670
|
|
* Automatic session creation flag (default is true).
|
671
|
|
*/
|
672
|
149
|
this.isAutomaticSession = true;
|
673
|
|
/**
|
674
|
|
* The content type to set in the http request
|
675
|
|
*/
|
676
|
149
|
this.contentType = "application/x-www-form-urlencoded";
|
677
|
|
}
|
678
|
|
}
|
679
|
|
}
|