0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: *
0017: */
0018:
0019: package org.apache.jmeter.protocol.http.sampler;
0020:
0021: import java.io.ByteArrayOutputStream;
0022: import java.io.File;
0023: import java.io.FileOutputStream;
0024: import java.io.IOException;
0025: import java.io.OutputStream;
0026: import java.io.UnsupportedEncodingException;
0027: import java.net.URLEncoder;
0028: import java.net.URL;
0029: import java.util.Locale;
0030:
0031: import org.apache.jmeter.engine.util.ValueReplacer;
0032: import org.apache.jmeter.protocol.http.control.HttpMirrorControl;
0033: import org.apache.jmeter.protocol.http.util.EncoderCache;
0034: import org.apache.jmeter.protocol.http.util.HTTPArgument;
0035: import org.apache.jmeter.testelement.TestPlan;
0036: import org.apache.jmeter.threads.JMeterContextService;
0037: import org.apache.jmeter.threads.JMeterVariables;
0038: import org.apache.jmeter.util.JMeterUtils;
0039: import org.apache.oro.text.regex.MatchResult;
0040: import org.apache.oro.text.regex.Pattern;
0041: import org.apache.oro.text.regex.PatternMatcherInput;
0042: import org.apache.oro.text.regex.Perl5Compiler;
0043: import org.apache.oro.text.regex.Perl5Matcher;
0044:
0045: import junit.framework.Test;
0046: import junit.framework.TestCase;
0047: import junit.framework.TestSuite;
0048: import junit.extensions.TestSetup;
0049:
0050: /**
0051: * Class for performing actual samples for HTTPSampler and HTTPSampler2.
0052: * The samples are executed against the HttpMirrorServer, which is
0053: * started when the unit tests are executed.
0054: */
0055: public class TestHTTPSamplersAgainstHttpMirrorServer extends TestCase {
0056: private final static int HTTP_SAMPLER = 0;
0057: private final static int HTTP_SAMPLER2 = 1;
0058:
0059: /** The encodings used for http headers and control information */
0060: private final static String ISO_8859_1 = "ISO-8859-1"; // $NON-NLS-1$
0061: private static final String US_ASCII = "US-ASCII"; // $NON-NLS-1$
0062:
0063: private static final byte[] CRLF = { 0x0d, 0x0A };
0064: private static byte[] TEST_FILE_CONTENT;
0065:
0066: private static File temporaryFile;
0067:
0068: public TestHTTPSamplersAgainstHttpMirrorServer(String arg0) {
0069: super (arg0);
0070: }
0071:
0072: public static Test suite() {
0073: TestSetup setup = new TestSetup(new TestSuite(
0074: TestHTTPSamplersAgainstHttpMirrorServer.class)) {
0075: private int webServerPort = 8080;
0076: private HttpMirrorControl webServerControl;
0077:
0078: protected void setUp() throws Exception {
0079: webServerControl = new HttpMirrorControl();
0080: webServerControl.setPort(webServerPort);
0081: webServerControl.startHttpMirror();
0082:
0083: // Create the test file content
0084: TEST_FILE_CONTENT = new String(
0085: "some foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85")
0086: .getBytes("UTF-8");
0087:
0088: // create a temporary file to make sure we always have a file to give to the PostWriter
0089: // Whereever we are or Whatever the current path is.
0090: temporaryFile = File.createTempFile(
0091: "TestHTTPSamplersAgainstHttpMirrorServer",
0092: "tmp");
0093: OutputStream output = new FileOutputStream(
0094: temporaryFile);
0095: output.write(TEST_FILE_CONTENT);
0096: output.flush();
0097: output.close();
0098: try {
0099: Thread.sleep(100);
0100: } catch (InterruptedException e) {
0101: }// Allow thread chance to fail
0102: if (!webServerControl.isServerAlive()) {
0103: throw new Exception("Could not start mirror server");
0104: }
0105: }
0106:
0107: protected void tearDown() throws Exception {
0108: // Shutdown web server
0109: webServerControl.stopHttpMirror();
0110: //webServerControl = null;
0111:
0112: // delete temporay file
0113: temporaryFile.delete();
0114: }
0115: };
0116: return setup;
0117: };
0118:
0119: public void testPostRequest_UrlEncoded() throws Exception {
0120: testPostRequest_UrlEncoded(HTTP_SAMPLER, ISO_8859_1);
0121: }
0122:
0123: public void testPostRequest_UrlEncoded2() throws Exception {
0124: testPostRequest_UrlEncoded(HTTP_SAMPLER2, US_ASCII);
0125: }
0126:
0127: public void testPostRequest_FormMultipart() throws Exception {
0128: testPostRequest_FormMultipart(HTTP_SAMPLER, ISO_8859_1);
0129: }
0130:
0131: public void testPostRequest_FormMultipart2() throws Exception {
0132: testPostRequest_FormMultipart(HTTP_SAMPLER2, US_ASCII);
0133: }
0134:
0135: public void testPostRequest_FileUpload() throws Exception {
0136: testPostRequest_FileUpload(HTTP_SAMPLER, ISO_8859_1);
0137: }
0138:
0139: public void testPostRequest_FileUpload2() throws Exception {
0140: testPostRequest_FileUpload(HTTP_SAMPLER2, US_ASCII);
0141: }
0142:
0143: public void testPostRequest_BodyFromParameterValues()
0144: throws Exception {
0145: testPostRequest_BodyFromParameterValues(HTTP_SAMPLER,
0146: ISO_8859_1);
0147: }
0148:
0149: public void testPostRequest_BodyFromParameterValues2()
0150: throws Exception {
0151: testPostRequest_BodyFromParameterValues(HTTP_SAMPLER2, US_ASCII);
0152: }
0153:
0154: public void testGetRequest() throws Exception {
0155: testGetRequest(HTTP_SAMPLER);
0156: }
0157:
0158: public void testGetRequest2() throws Exception {
0159: testGetRequest(HTTP_SAMPLER2);
0160: }
0161:
0162: public void testGetRequest_Parameters() throws Exception {
0163: testGetRequest_Parameters(HTTP_SAMPLER);
0164: }
0165:
0166: public void testGetRequest_Parameters2() throws Exception {
0167: testGetRequest_Parameters(HTTP_SAMPLER2);
0168: }
0169:
0170: private void testPostRequest_UrlEncoded(int samplerType,
0171: String samplerDefaultEncoding) throws Exception {
0172: String titleField = "title";
0173: String titleValue = "mytitle";
0174: String descriptionField = "description";
0175: String descriptionValue = "mydescription";
0176:
0177: // Test sending data with default encoding
0178: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0179: String contentEncoding = "";
0180: setupUrl(sampler, contentEncoding);
0181: setupFormData(sampler, false, titleField, titleValue,
0182: descriptionField, descriptionValue);
0183: HTTPSampleResult res = executeSampler(sampler);
0184: checkPostRequestUrlEncoded(sampler, res,
0185: samplerDefaultEncoding, contentEncoding, titleField,
0186: titleValue, descriptionField, descriptionValue, false);
0187:
0188: // Test sending data as ISO-8859-1
0189: sampler = createHttpSampler(samplerType);
0190: contentEncoding = ISO_8859_1;
0191: setupUrl(sampler, contentEncoding);
0192: setupFormData(sampler, false, titleField, titleValue,
0193: descriptionField, descriptionValue);
0194: res = executeSampler(sampler);
0195: checkPostRequestUrlEncoded(sampler, res,
0196: samplerDefaultEncoding, contentEncoding, titleField,
0197: titleValue, descriptionField, descriptionValue, false);
0198:
0199: // Test sending data as UTF-8
0200: sampler = createHttpSampler(samplerType);
0201: contentEncoding = "UTF-8";
0202: titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
0203: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
0204: setupUrl(sampler, contentEncoding);
0205: setupFormData(sampler, false, titleField, titleValue,
0206: descriptionField, descriptionValue);
0207: res = executeSampler(sampler);
0208: checkPostRequestUrlEncoded(sampler, res,
0209: samplerDefaultEncoding, contentEncoding, titleField,
0210: titleValue, descriptionField, descriptionValue, false);
0211:
0212: // Test sending data as UTF-8, with values that will change when urlencoded
0213: sampler = createHttpSampler(samplerType);
0214: contentEncoding = "UTF-8";
0215: titleValue = "mytitle/=";
0216: descriptionValue = "mydescription /\\";
0217: setupUrl(sampler, contentEncoding);
0218: setupFormData(sampler, false, titleField, titleValue,
0219: descriptionField, descriptionValue);
0220: res = executeSampler(sampler);
0221: checkPostRequestUrlEncoded(sampler, res,
0222: samplerDefaultEncoding, contentEncoding, titleField,
0223: titleValue, descriptionField, descriptionValue, false);
0224:
0225: // Test sending data as UTF-8, with values that have been urlencoded
0226: sampler = createHttpSampler(samplerType);
0227: contentEncoding = "UTF-8";
0228: titleValue = "mytitle%2F%3D";
0229: descriptionValue = "mydescription+++%2F%5C";
0230: setupUrl(sampler, contentEncoding);
0231: setupFormData(sampler, true, titleField, titleValue,
0232: descriptionField, descriptionValue);
0233: res = executeSampler(sampler);
0234: checkPostRequestUrlEncoded(sampler, res,
0235: samplerDefaultEncoding, contentEncoding, titleField,
0236: titleValue, descriptionField, descriptionValue, true);
0237:
0238: // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
0239: sampler = createHttpSampler(samplerType);
0240: contentEncoding = "UTF-8";
0241: titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
0242: descriptionValue = "mydescription";
0243: setupUrl(sampler, contentEncoding);
0244: setupFormData(sampler, false, titleField, titleValue,
0245: descriptionField, descriptionValue);
0246: res = executeSampler(sampler);
0247: checkPostRequestUrlEncoded(sampler, res,
0248: samplerDefaultEncoding, contentEncoding, titleField,
0249: titleValue, descriptionField, descriptionValue, false);
0250:
0251: // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses,
0252: // with values urlencoded, but the always encode set to false for the arguments
0253: // This is how the HTTP Proxy server adds arguments to the sampler
0254: sampler = createHttpSampler(samplerType);
0255: contentEncoding = "UTF-8";
0256: titleValue = "%2FwEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ%2FrA%2B8DZ2dnZ2dnZ2d%2FGNDar6OshPwdJc%3D";
0257: descriptionValue = "mydescription";
0258: setupUrl(sampler, contentEncoding);
0259: setupFormData(sampler, false, titleField, titleValue,
0260: descriptionField, descriptionValue);
0261: ((HTTPArgument) sampler.getArguments().getArgument(0))
0262: .setAlwaysEncoded(false);
0263: ((HTTPArgument) sampler.getArguments().getArgument(1))
0264: .setAlwaysEncoded(false);
0265: res = executeSampler(sampler);
0266: assertFalse(((HTTPArgument) sampler.getArguments().getArgument(
0267: 0)).isAlwaysEncoded());
0268: assertFalse(((HTTPArgument) sampler.getArguments().getArgument(
0269: 1)).isAlwaysEncoded());
0270: checkPostRequestUrlEncoded(sampler, res,
0271: samplerDefaultEncoding, contentEncoding, titleField,
0272: titleValue, descriptionField, descriptionValue, true);
0273:
0274: // Test sending data as UTF-8, where user defined variables are used
0275: // to set the value for form data
0276: JMeterUtils.setLocale(Locale.ENGLISH);
0277: TestPlan testPlan = new TestPlan();
0278: JMeterVariables vars = new JMeterVariables();
0279: vars.put("title_prefix", "a test\u00c5");
0280: vars.put("description_suffix", "the_end");
0281: JMeterContextService.getContext().setVariables(vars);
0282: JMeterContextService.getContext().setSamplingStarted(true);
0283: ValueReplacer replacer = new ValueReplacer();
0284: replacer.setUserDefinedVariables(testPlan
0285: .getUserDefinedVariables());
0286:
0287: sampler = createHttpSampler(samplerType);
0288: contentEncoding = "UTF-8";
0289: titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
0290: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
0291: setupUrl(sampler, contentEncoding);
0292: setupFormData(sampler, false, titleField, titleValue,
0293: descriptionField, descriptionValue);
0294: // Replace the variables in the sampler
0295: replacer.replaceValues(sampler);
0296: res = executeSampler(sampler);
0297: String expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
0298: String expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
0299: checkPostRequestUrlEncoded(sampler, res,
0300: samplerDefaultEncoding, contentEncoding, titleField,
0301: expectedTitleValue, descriptionField,
0302: expectedDescriptionValue, false);
0303: }
0304:
0305: private void testPostRequest_FormMultipart(int samplerType,
0306: String samplerDefaultEncoding) throws Exception {
0307: String titleField = "title";
0308: String titleValue = "mytitle";
0309: String descriptionField = "description";
0310: String descriptionValue = "mydescription";
0311:
0312: // Test sending data with default encoding
0313: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0314: String contentEncoding = "";
0315: setupUrl(sampler, contentEncoding);
0316: setupFormData(sampler, false, titleField, titleValue,
0317: descriptionField, descriptionValue);
0318: sampler.setDoMultipartPost(true);
0319: HTTPSampleResult res = executeSampler(sampler);
0320: checkPostRequestFormMultipart(sampler, res,
0321: samplerDefaultEncoding, contentEncoding, titleField,
0322: titleValue, descriptionField, descriptionValue);
0323:
0324: // Test sending data as ISO-8859-1
0325: sampler = createHttpSampler(samplerType);
0326: contentEncoding = ISO_8859_1;
0327: setupUrl(sampler, contentEncoding);
0328: setupFormData(sampler, false, titleField, titleValue,
0329: descriptionField, descriptionValue);
0330: sampler.setDoMultipartPost(true);
0331: res = executeSampler(sampler);
0332: checkPostRequestFormMultipart(sampler, res,
0333: samplerDefaultEncoding, contentEncoding, titleField,
0334: titleValue, descriptionField, descriptionValue);
0335:
0336: // Test sending data as UTF-8
0337: sampler = createHttpSampler(samplerType);
0338: contentEncoding = "UTF-8";
0339: titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
0340: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
0341: setupUrl(sampler, contentEncoding);
0342: setupFormData(sampler, false, titleField, titleValue,
0343: descriptionField, descriptionValue);
0344: sampler.setDoMultipartPost(true);
0345: res = executeSampler(sampler);
0346: checkPostRequestFormMultipart(sampler, res,
0347: samplerDefaultEncoding, contentEncoding, titleField,
0348: titleValue, descriptionField, descriptionValue);
0349:
0350: // Test sending data as UTF-8, with values that would have been urlencoded
0351: // if it was not sent as multipart
0352: sampler = createHttpSampler(samplerType);
0353: contentEncoding = "UTF-8";
0354: titleValue = "mytitle/=";
0355: descriptionValue = "mydescription /\\";
0356: setupUrl(sampler, contentEncoding);
0357: setupFormData(sampler, false, titleField, titleValue,
0358: descriptionField, descriptionValue);
0359: sampler.setDoMultipartPost(true);
0360: res = executeSampler(sampler);
0361: checkPostRequestFormMultipart(sampler, res,
0362: samplerDefaultEncoding, contentEncoding, titleField,
0363: titleValue, descriptionField, descriptionValue);
0364:
0365: // Test sending data as UTF-8, with values that have been urlencoded
0366: sampler = createHttpSampler(samplerType);
0367: contentEncoding = "UTF-8";
0368: titleValue = "mytitle%2F%3D";
0369: descriptionValue = "mydescription+++%2F%5C";
0370: setupUrl(sampler, contentEncoding);
0371: setupFormData(sampler, true, titleField, titleValue,
0372: descriptionField, descriptionValue);
0373: sampler.setDoMultipartPost(true);
0374: res = executeSampler(sampler);
0375: String expectedTitleValue = "mytitle/=";
0376: String expectedDescriptionValue = "mydescription /\\";
0377: checkPostRequestFormMultipart(sampler, res,
0378: samplerDefaultEncoding, contentEncoding, titleField,
0379: expectedTitleValue, descriptionField,
0380: expectedDescriptionValue);
0381:
0382: // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
0383: sampler = createHttpSampler(samplerType);
0384: contentEncoding = "UTF-8";
0385: titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
0386: descriptionValue = "mydescription";
0387: setupUrl(sampler, contentEncoding);
0388: setupFormData(sampler, false, titleField, titleValue,
0389: descriptionField, descriptionValue);
0390: sampler.setDoMultipartPost(true);
0391: res = executeSampler(sampler);
0392: checkPostRequestFormMultipart(sampler, res,
0393: samplerDefaultEncoding, contentEncoding, titleField,
0394: titleValue, descriptionField, descriptionValue);
0395:
0396: // Test sending data as UTF-8, where user defined variables are used
0397: // to set the value for form data
0398: JMeterUtils.setLocale(Locale.ENGLISH);
0399: TestPlan testPlan = new TestPlan();
0400: JMeterVariables vars = new JMeterVariables();
0401: vars.put("title_prefix", "a test\u00c5");
0402: vars.put("description_suffix", "the_end");
0403: JMeterContextService.getContext().setVariables(vars);
0404: JMeterContextService.getContext().setSamplingStarted(true);
0405: ValueReplacer replacer = new ValueReplacer();
0406: replacer.setUserDefinedVariables(testPlan
0407: .getUserDefinedVariables());
0408:
0409: sampler = createHttpSampler(samplerType);
0410: contentEncoding = "UTF-8";
0411: titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
0412: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
0413: setupUrl(sampler, contentEncoding);
0414: setupFormData(sampler, false, titleField, titleValue,
0415: descriptionField, descriptionValue);
0416: sampler.setDoMultipartPost(true);
0417: // Replace the variables in the sampler
0418: replacer.replaceValues(sampler);
0419: res = executeSampler(sampler);
0420: expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
0421: expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
0422: checkPostRequestFormMultipart(sampler, res,
0423: samplerDefaultEncoding, contentEncoding, titleField,
0424: expectedTitleValue, descriptionField,
0425: expectedDescriptionValue);
0426: }
0427:
0428: private void testPostRequest_FileUpload(int samplerType,
0429: String samplerDefaultEncoding) throws Exception {
0430: String titleField = "title";
0431: String titleValue = "mytitle";
0432: String descriptionField = "description";
0433: String descriptionValue = "mydescription";
0434: String fileField = "file1";
0435: String fileMimeType = "text/plain";
0436:
0437: // Test sending data with default encoding
0438: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0439: String contentEncoding = "";
0440: setupUrl(sampler, contentEncoding);
0441: setupFileUploadData(sampler, false, titleField, titleValue,
0442: descriptionField, descriptionValue, fileField,
0443: temporaryFile, fileMimeType);
0444: HTTPSampleResult res = executeSampler(sampler);
0445: checkPostRequestFileUpload(sampler, res,
0446: samplerDefaultEncoding, contentEncoding, titleField,
0447: titleValue, descriptionField, descriptionValue,
0448: fileField, temporaryFile, fileMimeType,
0449: TEST_FILE_CONTENT);
0450:
0451: // Test sending data as ISO-8859-1
0452: sampler = createHttpSampler(samplerType);
0453: contentEncoding = ISO_8859_1;
0454: setupUrl(sampler, contentEncoding);
0455: setupFileUploadData(sampler, false, titleField, titleValue,
0456: descriptionField, descriptionValue, fileField,
0457: temporaryFile, fileMimeType);
0458: res = executeSampler(sampler);
0459: checkPostRequestFileUpload(sampler, res,
0460: samplerDefaultEncoding, contentEncoding, titleField,
0461: titleValue, descriptionField, descriptionValue,
0462: fileField, temporaryFile, fileMimeType,
0463: TEST_FILE_CONTENT);
0464:
0465: // Test sending data as UTF-8
0466: sampler = createHttpSampler(samplerType);
0467: contentEncoding = "UTF-8";
0468: titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
0469: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
0470: setupUrl(sampler, contentEncoding);
0471: setupFileUploadData(sampler, false, titleField, titleValue,
0472: descriptionField, descriptionValue, fileField,
0473: temporaryFile, fileMimeType);
0474: res = executeSampler(sampler);
0475: checkPostRequestFileUpload(sampler, res,
0476: samplerDefaultEncoding, contentEncoding, titleField,
0477: titleValue, descriptionField, descriptionValue,
0478: fileField, temporaryFile, fileMimeType,
0479: TEST_FILE_CONTENT);
0480: }
0481:
0482: private void testPostRequest_BodyFromParameterValues(
0483: int samplerType, String samplerDefaultEncoding)
0484: throws Exception {
0485: final String titleField = ""; // ensure only values are used
0486: String titleValue = "mytitle";
0487: final String descriptionField = ""; // ensure only values are used
0488: String descriptionValue = "mydescription";
0489:
0490: // Test sending data with default encoding
0491: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0492: String contentEncoding = "";
0493: setupUrl(sampler, contentEncoding);
0494: setupFormData(sampler, false, titleField, titleValue,
0495: descriptionField, descriptionValue);
0496: ((HTTPArgument) sampler.getArguments().getArgument(0))
0497: .setAlwaysEncoded(false);
0498: ((HTTPArgument) sampler.getArguments().getArgument(1))
0499: .setAlwaysEncoded(false);
0500: HTTPSampleResult res = executeSampler(sampler);
0501: String expectedPostBody = titleValue + descriptionValue;
0502: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0503: contentEncoding, expectedPostBody);
0504:
0505: // Test sending data as ISO-8859-1
0506: sampler = createHttpSampler(samplerType);
0507: contentEncoding = ISO_8859_1;
0508: setupUrl(sampler, contentEncoding);
0509: setupFormData(sampler, false, titleField, titleValue,
0510: descriptionField, descriptionValue);
0511: ((HTTPArgument) sampler.getArguments().getArgument(0))
0512: .setAlwaysEncoded(false);
0513: ((HTTPArgument) sampler.getArguments().getArgument(1))
0514: .setAlwaysEncoded(false);
0515: res = executeSampler(sampler);
0516: expectedPostBody = titleValue + descriptionValue;
0517: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0518: contentEncoding, expectedPostBody);
0519:
0520: // Test sending data as UTF-8
0521: sampler = createHttpSampler(samplerType);
0522: contentEncoding = "UTF-8";
0523: titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
0524: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
0525: setupUrl(sampler, contentEncoding);
0526: setupFormData(sampler, false, titleField, titleValue,
0527: descriptionField, descriptionValue);
0528: ((HTTPArgument) sampler.getArguments().getArgument(0))
0529: .setAlwaysEncoded(false);
0530: ((HTTPArgument) sampler.getArguments().getArgument(1))
0531: .setAlwaysEncoded(false);
0532: res = executeSampler(sampler);
0533: expectedPostBody = titleValue + descriptionValue;
0534: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0535: contentEncoding, expectedPostBody);
0536:
0537: // Test sending data as UTF-8, with values that will change when urlencoded
0538: sampler = createHttpSampler(samplerType);
0539: contentEncoding = "UTF-8";
0540: titleValue = "mytitle/=";
0541: descriptionValue = "mydescription /\\";
0542: setupUrl(sampler, contentEncoding);
0543: setupFormData(sampler, false, titleField, titleValue,
0544: descriptionField, descriptionValue);
0545: ((HTTPArgument) sampler.getArguments().getArgument(0))
0546: .setAlwaysEncoded(false);
0547: ((HTTPArgument) sampler.getArguments().getArgument(1))
0548: .setAlwaysEncoded(false);
0549: res = executeSampler(sampler);
0550: expectedPostBody = titleValue + descriptionValue;
0551: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0552: contentEncoding, expectedPostBody);
0553:
0554: // Test sending data as UTF-8, with values that will change when urlencoded, and where
0555: // we tell the sampler to urlencode the parameter value
0556: sampler = createHttpSampler(samplerType);
0557: contentEncoding = "UTF-8";
0558: titleValue = "mytitle/=";
0559: descriptionValue = "mydescription /\\";
0560: setupUrl(sampler, contentEncoding);
0561: setupFormData(sampler, true, titleField, titleValue,
0562: descriptionField, descriptionValue);
0563: res = executeSampler(sampler);
0564: expectedPostBody = URLEncoder.encode(titleValue
0565: + descriptionValue, contentEncoding);
0566: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0567: contentEncoding, expectedPostBody);
0568:
0569: // Test sending data as UTF-8, with values that have been urlencoded
0570: sampler = createHttpSampler(samplerType);
0571: contentEncoding = "UTF-8";
0572: titleValue = "mytitle%2F%3D";
0573: descriptionValue = "mydescription+++%2F%5C";
0574: setupUrl(sampler, contentEncoding);
0575: setupFormData(sampler, false, titleField, titleValue,
0576: descriptionField, descriptionValue);
0577: ((HTTPArgument) sampler.getArguments().getArgument(0))
0578: .setAlwaysEncoded(false);
0579: ((HTTPArgument) sampler.getArguments().getArgument(1))
0580: .setAlwaysEncoded(false);
0581: res = executeSampler(sampler);
0582: expectedPostBody = titleValue + descriptionValue;
0583: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0584: contentEncoding, expectedPostBody);
0585:
0586: // Test sending data as UTF-8, with values that have been urlencoded, and
0587: // where we tell the sampler to urlencode the parameter values
0588: sampler = createHttpSampler(samplerType);
0589: contentEncoding = "UTF-8";
0590: titleValue = "mytitle%2F%3D";
0591: descriptionValue = "mydescription+++%2F%5C";
0592: setupUrl(sampler, contentEncoding);
0593: setupFormData(sampler, true, titleField, titleValue,
0594: descriptionField, descriptionValue);
0595: res = executeSampler(sampler);
0596: expectedPostBody = titleValue + descriptionValue;
0597: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0598: contentEncoding, expectedPostBody);
0599:
0600: // Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
0601: sampler = createHttpSampler(samplerType);
0602: contentEncoding = "UTF-8";
0603: titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
0604: descriptionValue = "mydescription";
0605: setupUrl(sampler, contentEncoding);
0606: setupFormData(sampler, false, titleField, titleValue,
0607: descriptionField, descriptionValue);
0608: ((HTTPArgument) sampler.getArguments().getArgument(0))
0609: .setAlwaysEncoded(false);
0610: ((HTTPArgument) sampler.getArguments().getArgument(1))
0611: .setAlwaysEncoded(false);
0612: res = executeSampler(sampler);
0613: expectedPostBody = titleValue + descriptionValue;
0614: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0615: contentEncoding, expectedPostBody);
0616:
0617: // Test sending data as UTF-8, with + as part of the value,
0618: // where the value is set in sampler as not urluencoded, but the
0619: // isalwaysencoded flag of the argument is set to false.
0620: // This mimics the HTTPSamplerBase.addNonEncodedArgument, which the
0621: // Proxy server calls in some cases
0622: sampler = createHttpSampler(samplerType);
0623: contentEncoding = "UTF-8";
0624: titleValue = "mytitle++";
0625: descriptionValue = "mydescription+";
0626: setupUrl(sampler, contentEncoding);
0627: setupFormData(sampler, false, titleField, titleValue,
0628: descriptionField, descriptionValue);
0629: ((HTTPArgument) sampler.getArguments().getArgument(0))
0630: .setAlwaysEncoded(false);
0631: ((HTTPArgument) sampler.getArguments().getArgument(1))
0632: .setAlwaysEncoded(false);
0633: res = executeSampler(sampler);
0634: expectedPostBody = titleValue + descriptionValue;
0635: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0636: contentEncoding, expectedPostBody);
0637:
0638: // Test sending data as UTF-8, where user defined variables are used
0639: // to set the value for form data
0640: JMeterUtils.setLocale(Locale.ENGLISH);
0641: TestPlan testPlan = new TestPlan();
0642: JMeterVariables vars = new JMeterVariables();
0643: vars.put("title_prefix", "a test\u00c5");
0644: vars.put("description_suffix", "the_end");
0645: JMeterContextService.getContext().setVariables(vars);
0646: JMeterContextService.getContext().setSamplingStarted(true);
0647: ValueReplacer replacer = new ValueReplacer();
0648: replacer.setUserDefinedVariables(testPlan
0649: .getUserDefinedVariables());
0650:
0651: sampler = createHttpSampler(samplerType);
0652: contentEncoding = "UTF-8";
0653: titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
0654: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
0655: setupUrl(sampler, contentEncoding);
0656: setupFormData(sampler, false, titleField, titleValue,
0657: descriptionField, descriptionValue);
0658: ((HTTPArgument) sampler.getArguments().getArgument(0))
0659: .setAlwaysEncoded(false);
0660: ((HTTPArgument) sampler.getArguments().getArgument(1))
0661: .setAlwaysEncoded(false);
0662: // Replace the variables in the sampler
0663: replacer.replaceValues(sampler);
0664: res = executeSampler(sampler);
0665: String expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
0666: String expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
0667: expectedPostBody = expectedTitleValue
0668: + expectedDescriptionValue;
0669: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0670: contentEncoding, expectedPostBody);
0671: }
0672:
0673: private void testGetRequest(int samplerType) throws Exception {
0674: // Test sending simple HTTP get
0675: // Test sending data with default encoding
0676: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0677: String contentEncoding = "";
0678: setupUrl(sampler, contentEncoding);
0679: sampler.setMethod(HTTPSamplerBase.GET);
0680: HTTPSampleResult res = executeSampler(sampler);
0681: checkGetRequest(sampler, res);
0682:
0683: // Test sending data with ISO-8859-1 encoding
0684: sampler = createHttpSampler(samplerType);
0685: contentEncoding = ISO_8859_1;
0686: setupUrl(sampler, contentEncoding);
0687: sampler.setMethod(HTTPSamplerBase.GET);
0688: res = executeSampler(sampler);
0689: checkGetRequest(sampler, res);
0690:
0691: // Test sending data with UTF-8 encoding
0692: sampler = createHttpSampler(samplerType);
0693: contentEncoding = "UTF-8";
0694: setupUrl(sampler, contentEncoding);
0695: sampler.setMethod(HTTPSamplerBase.GET);
0696: res = executeSampler(sampler);
0697: checkGetRequest(sampler, res);
0698: }
0699:
0700: private void testGetRequest_Parameters(int samplerType)
0701: throws Exception {
0702: String titleField = "title";
0703: String titleValue = "mytitle";
0704: String descriptionField = "description";
0705: String descriptionValue = "mydescription";
0706:
0707: // Test sending simple HTTP get
0708: // Test sending data with default encoding
0709: HTTPSamplerBase sampler = createHttpSampler(samplerType);
0710: String contentEncoding = "";
0711: setupUrl(sampler, contentEncoding);
0712: sampler.setMethod(HTTPSamplerBase.GET);
0713: setupFormData(sampler, false, titleField, titleValue,
0714: descriptionField, descriptionValue);
0715: HTTPSampleResult res = executeSampler(sampler);
0716: sampler.setRunningVersion(true);
0717: URL executedUrl = sampler.getUrl();
0718: sampler.setRunningVersion(false);
0719: checkGetRequest_Parameters(sampler, res, contentEncoding,
0720: executedUrl, titleField, titleValue, descriptionField,
0721: descriptionValue, false);
0722:
0723: // Test sending data with ISO-8859-1 encoding
0724: sampler = createHttpSampler(samplerType);
0725: contentEncoding = ISO_8859_1;
0726: titleValue = "mytitle\uc385";
0727: descriptionValue = "mydescription\uc385";
0728: setupUrl(sampler, contentEncoding);
0729: sampler.setMethod(HTTPSamplerBase.GET);
0730: setupFormData(sampler, false, titleField, titleValue,
0731: descriptionField, descriptionValue);
0732: res = executeSampler(sampler);
0733: sampler.setRunningVersion(true);
0734: executedUrl = sampler.getUrl();
0735: sampler.setRunningVersion(false);
0736: checkGetRequest_Parameters(sampler, res, contentEncoding,
0737: executedUrl, titleField, titleValue, descriptionField,
0738: descriptionValue, false);
0739:
0740: // Test sending data with UTF-8 encoding
0741: sampler = createHttpSampler(samplerType);
0742: contentEncoding = "UTF-8";
0743: titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
0744: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
0745: setupUrl(sampler, contentEncoding);
0746: sampler.setMethod(HTTPSamplerBase.GET);
0747: setupFormData(sampler, false, titleField, titleValue,
0748: descriptionField, descriptionValue);
0749: res = executeSampler(sampler);
0750: sampler.setRunningVersion(true);
0751: executedUrl = sampler.getUrl();
0752: sampler.setRunningVersion(false);
0753: checkGetRequest_Parameters(sampler, res, contentEncoding,
0754: executedUrl, titleField, titleValue, descriptionField,
0755: descriptionValue, false);
0756:
0757: // Test sending data as UTF-8, with values that changes when urlencoded
0758: sampler = createHttpSampler(samplerType);
0759: contentEncoding = "UTF-8";
0760: titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
0761: descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
0762: setupUrl(sampler, contentEncoding);
0763: sampler.setMethod(HTTPSamplerBase.GET);
0764: setupFormData(sampler, false, titleField, titleValue,
0765: descriptionField, descriptionValue);
0766: res = executeSampler(sampler);
0767: sampler.setRunningVersion(true);
0768: executedUrl = sampler.getUrl();
0769: sampler.setRunningVersion(false);
0770: String expectedTitleValue = "mytitle\u0153%2B\u20a1+\u0115%26yes\u00c5";
0771: String expectedDescriptionValue = "mydescription+\u0153+\u20a1+\u0115+\u00c5";
0772: checkGetRequest_Parameters(sampler, res, contentEncoding,
0773: executedUrl, titleField, titleValue, descriptionField,
0774: descriptionValue, false);
0775:
0776: // Test sending data as UTF-8, with values that have been urlencoded
0777: sampler = createHttpSampler(samplerType);
0778: contentEncoding = "UTF-8";
0779: titleValue = "mytitle%2F%3D";
0780: descriptionValue = "mydescription+++%2F%5C";
0781: setupUrl(sampler, contentEncoding);
0782: sampler.setMethod(HTTPSamplerBase.GET);
0783: setupFormData(sampler, true, titleField, titleValue,
0784: descriptionField, descriptionValue);
0785: res = executeSampler(sampler);
0786: sampler.setRunningVersion(true);
0787: executedUrl = sampler.getUrl();
0788: sampler.setRunningVersion(false);
0789: checkGetRequest_Parameters(sampler, res, contentEncoding,
0790: executedUrl, titleField, titleValue, descriptionField,
0791: descriptionValue, true);
0792:
0793: // Test sending data as UTF-8, where user defined variables are used
0794: // to set the value for form data
0795: JMeterUtils.setLocale(Locale.ENGLISH);
0796: TestPlan testPlan = new TestPlan();
0797: JMeterVariables vars = new JMeterVariables();
0798: vars.put("title_prefix", "a test\u00c5");
0799: vars.put("description_suffix", "the_end");
0800: JMeterContextService.getContext().setVariables(vars);
0801: JMeterContextService.getContext().setSamplingStarted(true);
0802: ValueReplacer replacer = new ValueReplacer();
0803: replacer.setUserDefinedVariables(testPlan
0804: .getUserDefinedVariables());
0805:
0806: sampler = createHttpSampler(samplerType);
0807: contentEncoding = "UTF-8";
0808: titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
0809: descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
0810: setupUrl(sampler, contentEncoding);
0811: sampler.setMethod(HTTPSamplerBase.GET);
0812: setupFormData(sampler, false, titleField, titleValue,
0813: descriptionField, descriptionValue);
0814: // Replace the variables in the sampler
0815: replacer.replaceValues(sampler);
0816: res = executeSampler(sampler);
0817: expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
0818: expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
0819: sampler.setRunningVersion(true);
0820: executedUrl = sampler.getUrl();
0821: sampler.setRunningVersion(false);
0822: checkGetRequest_Parameters(sampler, res, contentEncoding,
0823: executedUrl, titleField, expectedTitleValue,
0824: descriptionField, expectedDescriptionValue, false);
0825: }
0826:
0827: private HTTPSampleResult executeSampler(HTTPSamplerBase sampler) {
0828: sampler.setRunningVersion(true);
0829: sampler.threadStarted();
0830: HTTPSampleResult res = (HTTPSampleResult) sampler.sample();
0831: sampler.threadFinished();
0832: sampler.setRunningVersion(false);
0833: return res;
0834: }
0835:
0836: private void checkPostRequestUrlEncoded(HTTPSamplerBase sampler,
0837: HTTPSampleResult res, String samplerDefaultEncoding,
0838: String contentEncoding, String titleField,
0839: String titleValue, String descriptionField,
0840: String descriptionValue, boolean valuesAlreadyUrlEncoded)
0841: throws IOException {
0842: if (contentEncoding == null || contentEncoding.length() == 0) {
0843: contentEncoding = samplerDefaultEncoding;
0844: }
0845: // Check URL
0846: assertEquals(sampler.getUrl(), res.getURL());
0847: String expectedPostBody = null;
0848: if (!valuesAlreadyUrlEncoded) {
0849: String expectedTitle = URLEncoder.encode(titleValue,
0850: contentEncoding);
0851: String expectedDescription = URLEncoder.encode(
0852: descriptionValue, contentEncoding);
0853: expectedPostBody = titleField + "=" + expectedTitle + "&"
0854: + descriptionField + "=" + expectedDescription;
0855: } else {
0856: expectedPostBody = titleField + "=" + titleValue + "&"
0857: + descriptionField + "=" + descriptionValue;
0858: }
0859: // Check the request
0860: checkPostRequestBody(sampler, res, samplerDefaultEncoding,
0861: contentEncoding, expectedPostBody);
0862: }
0863:
0864: private void checkPostRequestFormMultipart(HTTPSamplerBase sampler,
0865: HTTPSampleResult res, String samplerDefaultEncoding,
0866: String contentEncoding, String titleField,
0867: String titleValue, String descriptionField,
0868: String descriptionValue) throws IOException {
0869: if (contentEncoding == null || contentEncoding.length() == 0) {
0870: contentEncoding = samplerDefaultEncoding;
0871: }
0872: // Check URL
0873: assertEquals(sampler.getUrl(), res.getURL());
0874: String boundaryString = getBoundaryStringFromContentType(res
0875: .getRequestHeaders());
0876: assertNotNull(boundaryString);
0877: byte[] expectedPostBody = createExpectedFormdataOutput(
0878: boundaryString, contentEncoding, titleField,
0879: titleValue, descriptionField, descriptionValue, true,
0880: true);
0881: // Check request headers
0882: checkHeaderTypeLength(res.getRequestHeaders(),
0883: "multipart/form-data" + "; boundary=" + boundaryString,
0884: expectedPostBody.length);
0885: // Check post body from the result query string
0886: checkArraysHaveSameContent(expectedPostBody, res
0887: .getQueryString().getBytes(contentEncoding));
0888:
0889: // Find the data sent to the mirror server, which the mirror server is sending back to us
0890: String dataSentToMirrorServer = new String(res
0891: .getResponseData(), contentEncoding);
0892: int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
0893: String headersSent = null;
0894: String bodySent = null;
0895: if (posDividerHeadersAndBody >= 0) {
0896: headersSent = dataSentToMirrorServer.substring(0,
0897: posDividerHeadersAndBody);
0898: // Skip the blank line with crlf dividing headers and body
0899: bodySent = dataSentToMirrorServer
0900: .substring(posDividerHeadersAndBody + 2);
0901: } else {
0902: fail("No header and body section found");
0903: }
0904: // Check response headers
0905: checkHeaderTypeLength(headersSent, "multipart/form-data"
0906: + "; boundary=" + boundaryString,
0907: expectedPostBody.length);
0908: // Check post body which was sent to the mirror server, and
0909: // sent back by the mirror server
0910: checkArraysHaveSameContent(expectedPostBody, bodySent
0911: .getBytes(contentEncoding));
0912: // Check method, path and query sent
0913: checkMethodPathQuery(headersSent, sampler.getMethod(), sampler
0914: .getPath(), null);
0915: }
0916:
0917: private void checkPostRequestFileUpload(HTTPSamplerBase sampler,
0918: HTTPSampleResult res, String samplerDefaultEncoding,
0919: String contentEncoding, String titleField,
0920: String titleValue, String descriptionField,
0921: String descriptionValue, String fileField, File fileValue,
0922: String fileMimeType, byte[] fileContent) throws IOException {
0923: if (contentEncoding == null || contentEncoding.length() == 0) {
0924: contentEncoding = samplerDefaultEncoding;
0925: }
0926: // Check URL
0927: assertEquals(sampler.getUrl(), res.getURL());
0928: String boundaryString = getBoundaryStringFromContentType(res
0929: .getRequestHeaders());
0930: assertNotNull(boundaryString);
0931: byte[] expectedPostBody = createExpectedFormAndUploadOutput(
0932: boundaryString, contentEncoding, titleField,
0933: titleValue, descriptionField, descriptionValue,
0934: fileField, fileValue, fileMimeType, fileContent);
0935: // Check request headers
0936: checkHeaderTypeLength(res.getRequestHeaders(),
0937: "multipart/form-data" + "; boundary=" + boundaryString,
0938: expectedPostBody.length);
0939: // We cannot check post body from the result query string, since that will not contain
0940: // the actual file content, but placeholder text for file content
0941: //checkArraysHaveSameContent(expectedPostBody, res.getQueryString().getBytes(contentEncoding));
0942:
0943: // Find the data sent to the mirror server, which the mirror server is sending back to us
0944: String headersSent = getHeadersSent(res.getResponseData());
0945: if (headersSent == null) {
0946: fail("No header and body section found");
0947: }
0948: // Check response headers
0949: checkHeaderTypeLength(headersSent, "multipart/form-data"
0950: + "; boundary=" + boundaryString,
0951: expectedPostBody.length);
0952: byte[] bodySent = getBodySent(res.getResponseData());
0953: assertNotNull("Sent body should not be null", bodySent);
0954: // Check post body which was sent to the mirror server, and
0955: // sent back by the mirror server
0956: checkArraysHaveSameContent(expectedPostBody, bodySent);
0957: // Check method, path and query sent
0958: checkMethodPathQuery(headersSent, sampler.getMethod(), sampler
0959: .getPath(), null);
0960: }
0961:
0962: private void checkPostRequestBody(HTTPSamplerBase sampler,
0963: HTTPSampleResult res, String samplerDefaultEncoding,
0964: String contentEncoding, String expectedPostBody)
0965: throws IOException {
0966: if (contentEncoding == null || contentEncoding.length() == 0) {
0967: contentEncoding = samplerDefaultEncoding;
0968: }
0969: // Check URL
0970: assertEquals(sampler.getUrl(), res.getURL());
0971: // Check request headers
0972: checkHeaderTypeLength(res.getRequestHeaders(),
0973: HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED,
0974: expectedPostBody.getBytes(contentEncoding).length);
0975: // Check post body from the result query string
0976: checkArraysHaveSameContent(expectedPostBody
0977: .getBytes(contentEncoding), res.getQueryString()
0978: .getBytes(contentEncoding));
0979:
0980: // Find the data sent to the mirror server, which the mirror server is sending back to us
0981: String dataSentToMirrorServer = new String(res
0982: .getResponseData(), contentEncoding);
0983: int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
0984: String headersSent = null;
0985: String bodySent = null;
0986: if (posDividerHeadersAndBody >= 0) {
0987: headersSent = dataSentToMirrorServer.substring(0,
0988: posDividerHeadersAndBody);
0989: // Skip the blank line with crlf dividing headers and body
0990: bodySent = dataSentToMirrorServer
0991: .substring(posDividerHeadersAndBody + 2);
0992: } else {
0993: fail("No header and body section found");
0994: }
0995: // Check response headers
0996: checkHeaderTypeLength(headersSent,
0997: HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED,
0998: expectedPostBody.getBytes(contentEncoding).length);
0999: // Check post body which was sent to the mirror server, and
1000: // sent back by the mirror server
1001: checkArraysHaveSameContent(expectedPostBody
1002: .getBytes(contentEncoding), bodySent
1003: .getBytes(contentEncoding));
1004: // Check method, path and query sent
1005: checkMethodPathQuery(headersSent, sampler.getMethod(), sampler
1006: .getPath(), null);
1007: }
1008:
1009: private void checkGetRequest(HTTPSamplerBase sampler,
1010: HTTPSampleResult res) throws IOException {
1011: // Check URL
1012: assertEquals(sampler.getUrl(), res.getURL());
1013: // Check method
1014: assertEquals(sampler.getMethod(), res.getHTTPMethod());
1015: // Check that the query string is empty
1016: assertEquals(0, res.getQueryString().length());
1017:
1018: // Find the data sent to the mirror server, which the mirror server is sending back to us
1019: String dataSentToMirrorServer = new String(res
1020: .getResponseData(), EncoderCache.URL_ARGUMENT_ENCODING);
1021: int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
1022: String headersSent = null;
1023: String bodySent = null;
1024: if (posDividerHeadersAndBody >= 0) {
1025: headersSent = dataSentToMirrorServer.substring(0,
1026: posDividerHeadersAndBody);
1027: // Skip the blank line with crlf dividing headers and body
1028: bodySent = dataSentToMirrorServer
1029: .substring(posDividerHeadersAndBody + 2);
1030: } else {
1031: fail("No header and body section found");
1032: }
1033: // No body should have been sent
1034: assertEquals(bodySent.length(), 0);
1035: // Check method, path and query sent
1036: checkMethodPathQuery(headersSent, sampler.getMethod(), sampler
1037: .getPath(), null);
1038: }
1039:
1040: private void checkGetRequest_Parameters(HTTPSamplerBase sampler,
1041: HTTPSampleResult res, String contentEncoding,
1042: URL executedUrl, String titleField, String titleValue,
1043: String descriptionField, String descriptionValue,
1044: boolean valuesAlreadyUrlEncoded) throws IOException {
1045: if (contentEncoding == null || contentEncoding.length() == 0) {
1046: contentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
1047: }
1048: // Check URL
1049: assertEquals(executedUrl, res.getURL());
1050: // Check method
1051: assertEquals(sampler.getMethod(), res.getHTTPMethod());
1052: // Cannot check the query string of the result, because the mirror server
1053: // replies without including query string in URL
1054:
1055: String expectedQueryString = null;
1056: if (!valuesAlreadyUrlEncoded) {
1057: String expectedTitle = URLEncoder.encode(titleValue,
1058: contentEncoding);
1059: String expectedDescription = URLEncoder.encode(
1060: descriptionValue, contentEncoding);
1061: expectedQueryString = titleField + "=" + expectedTitle
1062: + "&" + descriptionField + "="
1063: + expectedDescription;
1064: } else {
1065: expectedQueryString = titleField + "=" + titleValue + "&"
1066: + descriptionField + "=" + descriptionValue;
1067: }
1068:
1069: // Find the data sent to the mirror server, which the mirror server is sending back to us
1070: String dataSentToMirrorServer = new String(res
1071: .getResponseData(), EncoderCache.URL_ARGUMENT_ENCODING);
1072: int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
1073: String headersSent = null;
1074: String bodySent = null;
1075: if (posDividerHeadersAndBody >= 0) {
1076: headersSent = dataSentToMirrorServer.substring(0,
1077: posDividerHeadersAndBody);
1078: // Skip the blank line with crlf dividing headers and body
1079: bodySent = dataSentToMirrorServer
1080: .substring(posDividerHeadersAndBody + 2);
1081: } else {
1082: fail("No header and body section found");
1083: }
1084: // No body should have been sent
1085: assertEquals(bodySent.length(), 0);
1086: // Check method, path and query sent
1087: checkMethodPathQuery(headersSent, sampler.getMethod(), sampler
1088: .getPath(), expectedQueryString);
1089: }
1090:
1091: private void checkMethodPathQuery(String headersSent,
1092: String expectedMethod, String expectedPath,
1093: String expectedQueryString) throws IOException {
1094: // Check the Request URI sent to the mirror server, and
1095: // sent back by the mirror server
1096: int indexFirstSpace = headersSent.indexOf(" ");
1097: int indexSecondSpace = headersSent.indexOf(" ", headersSent
1098: .length() > indexFirstSpace ? indexFirstSpace + 1
1099: : indexFirstSpace);
1100: if (indexFirstSpace <= 0 && indexSecondSpace <= 0
1101: || indexFirstSpace == indexSecondSpace) {
1102: fail("Could not find method and URI sent");
1103: }
1104: String methodSent = headersSent.substring(0, indexFirstSpace);
1105: assertEquals(expectedMethod, methodSent);
1106: String uriSent = headersSent.substring(indexFirstSpace + 1,
1107: indexSecondSpace);
1108: int indexQueryStart = uriSent.indexOf("?");
1109: if (expectedQueryString != null
1110: && expectedQueryString.length() > 0) {
1111: // We should have a query string part
1112: if (indexQueryStart <= 0
1113: || (indexQueryStart == uriSent.length() - 1)) {
1114: fail("Could not find query string in URI");
1115: }
1116: } else {
1117: if (indexQueryStart > 0) {
1118: // We should not have a query string part
1119: fail("Query string present in URI");
1120: } else {
1121: indexQueryStart = uriSent.length();
1122: }
1123: }
1124: // Check path
1125: String pathSent = uriSent.substring(0, indexQueryStart);
1126: assertEquals(expectedPath, pathSent);
1127: // Check query
1128: if (expectedQueryString != null
1129: && expectedQueryString.length() > 0) {
1130: String queryStringSent = uriSent
1131: .substring(indexQueryStart + 1);
1132: // Is it only the parameter values which are encoded in the specified
1133: // content encoding, the rest of the query is encoded in UTF-8
1134: // Therefore we compare the whole query using UTF-8
1135: checkArraysHaveSameContent(
1136: expectedQueryString
1137: .getBytes(EncoderCache.URL_ARGUMENT_ENCODING),
1138: queryStringSent
1139: .getBytes(EncoderCache.URL_ARGUMENT_ENCODING));
1140: }
1141: }
1142:
1143: private String getHeadersSent(byte[] responseData)
1144: throws IOException {
1145: // Find the data sent to the mirror server, which the mirror server is sending back to us
1146: // We assume the headers are in ISO_8859_1, and the body can be in any content encoding.
1147: String dataSentToMirrorServer = new String(responseData,
1148: ISO_8859_1);
1149: int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
1150: String headersSent = null;
1151: if (posDividerHeadersAndBody >= 0) {
1152: headersSent = dataSentToMirrorServer.substring(0,
1153: posDividerHeadersAndBody);
1154: }
1155: return headersSent;
1156: }
1157:
1158: private byte[] getBodySent(byte[] responseData) throws IOException {
1159: // Find the data sent to the mirror server, which the mirror server is sending back to us
1160: // We assume the headers are in ISO_8859_1, and the body can be in any content encoding.
1161: // Therefore we get the data sent in ISO_8859_1, to be able to determine the end of the
1162: // header part, and then we just construct a byte array to hold the body part, not taking
1163: // encoding of the body into consideration, because it can contain file data, which is
1164: // sent as raw byte data
1165: byte[] bodySent = null;
1166: String headersSent = getHeadersSent(responseData);
1167: if (headersSent != null) {
1168: // Get the content length, it tells us how much data to read
1169: // TODO : Maybe support chunked encoding, then we cannot rely on content length
1170: String contentLengthValue = getSentRequestHeaderValue(
1171: headersSent, HTTPSamplerBase.HEADER_CONTENT_LENGTH);
1172: int contentLength = -1;
1173: if (contentLengthValue != null) {
1174: contentLength = new Integer(contentLengthValue)
1175: .intValue();
1176: } else {
1177: fail("Did not receive any content-lenght header");
1178: }
1179: bodySent = new byte[contentLength];
1180: System.arraycopy(responseData, responseData.length
1181: - contentLength, bodySent, 0, contentLength);
1182: }
1183: return bodySent;
1184: }
1185:
1186: private boolean isInRequestHeaders(String requestHeaders,
1187: String headerName, String headerValue) {
1188: return checkRegularExpression(requestHeaders, headerName + ": "
1189: + headerValue);
1190: }
1191:
1192: private void checkHeaderTypeLength(String requestHeaders,
1193: String contentType, int contentLen) {
1194: boolean typeOK = isInRequestHeaders(requestHeaders,
1195: HTTPSamplerBase.HEADER_CONTENT_TYPE, contentType);
1196: boolean lengOK = isInRequestHeaders(requestHeaders,
1197: HTTPSamplerBase.HEADER_CONTENT_LENGTH, Integer
1198: .toString(contentLen));
1199: if (!typeOK || !lengOK) {
1200: fail("Expected type:" + contentType + " & length: "
1201: + contentLen + " in:\n" + requestHeaders);
1202: }
1203: }
1204:
1205: private String getSentRequestHeaderValue(String requestHeaders,
1206: String headerName) {
1207: Perl5Matcher localMatcher = JMeterUtils.getMatcher();
1208: String expression = ".*" + headerName + ": (\\d*).*";
1209: Pattern pattern = JMeterUtils.getPattern(expression,
1210: Perl5Compiler.READ_ONLY_MASK
1211: | Perl5Compiler.CASE_INSENSITIVE_MASK
1212: | Perl5Compiler.SINGLELINE_MASK);
1213: if (localMatcher.matches(requestHeaders, pattern)) {
1214: // The value is in the first group, group 0 is the whole match
1215: return localMatcher.getMatch().group(1);
1216: } else {
1217: return null;
1218: }
1219: }
1220:
1221: private boolean checkRegularExpression(String stringToCheck,
1222: String regularExpression) {
1223: Perl5Matcher localMatcher = JMeterUtils.getMatcher();
1224: Pattern pattern = JMeterUtils.getPattern(regularExpression,
1225: Perl5Compiler.READ_ONLY_MASK
1226: | Perl5Compiler.CASE_INSENSITIVE_MASK
1227: | Perl5Compiler.SINGLELINE_MASK);
1228: return localMatcher.contains(stringToCheck, pattern);
1229: }
1230:
1231: private int getPositionOfBody(String stringToCheck) {
1232: Perl5Matcher localMatcher = JMeterUtils.getMatcher();
1233: // The headers and body are divided by a blank line
1234: String regularExpression = "^.$";
1235: Pattern pattern = JMeterUtils.getPattern(regularExpression,
1236: Perl5Compiler.READ_ONLY_MASK
1237: | Perl5Compiler.CASE_INSENSITIVE_MASK
1238: | Perl5Compiler.MULTILINE_MASK);
1239:
1240: PatternMatcherInput input = new PatternMatcherInput(
1241: stringToCheck);
1242: while (localMatcher.contains(input, pattern)) {
1243: MatchResult match = localMatcher.getMatch();
1244: return match.beginOffset(0);
1245: }
1246: // No divider was found
1247: return -1;
1248: }
1249:
1250: private String getBoundaryStringFromContentType(
1251: String requestHeaders) {
1252: Perl5Matcher localMatcher = JMeterUtils.getMatcher();
1253: String regularExpression = "^"
1254: + HTTPSamplerBase.HEADER_CONTENT_TYPE
1255: + ": multipart/form-data; boundary=(.+)$";
1256: Pattern pattern = JMeterUtils.getPattern(regularExpression,
1257: Perl5Compiler.READ_ONLY_MASK
1258: | Perl5Compiler.CASE_INSENSITIVE_MASK
1259: | Perl5Compiler.MULTILINE_MASK);
1260: if (localMatcher.contains(requestHeaders, pattern)) {
1261: MatchResult match = localMatcher.getMatch();
1262: return match.group(1);
1263: } else {
1264: return null;
1265: }
1266: }
1267:
1268: private void setupUrl(HTTPSamplerBase sampler,
1269: String contentEncoding) {
1270: String protocol = "http";
1271: // String domain = "localhost";
1272: String domain = "localhost";
1273: String path = "/test/somescript.jsp";
1274: int port = 8080;
1275: sampler.setProtocol(protocol);
1276: sampler.setMethod(HTTPSamplerBase.POST);
1277: sampler.setPath(path);
1278: sampler.setDomain(domain);
1279: sampler.setPort(port);
1280: sampler.setContentEncoding(contentEncoding);
1281: }
1282:
1283: /**
1284: * Setup the form data with specified values
1285: *
1286: * @param httpSampler
1287: */
1288: private void setupFormData(HTTPSamplerBase httpSampler,
1289: boolean isEncoded, String titleField, String titleValue,
1290: String descriptionField, String descriptionValue) {
1291: if (isEncoded) {
1292: httpSampler.addEncodedArgument(titleField, titleValue);
1293: httpSampler.addEncodedArgument(descriptionField,
1294: descriptionValue);
1295: } else {
1296: httpSampler.addArgument(titleField, titleValue);
1297: httpSampler.addArgument(descriptionField, descriptionValue);
1298: }
1299: }
1300:
1301: /**
1302: * Setup the form data with specified values, and file to upload
1303: *
1304: * @param httpSampler
1305: */
1306: private void setupFileUploadData(HTTPSamplerBase httpSampler,
1307: boolean isEncoded, String titleField, String titleValue,
1308: String descriptionField, String descriptionValue,
1309: String fileField, File fileValue, String fileMimeType) {
1310: // Set the form data
1311: setupFormData(httpSampler, isEncoded, titleField, titleValue,
1312: descriptionField, descriptionValue);
1313: // Set the file upload data
1314: httpSampler.setFileField(fileField);
1315: httpSampler.setFilename(fileValue.getAbsolutePath());
1316: httpSampler.setMimetype(fileMimeType);
1317: }
1318:
1319: /**
1320: * Check that the the two byte arrays have identical content
1321: *
1322: * @param expected
1323: * @param actual
1324: * @throws UnsupportedEncodingException
1325: */
1326: private void checkArraysHaveSameContent(byte[] expected,
1327: byte[] actual) throws UnsupportedEncodingException {
1328: if (expected != null && actual != null) {
1329: if (expected.length != actual.length) {
1330: System.out.println(">>>>>>>>>>>>>>>>>>>>");
1331: System.out.println(new String(expected, "UTF-8"));
1332: System.out.println("====================");
1333: System.out.println(new String(actual, "UTF-8"));
1334: System.out.println("<<<<<<<<<<<<<<<<<<<<");
1335: fail("arrays have different length, expected is "
1336: + expected.length + ", actual is "
1337: + actual.length);
1338: } else {
1339: for (int i = 0; i < expected.length; i++) {
1340: if (expected[i] != actual[i]) {
1341: System.out.println(">>>>>>>>>>>>>>>>>>>>");
1342: System.out.println(new String(expected, 0,
1343: i + 1));
1344: System.out.println("====================");
1345: System.out
1346: .println(new String(actual, 0, i + 1));
1347: System.out.println("<<<<<<<<<<<<<<<<<<<<");
1348: /*
1349: // Useful to when debugging
1350: for(int j = 0; j < expected.length; j++) {
1351: System.out.print(expected[j] + " ");
1352: }
1353: System.out.println();
1354: for(int j = 0; j < actual.length; j++) {
1355: System.out.print(actual[j] + " ");
1356: }
1357: System.out.println();
1358: */
1359: fail("byte at position " + i
1360: + " is different, expected is "
1361: + expected[i] + ", actual is "
1362: + actual[i]);
1363: }
1364: }
1365: }
1366: } else {
1367: fail("expected or actual byte arrays were null");
1368: }
1369: }
1370:
1371: /**
1372: * Create the expected output multipart/form-data, with only form data,
1373: * and no file multipart.
1374: * This method is copied from the PostWriterTest class
1375: *
1376: * @param lastMultipart true if this is the last multipart in the request
1377: */
1378: private byte[] createExpectedFormdataOutput(String boundaryString,
1379: String contentEncoding, String titleField,
1380: String titleValue, String descriptionField,
1381: String descriptionValue, boolean firstMultipart,
1382: boolean lastMultipart) throws IOException {
1383: // The encoding used for http headers and control information
1384: final byte[] DASH_DASH = new String("--").getBytes(ISO_8859_1);
1385:
1386: final ByteArrayOutputStream output = new ByteArrayOutputStream();
1387: if (firstMultipart) {
1388: output.write(DASH_DASH);
1389: output.write(boundaryString.getBytes(ISO_8859_1));
1390: output.write(CRLF);
1391: }
1392: output.write("Content-Disposition: form-data; name=\""
1393: .getBytes(ISO_8859_1));
1394: output.write(titleField.getBytes(ISO_8859_1));
1395: output.write("\"".getBytes(ISO_8859_1));
1396: output.write(CRLF);
1397: output.write("Content-Type: text/plain".getBytes(ISO_8859_1));
1398: if (contentEncoding != null) {
1399: output.write("; charset=".getBytes(ISO_8859_1));
1400: output.write(contentEncoding.getBytes(ISO_8859_1));
1401: }
1402: output.write(CRLF);
1403: output.write("Content-Transfer-Encoding: 8bit"
1404: .getBytes(ISO_8859_1));
1405: output.write(CRLF);
1406: output.write(CRLF);
1407: if (contentEncoding != null) {
1408: output.write(titleValue.getBytes(contentEncoding));
1409: } else {
1410: output.write(titleValue.getBytes());
1411: }
1412: output.write(CRLF);
1413: output.write(DASH_DASH);
1414: output.write(boundaryString.getBytes(ISO_8859_1));
1415: output.write(CRLF);
1416: output.write("Content-Disposition: form-data; name=\""
1417: .getBytes(ISO_8859_1));
1418: output.write(descriptionField.getBytes(ISO_8859_1));
1419: output.write("\"".getBytes(ISO_8859_1));
1420: output.write(CRLF);
1421: output.write("Content-Type: text/plain".getBytes(ISO_8859_1));
1422: if (contentEncoding != null) {
1423: output.write("; charset=".getBytes(ISO_8859_1));
1424: output.write(contentEncoding.getBytes(ISO_8859_1));
1425: }
1426: output.write(CRLF);
1427: output.write("Content-Transfer-Encoding: 8bit"
1428: .getBytes(ISO_8859_1));
1429: output.write(CRLF);
1430: output.write(CRLF);
1431: if (contentEncoding != null) {
1432: output.write(descriptionValue.getBytes(contentEncoding));
1433: } else {
1434: output.write(descriptionValue.getBytes());
1435: }
1436: output.write(CRLF);
1437: output.write(DASH_DASH);
1438: output.write(boundaryString.getBytes(ISO_8859_1));
1439: if (lastMultipart) {
1440: output.write(DASH_DASH);
1441: }
1442: output.write(CRLF);
1443:
1444: output.flush();
1445: output.close();
1446:
1447: return output.toByteArray();
1448: }
1449:
1450: /**
1451: * Create the expected file multipart
1452: *
1453: * @param lastMultipart true if this is the last multipart in the request
1454: */
1455: private byte[] createExpectedFilepartOutput(String boundaryString,
1456: String fileField, File file, String mimeType,
1457: byte[] fileContent, boolean firstMultipart,
1458: boolean lastMultipart) throws IOException {
1459: final byte[] DASH_DASH = new String("--").getBytes(ISO_8859_1);
1460:
1461: final ByteArrayOutputStream output = new ByteArrayOutputStream();
1462: if (firstMultipart) {
1463: output.write(DASH_DASH);
1464: output.write(boundaryString.getBytes(ISO_8859_1));
1465: output.write(CRLF);
1466: }
1467: // replace all backslash with double backslash
1468: String filename = file.getName();
1469: output.write("Content-Disposition: form-data; name=\""
1470: .getBytes(ISO_8859_1));
1471: output.write(fileField.getBytes(ISO_8859_1));
1472: output.write(("\"; filename=\"" + filename + "\"")
1473: .getBytes(ISO_8859_1));
1474: output.write(CRLF);
1475: output.write("Content-Type: ".getBytes(ISO_8859_1));
1476: output.write(mimeType.getBytes(ISO_8859_1));
1477: output.write(CRLF);
1478: output.write("Content-Transfer-Encoding: binary"
1479: .getBytes(ISO_8859_1));
1480: output.write(CRLF);
1481: output.write(CRLF);
1482: output.write(fileContent);
1483: output.write(CRLF);
1484: output.write(DASH_DASH);
1485: output.write(boundaryString.getBytes(ISO_8859_1));
1486: if (lastMultipart) {
1487: output.write(DASH_DASH);
1488: }
1489: output.write(CRLF);
1490:
1491: output.flush();
1492: output.close();
1493:
1494: return output.toByteArray();
1495: }
1496:
1497: /**
1498: * Create the expected output post body for form data and file multiparts
1499: * with specified values, when request is multipart
1500: */
1501: private byte[] createExpectedFormAndUploadOutput(
1502: String boundaryString, String contentEncoding,
1503: String titleField, String titleValue,
1504: String descriptionField, String descriptionValue,
1505: String fileField, File fileValue, String fileMimeType,
1506: byte[] fileContent) throws IOException {
1507: // Create the multiparts
1508: byte[] formdataMultipart = createExpectedFormdataOutput(
1509: boundaryString, contentEncoding, titleField,
1510: titleValue, descriptionField, descriptionValue, true,
1511: false);
1512: byte[] fileMultipart = createExpectedFilepartOutput(
1513: boundaryString, fileField, fileValue, fileMimeType,
1514: fileContent, false, true);
1515:
1516: // Join the two multiparts
1517: ByteArrayOutputStream output = new ByteArrayOutputStream();
1518: output.write(formdataMultipart);
1519: output.write(fileMultipart);
1520:
1521: output.flush();
1522: output.close();
1523:
1524: return output.toByteArray();
1525: }
1526:
1527: private HTTPSamplerBase createHttpSampler(int samplerType) {
1528: if (samplerType == HTTP_SAMPLER2) {
1529: return new HTTPSampler2();
1530: } else {
1531: return new HTTPSampler();
1532: }
1533: }
1534: }
|