001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.protocol.http.proxy;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.IOException;
023: import java.net.URLEncoder;
024: import java.util.Collections;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import org.apache.jmeter.config.Arguments;
029: import org.apache.jmeter.junit.JMeterTestCase;
030: import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
031: import org.apache.jmeter.protocol.http.util.HTTPArgument;
032:
033: public class TestHttpRequestHdr extends JMeterTestCase {
034: public TestHttpRequestHdr(String name) {
035: super (name);
036: }
037:
038: public void testRepeatedArguments() throws Exception {
039: String url = "http://localhost/matrix.html";
040: // A HTTP GET request
041: String contentEncoding = "UTF-8";
042: String testGetRequest = "GET "
043: + url
044: + "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= "
045: + "HTTP/1.0\r\n\r\n";
046: HTTPSamplerBase s = getSamplerForRequest(url, testGetRequest,
047: contentEncoding);
048: assertEquals(HTTPSamplerBase.GET, s.getMethod());
049: assertEquals(contentEncoding, s.getContentEncoding());
050: // Check arguments
051: Arguments arguments = s.getArguments();
052: assertEquals(13, arguments.getArgumentCount());
053: checkArgument((HTTPArgument) arguments.getArgument(0),
054: "update", "yes", "yes", contentEncoding, false);
055: checkArgument((HTTPArgument) arguments.getArgument(1), "d",
056: "1", "1", contentEncoding, false);
057: checkArgument((HTTPArgument) arguments.getArgument(2), "d",
058: "2", "2", contentEncoding, false);
059: checkArgument((HTTPArgument) arguments.getArgument(3), "d", "",
060: "", contentEncoding, false);
061: checkArgument((HTTPArgument) arguments.getArgument(4), "d", "",
062: "", contentEncoding, false);
063: checkArgument((HTTPArgument) arguments.getArgument(5), "d", "",
064: "", contentEncoding, false);
065: checkArgument((HTTPArgument) arguments.getArgument(6), "d", "",
066: "", contentEncoding, false);
067: checkArgument((HTTPArgument) arguments.getArgument(7), "d", "",
068: "", contentEncoding, false);
069: checkArgument((HTTPArgument) arguments.getArgument(8), "d",
070: "1", "1", contentEncoding, false);
071: checkArgument((HTTPArgument) arguments.getArgument(9), "d",
072: "2", "2", contentEncoding, false);
073: checkArgument((HTTPArgument) arguments.getArgument(10), "d",
074: "1", "1", contentEncoding, false);
075: checkArgument((HTTPArgument) arguments.getArgument(11), "d",
076: "", "", contentEncoding, false);
077: checkArgument((HTTPArgument) arguments.getArgument(12), "d",
078: "", "", contentEncoding, false);
079:
080: // A HTTP POST request
081: contentEncoding = "UTF-8";
082: String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d=";
083: String testPostRequest = "POST " + url + " HTTP/1.0\n"
084: + "Content-type: "
085: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
086: + "\r\n" + "Content-length: "
087: + getBodyLength(postBody, contentEncoding) + "\r\n"
088: + "\r\n" + postBody;
089: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
090: assertEquals(HTTPSamplerBase.POST, s.getMethod());
091: assertFalse(s.getDoMultipartPost());
092: assertEquals(contentEncoding, s.getContentEncoding());
093: // Check arguments
094: arguments = s.getArguments();
095: assertEquals(13, arguments.getArgumentCount());
096: checkArgument((HTTPArgument) arguments.getArgument(0),
097: "update", "yes", "yes", contentEncoding, false);
098: checkArgument((HTTPArgument) arguments.getArgument(1), "d",
099: "1", "1", contentEncoding, false);
100: checkArgument((HTTPArgument) arguments.getArgument(2), "d",
101: "2", "2", contentEncoding, false);
102: checkArgument((HTTPArgument) arguments.getArgument(3), "d", "",
103: "", contentEncoding, false);
104: checkArgument((HTTPArgument) arguments.getArgument(4), "d", "",
105: "", contentEncoding, false);
106: checkArgument((HTTPArgument) arguments.getArgument(5), "d", "",
107: "", contentEncoding, false);
108: checkArgument((HTTPArgument) arguments.getArgument(6), "d", "",
109: "", contentEncoding, false);
110: checkArgument((HTTPArgument) arguments.getArgument(7), "d", "",
111: "", contentEncoding, false);
112: checkArgument((HTTPArgument) arguments.getArgument(8), "d",
113: "1", "1", contentEncoding, false);
114: checkArgument((HTTPArgument) arguments.getArgument(9), "d",
115: "2", "2", contentEncoding, false);
116: checkArgument((HTTPArgument) arguments.getArgument(10), "d",
117: "1", "1", contentEncoding, false);
118: checkArgument((HTTPArgument) arguments.getArgument(11), "d",
119: "", "", contentEncoding, false);
120: checkArgument((HTTPArgument) arguments.getArgument(12), "d",
121: "", "", contentEncoding, false);
122:
123: // A HTTP POST request, with content-type text/plain
124: contentEncoding = "UTF-8";
125: postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=\uc385&d=";
126: testPostRequest = "POST " + url + " HTTP/1.1\r\n"
127: + "Content-type: text/plain\r\n" + "Content-length: "
128: + getBodyLength(postBody, contentEncoding) + "\r\n"
129: + "\r\n" + postBody;
130: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
131: assertEquals(HTTPSamplerBase.POST, s.getMethod());
132: assertFalse(s.getDoMultipartPost());
133: assertEquals(contentEncoding, s.getContentEncoding());
134: // Check arguments
135: // We should have one argument, with the value equal to the post body
136: arguments = s.getArguments();
137: assertEquals(1, arguments.getArgumentCount());
138: checkArgument((HTTPArgument) arguments.getArgument(0), "",
139: postBody, postBody, contentEncoding, false);
140:
141: // A HTTP POST request, with content-type text/plain; charset=UTF-8
142: // The encoding should be picked up from the header we send with the request
143: contentEncoding = "UTF-8";
144: postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=\uc385&d=";
145: testPostRequest = "POST " + url + " HTTP/1.1\r\n"
146: + "Content-type: text/plain; charset="
147: + contentEncoding + "\r\n" + "Content-length: "
148: + getBodyLength(postBody, contentEncoding) + "\r\n"
149: + "\r\n" + postBody;
150: // Use null for url to simulate that HttpRequestHdr do not
151: // know the encoding for the page. Specify contentEncoding, so the
152: // request is "sent" using that encoding
153: s = getSamplerForRequest(null, testPostRequest, contentEncoding);
154: assertEquals(HTTPSamplerBase.POST, s.getMethod());
155: assertFalse(s.getDoMultipartPost());
156: assertEquals(contentEncoding, s.getContentEncoding());
157: // Check arguments
158: // We should have one argument, with the value equal to the post body
159: arguments = s.getArguments();
160: assertEquals(1, arguments.getArgumentCount());
161: checkArgument((HTTPArgument) arguments.getArgument(0), "",
162: postBody, postBody, contentEncoding, false);
163: }
164:
165: public void testEncodedArguments() throws Exception {
166: String url = "http://localhost/matrix.html";
167: // A HTTP GET request, with encoding not known
168: String contentEncoding = "";
169: String queryString = "abc%3FSPACE=a+b&space=a%20b&query=What%3F";
170: String testGetRequest = "GET " + url + "?" + queryString
171: + " HTTP/1.1\r\n\r\n";
172: // Use null for url and contentEncoding, to simulate that HttpRequestHdr do not
173: // know the encoding for the page
174: HTTPSamplerBase s = getSamplerForRequest(null, testGetRequest,
175: null);
176: assertEquals(HTTPSamplerBase.GET, s.getMethod());
177: assertEquals(queryString, s.getQueryString());
178: assertEquals(contentEncoding, s.getContentEncoding());
179:
180: // Check arguments
181: Arguments arguments = s.getArguments();
182: assertEquals(3, arguments.getArgumentCount());
183: // When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false
184: checkArgument((HTTPArgument) arguments.getArgument(0),
185: "abc%3FSPACE", "a+b", "a+b", contentEncoding, false);
186: checkArgument((HTTPArgument) arguments.getArgument(1), "space",
187: "a%20b", "a%20b", contentEncoding, false);
188: checkArgument((HTTPArgument) arguments.getArgument(2), "query",
189: "What%3F", "What%3F", contentEncoding, false);
190:
191: // A HTTP GET request, with UTF-8 encoding
192: contentEncoding = "UTF-8";
193: queryString = "abc%3FSPACE=a+b&space=a%20b&query=What%3F";
194: testGetRequest = "GET " + url + "?" + queryString
195: + " HTTP/1.1\r\n\r\n";
196: s = getSamplerForRequest(url, testGetRequest, contentEncoding);
197: assertEquals(HTTPSamplerBase.GET, s.getMethod());
198: String expectedQueryString = "abc%3FSPACE=a+b&space=a+b&query=What%3F";
199: assertEquals(expectedQueryString, s.getQueryString());
200: assertEquals(contentEncoding, s.getContentEncoding());
201:
202: // Check arguments
203: arguments = s.getArguments();
204: assertEquals(3, arguments.getArgumentCount());
205: checkArgument((HTTPArgument) arguments.getArgument(0),
206: "abc?SPACE", "a b", "a+b", contentEncoding, true);
207: checkArgument((HTTPArgument) arguments.getArgument(1), "space",
208: "a b", "a+b", contentEncoding, true);
209: checkArgument((HTTPArgument) arguments.getArgument(2), "query",
210: "What?", "What%3F", contentEncoding, true);
211:
212: // A HTTP POST request, with unknown encoding
213: contentEncoding = "";
214: String postBody = "abc%3FSPACE=a+b&space=a%20b&query=What%3F";
215: String testPostRequest = "POST " + url + " HTTP/1.1\r\n"
216: + "Content-type: "
217: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
218: + "\r\n" + "Content-length: "
219: + getBodyLength(postBody, contentEncoding) + "\r\n"
220: + "\r\n" + postBody;
221: // Use null for url and contentEncoding, to simulate that HttpRequestHdr do not
222: // know the encoding for the page
223: s = getSamplerForRequest(null, testPostRequest, null);
224: assertEquals(HTTPSamplerBase.POST, s.getMethod());
225: assertEquals(queryString, s.getQueryString());
226: assertEquals(contentEncoding, s.getContentEncoding());
227: assertFalse(s.getDoMultipartPost());
228:
229: // Check arguments
230: arguments = s.getArguments();
231: assertEquals(3, arguments.getArgumentCount());
232: // When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false
233: checkArgument((HTTPArgument) arguments.getArgument(0),
234: "abc%3FSPACE", "a+b", "a+b", contentEncoding, false);
235: checkArgument((HTTPArgument) arguments.getArgument(1), "space",
236: "a%20b", "a%20b", contentEncoding, false);
237: checkArgument((HTTPArgument) arguments.getArgument(2), "query",
238: "What%3F", "What%3F", contentEncoding, false);
239:
240: // A HTTP POST request, with UTF-8 encoding
241: contentEncoding = "UTF-8";
242: postBody = "abc?SPACE=a+b&space=a%20b&query=What?";
243: testPostRequest = "POST " + url + " HTTP/1.1\n"
244: + "Content-type: "
245: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
246: + "\r\n" + "Content-length: "
247: + getBodyLength(postBody, contentEncoding) + "\r\n"
248: + "\r\n" + postBody;
249: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
250: assertEquals(HTTPSamplerBase.POST, s.getMethod());
251: expectedQueryString = "abc%3FSPACE=a+b&space=a+b&query=What%3F";
252: assertEquals(expectedQueryString, s.getQueryString());
253: assertEquals(contentEncoding, s.getContentEncoding());
254: assertFalse(s.getDoMultipartPost());
255:
256: // Check arguments
257: arguments = s.getArguments();
258: assertEquals(3, arguments.getArgumentCount());
259: checkArgument((HTTPArgument) arguments.getArgument(0),
260: "abc?SPACE", "a b", "a+b", contentEncoding, true);
261: checkArgument((HTTPArgument) arguments.getArgument(1), "space",
262: "a b", "a+b", contentEncoding, true);
263: checkArgument((HTTPArgument) arguments.getArgument(2), "query",
264: "What?", "What%3F", contentEncoding, true);
265: }
266:
267: public void testGetRequestEncodings() throws Exception {
268: String url = "http://localhost/matrix.html";
269: // A HTTP GET request, with encoding not known
270: String contentEncoding = "";
271: String param1Value = "yes";
272: String param2Value = "0+5 -\u00c5\uc385%C3%85";
273: String param2ValueEncoded = URLEncoder.encode(param2Value);
274: String testGetRequest = "GET " + url + "?param1=" + param1Value
275: + "¶m2=" + param2ValueEncoded + " "
276: + "HTTP/1.1\r\n\r\n";
277: // Use null for url and contentEncoding, to simulate that HttpRequestHdr do not
278: // know the encoding for the page
279: HTTPSamplerBase s = getSamplerForRequest(null, testGetRequest,
280: null);
281: assertEquals(HTTPSamplerBase.GET, s.getMethod());
282: assertEquals(contentEncoding, s.getContentEncoding());
283: // Check arguments
284: Arguments arguments = s.getArguments();
285: assertEquals(2, arguments.getArgumentCount());
286: checkArgument((HTTPArgument) arguments.getArgument(0),
287: "param1", param1Value, param1Value, contentEncoding,
288: false);
289: // When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false
290: checkArgument((HTTPArgument) arguments.getArgument(1),
291: "param2", param2ValueEncoded, param2ValueEncoded,
292: contentEncoding, false);
293:
294: // A HTTP GET request, with UTF-8 encoding
295: contentEncoding = "UTF-8";
296: param1Value = "yes";
297: param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
298: param2ValueEncoded = URLEncoder.encode(param2Value,
299: contentEncoding);
300: testGetRequest = "GET " + url + "?param1=" + param1Value
301: + "¶m2=" + param2ValueEncoded + " "
302: + "HTTP/1.1\r\n\r\n";
303: s = getSamplerForRequest(url, testGetRequest, contentEncoding);
304: assertEquals(HTTPSamplerBase.GET, s.getMethod());
305: assertEquals(contentEncoding, s.getContentEncoding());
306: // Check arguments
307: arguments = s.getArguments();
308: assertEquals(2, arguments.getArgumentCount());
309: checkArgument((HTTPArgument) arguments.getArgument(0),
310: "param1", param1Value, param1Value, contentEncoding,
311: false);
312: checkArgument((HTTPArgument) arguments.getArgument(1),
313: "param2", param2Value, param2ValueEncoded,
314: contentEncoding, true);
315:
316: // A HTTP GET request, with ISO-8859-1 encoding
317: contentEncoding = "ISO-8859-1";
318: param1Value = "yes";
319: param2Value = "0+5 -\u00c5%C3%85";
320: param2ValueEncoded = URLEncoder.encode(param2Value,
321: contentEncoding);
322: testGetRequest = "GET " + url + "?param1=" + param1Value
323: + "¶m2=" + param2ValueEncoded + " "
324: + "HTTP/1.1\r\n\r\n";
325: s = getSamplerForRequest(url, testGetRequest, contentEncoding);
326: assertEquals(HTTPSamplerBase.GET, s.getMethod());
327: assertEquals(contentEncoding, s.getContentEncoding());
328: // Check arguments
329: arguments = s.getArguments();
330: assertEquals(2, arguments.getArgumentCount());
331: checkArgument((HTTPArgument) arguments.getArgument(0),
332: "param1", param1Value, param1Value, contentEncoding,
333: false);
334: checkArgument((HTTPArgument) arguments.getArgument(1),
335: "param2", param2Value, param2ValueEncoded,
336: contentEncoding, true);
337: }
338:
339: public void testPostRequestEncodings() throws Exception {
340: String url = "http://localhost/matrix.html";
341: // A HTTP POST request, with encoding not known
342: String contentEncoding = "";
343: String param1Value = "yes";
344: String param2Value = "0+5 -\u00c5%C3%85";
345: String param2ValueEncoded = URLEncoder.encode(param2Value);
346: String postBody = "param1=" + param1Value + "¶m2="
347: + param2ValueEncoded + "\r\n";
348: String testPostRequest = "POST " + url + " HTTP/1.1\r\n"
349: + "Content-type: "
350: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
351: + "\r\n" + "Content-length: "
352: + getBodyLength(postBody, contentEncoding) + "\r\n"
353: + "\r\n" + postBody;
354:
355: // Use null for url and contentEncoding, to simulate that HttpRequestHdr do not
356: // know the encoding for the page
357: HTTPSamplerBase s = getSamplerForRequest(null, testPostRequest,
358: null);
359: assertEquals(HTTPSamplerBase.POST, s.getMethod());
360: assertEquals(contentEncoding, s.getContentEncoding());
361: // Check arguments
362: Arguments arguments = s.getArguments();
363: assertEquals(2, arguments.getArgumentCount());
364: checkArgument((HTTPArgument) arguments.getArgument(0),
365: "param1", param1Value, param1Value, contentEncoding,
366: false);
367: // When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false
368: checkArgument((HTTPArgument) arguments.getArgument(1),
369: "param2", param2ValueEncoded, param2ValueEncoded,
370: contentEncoding, false);
371:
372: // A HTTP POST request, with UTF-8 encoding
373: contentEncoding = "UTF-8";
374: param1Value = "yes";
375: param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85";
376: param2ValueEncoded = URLEncoder.encode(param2Value,
377: contentEncoding);
378: postBody = "param1=" + param1Value + "¶m2="
379: + param2ValueEncoded + "\r\n";
380: testPostRequest = "POST " + url + " HTTP/1.1\r\n"
381: + "Content-type: "
382: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
383: + "\r\n" + "Content-length: "
384: + getBodyLength(postBody, contentEncoding) + "\r\n"
385: + "\r\n" + postBody;
386:
387: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
388: assertEquals(HTTPSamplerBase.POST, s.getMethod());
389: assertEquals(contentEncoding, s.getContentEncoding());
390: // Check arguments
391: arguments = s.getArguments();
392: assertEquals(2, arguments.getArgumentCount());
393: checkArgument((HTTPArgument) arguments.getArgument(0),
394: "param1", param1Value, param1Value, contentEncoding,
395: false);
396: checkArgument((HTTPArgument) arguments.getArgument(1),
397: "param2", param2Value, param2ValueEncoded,
398: contentEncoding, true);
399:
400: // A HTTP POST request, with ISO-8859-1 encoding
401: contentEncoding = "ISO-8859-1";
402: param1Value = "yes";
403: param2Value = "0+5 -\u00c5%C3%85";
404: param2ValueEncoded = URLEncoder.encode(param2Value,
405: contentEncoding);
406: postBody = "param1=" + param1Value + "¶m2="
407: + param2ValueEncoded + "\r\n";
408: testPostRequest = "POST " + url + " HTTP/1.1\r\n"
409: + "Content-type: "
410: + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
411: + "\r\n" + "Content-length: "
412: + getBodyLength(postBody, contentEncoding) + "\r\n"
413: + "\r\n" + postBody;
414:
415: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
416: assertEquals(HTTPSamplerBase.POST, s.getMethod());
417: assertEquals(contentEncoding, s.getContentEncoding());
418: // Check arguments
419: arguments = s.getArguments();
420: assertEquals(2, arguments.getArgumentCount());
421: checkArgument((HTTPArgument) arguments.getArgument(0),
422: "param1", param1Value, param1Value, contentEncoding,
423: false);
424: checkArgument((HTTPArgument) arguments.getArgument(1),
425: "param2", param2Value, param2ValueEncoded,
426: contentEncoding, true);
427: }
428:
429: public void testPostMultipartFormData() throws Exception {
430: String url = "http://localhost/matrix.html";
431: // A HTTP POST request, multipart/form-data, simple values,
432: String contentEncoding = "UTF-8";
433: String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB";
434: String endOfLine = "\r\n";
435: String titleValue = "mytitle";
436: String descriptionValue = "mydescription";
437: String postBody = createMultipartFormBody(titleValue,
438: descriptionValue, contentEncoding, true, boundary,
439: endOfLine);
440: String testPostRequest = createMultipartFormRequest(url,
441: postBody, contentEncoding, boundary, endOfLine);
442:
443: HTTPSamplerBase s = getSamplerForRequest(url, testPostRequest,
444: contentEncoding);
445: assertEquals(HTTPSamplerBase.POST, s.getMethod());
446: assertEquals(contentEncoding, s.getContentEncoding());
447: assertTrue(s.getDoMultipartPost());
448:
449: // Check arguments
450: Arguments arguments = s.getArguments();
451: assertEquals(2, arguments.getArgumentCount());
452: checkArgument((HTTPArgument) arguments.getArgument(0), "title",
453: titleValue, titleValue, contentEncoding, false);
454: checkArgument((HTTPArgument) arguments.getArgument(1),
455: "description", descriptionValue, descriptionValue,
456: contentEncoding, false);
457:
458: // A HTTP POST request, multipart/form-data, simple values,
459: // with \r\n as end of line, which is according to spec,
460: // and with more headers in each multipart
461: endOfLine = "\r\n";
462: titleValue = "mytitle";
463: descriptionValue = "mydescription";
464: postBody = createMultipartFormBody(titleValue,
465: descriptionValue, contentEncoding, true, boundary,
466: endOfLine);
467: testPostRequest = createMultipartFormRequest(url, postBody,
468: contentEncoding, boundary, endOfLine);
469:
470: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
471: assertEquals(HTTPSamplerBase.POST, s.getMethod());
472: assertEquals(contentEncoding, s.getContentEncoding());
473: assertTrue(s.getDoMultipartPost());
474:
475: // Check arguments
476: arguments = s.getArguments();
477: assertEquals(2, arguments.getArgumentCount());
478: checkArgument((HTTPArgument) arguments.getArgument(0), "title",
479: titleValue, titleValue, contentEncoding, false);
480: checkArgument((HTTPArgument) arguments.getArgument(1),
481: "description", descriptionValue, descriptionValue,
482: contentEncoding, false);
483:
484: // A HTTP POST request, multipart/form-data, simple values,
485: // with \n as end of line, which should also be handled,
486: // and with more headers in each multipart
487: endOfLine = "\n";
488: titleValue = "mytitle";
489: descriptionValue = "mydescription";
490: postBody = createMultipartFormBody(titleValue,
491: descriptionValue, contentEncoding, true, boundary,
492: endOfLine);
493: testPostRequest = createMultipartFormRequest(url, postBody,
494: contentEncoding, boundary, endOfLine);
495:
496: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
497: assertEquals(HTTPSamplerBase.POST, s.getMethod());
498: assertEquals(contentEncoding, s.getContentEncoding());
499: assertTrue(s.getDoMultipartPost());
500:
501: // Check arguments
502: arguments = s.getArguments();
503: assertEquals(2, arguments.getArgumentCount());
504: checkArgument((HTTPArgument) arguments.getArgument(0), "title",
505: titleValue, titleValue, contentEncoding, false);
506: checkArgument((HTTPArgument) arguments.getArgument(1),
507: "description", descriptionValue, descriptionValue,
508: contentEncoding, false);
509:
510: // A HTTP POST request, multipart/form-data, with value that will change
511: // if they are url encoded
512: // Values are similar to __VIEWSTATE parameter that .net uses
513: endOfLine = "\r\n";
514: titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
515: descriptionValue = "mydescription";
516: postBody = createMultipartFormBody(titleValue,
517: descriptionValue, contentEncoding, true, boundary,
518: endOfLine);
519: testPostRequest = createMultipartFormRequest(url, postBody,
520: contentEncoding, boundary, endOfLine);
521:
522: s = getSamplerForRequest(url, testPostRequest, contentEncoding);
523: assertEquals(HTTPSamplerBase.POST, s.getMethod());
524: assertEquals(contentEncoding, s.getContentEncoding());
525: assertTrue(s.getDoMultipartPost());
526:
527: // Check arguments
528: arguments = s.getArguments();
529: assertEquals(2, arguments.getArgumentCount());
530: checkArgument((HTTPArgument) arguments.getArgument(0), "title",
531: titleValue, titleValue, contentEncoding, false);
532: checkArgument((HTTPArgument) arguments.getArgument(1),
533: "description", descriptionValue, descriptionValue,
534: contentEncoding, false);
535: }
536:
537: public void testPostMultipartFileUpload() throws Exception {
538: String url = "http://localhost/matrix.html";
539: // A HTTP POST request, multipart/form-data, simple values,
540: String contentEncoding = "UTF-8";
541: String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB";
542: String endOfLine = "\r\n";
543: String fileFieldValue = "test_file";
544: String fileName = "somefilename.txt";
545: String mimeType = "text/plain";
546: String fileContent = "somedummycontent\n\ndfgdfg\r\nfgdgdg\nContent-type:dfsfsfds";
547: String postBody = createMultipartFileUploadBody(fileFieldValue,
548: fileName, mimeType, fileContent, boundary, endOfLine);
549: String testPostRequest = createMultipartFormRequest(url,
550: postBody, contentEncoding, boundary, endOfLine);
551:
552: HTTPSamplerBase s = getSamplerForRequest(url, testPostRequest,
553: contentEncoding);
554: assertEquals(HTTPSamplerBase.POST, s.getMethod());
555: assertEquals(contentEncoding, s.getContentEncoding());
556: assertEquals("", s.getQueryString());
557: assertTrue(s.getDoMultipartPost());
558:
559: // Check arguments
560: Arguments arguments = s.getArguments();
561: assertEquals(0, arguments.getArgumentCount());
562: assertEquals(fileFieldValue, s.getFileField());
563: assertEquals(fileName, s.getFilename());
564: assertEquals(mimeType, s.getMimetype());
565: }
566:
567: private String createMultipartFormBody(String titleValue,
568: String descriptionValue, String contentEncoding,
569: boolean includeExtraHeaders, String boundary,
570: String endOfLine) {
571: // Title multipart
572: String postBody = "--" + boundary + endOfLine
573: + "Content-Disposition: form-data; name=\"title\""
574: + endOfLine;
575: if (includeExtraHeaders) {
576: postBody += "Content-Type: text/plain; charset="
577: + contentEncoding + endOfLine
578: + "Content-Transfer-Encoding: 8bit" + endOfLine;
579: }
580: postBody += endOfLine + titleValue + endOfLine + "--"
581: + boundary + endOfLine;
582: // Description multipart
583: postBody += "Content-Disposition: form-data; name=\"description\""
584: + endOfLine;
585: if (includeExtraHeaders) {
586: postBody += "Content-Type: text/plain; charset="
587: + contentEncoding + endOfLine
588: + "Content-Transfer-Encoding: 8bit" + endOfLine;
589: }
590: postBody += endOfLine + descriptionValue + endOfLine + "--"
591: + boundary + "--" + endOfLine;
592:
593: return postBody;
594: }
595:
596: private String createMultipartFileUploadBody(String fileField,
597: String fileName, String fileMimeType, String fileContent,
598: String boundary, String endOfLine) {
599: // File upload multipart
600: String postBody = "--" + boundary + endOfLine
601: + "Content-Disposition: form-data; name=\"" + fileField
602: + "\" filename=\"" + fileName + "\"" + endOfLine
603: + "Content-Type: " + fileMimeType + endOfLine
604: + "Content-Transfer-Encoding: binary" + endOfLine
605: + endOfLine + fileContent + endOfLine + "--" + boundary
606: + "--" + endOfLine;
607: return postBody;
608: }
609:
610: private String createMultipartFormRequest(String url,
611: String postBody, String contentEncoding, String boundary,
612: String endOfLine) throws IOException {
613: String postRequest = "POST " + url + " HTTP/1.1" + endOfLine
614: + "Content-type: "
615: + HTTPSamplerBase.MULTIPART_FORM_DATA + "; boundary="
616: + boundary + endOfLine + "Content-length: "
617: + getBodyLength(postBody, contentEncoding) + endOfLine
618: + endOfLine + postBody;
619: return postRequest;
620: }
621:
622: private HTTPSamplerBase getSamplerForRequest(String url,
623: String request, String contentEncoding) throws IOException {
624: HttpRequestHdr req = new HttpRequestHdr();
625: ByteArrayInputStream bis = null;
626: if (contentEncoding != null) {
627: bis = new ByteArrayInputStream(request
628: .getBytes(contentEncoding));
629:
630: } else {
631: // Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
632: bis = new ByteArrayInputStream(request
633: .getBytes("ISO-8859-1"));
634: }
635: req.parse(bis);
636: bis.close();
637: Map pageEncodings = Collections.synchronizedMap(new HashMap());
638: Map formEncodings = Collections.synchronizedMap(new HashMap());
639: if (url != null && contentEncoding != null) {
640: pageEncodings.put(url, contentEncoding);
641: }
642: return req.getSampler(pageEncodings, formEncodings);
643: }
644:
645: private void checkArgument(HTTPArgument arg, String expectedName,
646: String expectedValue, String expectedEncodedValue,
647: String contentEncoding, boolean expectedEncoded)
648: throws IOException {
649: assertEquals(expectedName, arg.getName());
650: // System.out.println("expect " + URLEncoder.encode(expectedValue, "UTF-8"));
651: // System.out.println("actual " + URLEncoder.encode(arg.getValue(), "UTF-8"));
652: assertEquals(expectedValue, arg.getValue());
653: if (contentEncoding != null && contentEncoding.length() > 0) {
654: assertEquals(expectedEncodedValue, arg
655: .getEncodedValue(contentEncoding));
656: } else {
657: // Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
658: assertEquals(expectedEncodedValue, arg
659: .getEncodedValue("ISO-8859-1"));
660: }
661: assertEquals(expectedEncoded, arg.isAlwaysEncoded());
662: }
663:
664: private int getBodyLength(String postBody, String contentEncoding)
665: throws IOException {
666: if (contentEncoding != null && contentEncoding.length() > 0) {
667: return postBody.getBytes(contentEncoding).length;
668: } else {
669: // Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
670: return postBody.getBytes().length;
671: }
672: }
673: }
|