📄 testhttpsamplersagainsthttpmirrorserver.java
字号:
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
// Test sending data as UTF-8, where user defined variables are used
// to set the value for form data
JMeterUtils.setLocale(Locale.ENGLISH);
TestPlan testPlan = new TestPlan();
JMeterVariables vars = new JMeterVariables();
vars.put("title_prefix", "a test\u00c5");
vars.put("description_suffix", "the_end");
JMeterContextService.getContext().setVariables(vars);
JMeterContextService.getContext().setSamplingStarted(true);
ValueReplacer replacer = new ValueReplacer();
replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument)sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument)sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
// Replace the variables in the sampler
replacer.replaceValues(sampler);
res = executeSampler(sampler);
String expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
String expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
expectedPostBody = expectedTitleValue+ expectedDescriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody);
}
private void testGetRequest(int samplerType) throws Exception {
// Test sending simple HTTP get
// Test sending data with default encoding
HTTPSamplerBase sampler = createHttpSampler(samplerType);
String contentEncoding = "";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
HTTPSampleResult res = executeSampler(sampler);
checkGetRequest(sampler, res);
// Test sending data with ISO-8859-1 encoding
sampler = createHttpSampler(samplerType);
contentEncoding = ISO_8859_1;
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
res = executeSampler(sampler);
checkGetRequest(sampler, res);
// Test sending data with UTF-8 encoding
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
res = executeSampler(sampler);
checkGetRequest(sampler, res);
}
private void testGetRequest_Parameters(int samplerType) throws Exception {
String titleField = "title";
String titleValue = "mytitle";
String descriptionField = "description";
String descriptionValue = "mydescription";
// Test sending simple HTTP get
// Test sending data with default encoding
HTTPSamplerBase sampler = createHttpSampler(samplerType);
String contentEncoding = "";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
HTTPSampleResult res = executeSampler(sampler);
sampler.setRunningVersion(true);
URL executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data with ISO-8859-1 encoding
sampler = createHttpSampler(samplerType);
contentEncoding = ISO_8859_1;
titleValue = "mytitle\uc385";
descriptionValue = "mydescription\uc385";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
sampler.setRunningVersion(true);
executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data with UTF-8 encoding
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
sampler.setRunningVersion(true);
executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as UTF-8, with values that changes when urlencoded
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
sampler.setRunningVersion(true);
executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
String expectedTitleValue = "mytitle\u0153%2B\u20a1+\u0115%26yes\u00c5";
String expectedDescriptionValue = "mydescription+\u0153+\u20a1+\u0115+\u00c5";
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as UTF-8, with values that have been urlencoded
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle%2F%3D";
descriptionValue = "mydescription+++%2F%5C";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
sampler.setRunningVersion(true);
executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, titleValue, descriptionField, descriptionValue, true);
// Test sending data as UTF-8, where user defined variables are used
// to set the value for form data
JMeterUtils.setLocale(Locale.ENGLISH);
TestPlan testPlan = new TestPlan();
JMeterVariables vars = new JMeterVariables();
vars.put("title_prefix", "a test\u00c5");
vars.put("description_suffix", "the_end");
JMeterContextService.getContext().setVariables(vars);
JMeterContextService.getContext().setSamplingStarted(true);
ValueReplacer replacer = new ValueReplacer();
replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
setupUrl(sampler, contentEncoding);
sampler.setMethod(HTTPSamplerBase.GET);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
// Replace the variables in the sampler
replacer.replaceValues(sampler);
res = executeSampler(sampler);
expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
sampler.setRunningVersion(true);
executedUrl = sampler.getUrl();
sampler.setRunningVersion(false);
checkGetRequest_Parameters(sampler, res, contentEncoding, executedUrl, titleField, expectedTitleValue, descriptionField, expectedDescriptionValue, false);
}
private HTTPSampleResult executeSampler(HTTPSamplerBase sampler) {
sampler.setRunningVersion(true);
sampler.threadStarted();
HTTPSampleResult res = (HTTPSampleResult) sampler.sample();
sampler.threadFinished();
sampler.setRunningVersion(false);
return res;
}
private void checkPostRequestUrlEncoded(
HTTPSamplerBase sampler,
HTTPSampleResult res,
String samplerDefaultEncoding,
String contentEncoding,
String titleField,
String titleValue,
String descriptionField,
String descriptionValue,
boolean valuesAlreadyUrlEncoded) throws IOException {
if(contentEncoding == null || contentEncoding.length() == 0) {
contentEncoding = samplerDefaultEncoding;
}
// Check URL
assertEquals(sampler.getUrl(), res.getURL());
String expectedPostBody = null;
if(!valuesAlreadyUrlEncoded) {
String expectedTitle = URLEncoder.encode(titleValue, contentEncoding);
String expectedDescription = URLEncoder.encode(descriptionValue, contentEncoding);
expectedPostBody = titleField + "=" + expectedTitle + "&" + descriptionField + "=" + expectedDescription;
}
else {
expectedPostBody = titleField + "=" + titleValue + "&" + descriptionField + "=" + descriptionValue;
}
// Check the request
checkPostRequestBody(
sampler,
res,
samplerDefaultEncoding,
contentEncoding,
expectedPostBody
);
}
private void checkPostRequestFormMultipart(
HTTPSamplerBase sampler,
HTTPSampleResult res,
String samplerDefaultEncoding,
String contentEncoding,
String titleField,
String titleValue,
String descriptionField,
String descriptionValue) throws IOException {
if(contentEncoding == null || contentEncoding.length() == 0) {
contentEncoding = samplerDefaultEncoding;
}
// Check URL
assertEquals(sampler.getUrl(), res.getURL());
String boundaryString = getBoundaryStringFromContentType(res.getRequestHeaders());
assertNotNull(boundaryString);
byte[] expectedPostBody = createExpectedFormdataOutput(boundaryString, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
// Check request headers
checkHeaderTypeLength(res.getRequestHeaders(), "multipart/form-data" + "; boundary=" + boundaryString, expectedPostBody.length);
// Check post body from the result query string
checkArraysHaveSameContent(expectedPostBody, res.getQueryString().getBytes(contentEncoding));
// Find the data sent to the mirror server, which the mirror server is sending back to us
String dataSentToMirrorServer = new String(res.getResponseData(), contentEncoding);
int posDividerHeadersAndBody = getPositionOfBody(dataSentToMirrorServer);
String headersSent = null;
String bodySent = null;
if(posDividerHeadersAndBody >= 0) {
headersSent = dataSentToMirrorServer.substring(0, posDividerHeadersAndBody);
// Skip the blank line with crlf dividing headers and body
bodySent = dataSentToMirrorServer.substring(posDividerHeadersAndBody+2);
}
else {
fail("No header and body section found");
}
// Check response headers
checkHeaderTypeLength(headersSent, "multipart/form-data" + "; boundary=" + boundaryString, expectedPostBody.length);
// Check post body which was sent to the mirror server, and
// sent back by the mirror server
checkArraysHaveSameContent(expectedPostBody, bodySent.getBytes(contentEncoding));
// Check method, path and query sent
checkMethodPathQuery(headersSent, sampler.getMethod(), sampler.getPath(), null);
}
private void checkPostRequestFileUpload(
HTTPSamplerBase sampler,
HTTPSampleResult res,
String samplerDefaultEncoding,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -