📄 testhttpsamplersagainsthttpmirrorserver.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.protocol.http.sampler;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.net.URL;
import java.util.Locale;
import org.apache.jmeter.engine.util.ValueReplacer;
import org.apache.jmeter.protocol.http.control.HttpMirrorControl;
import org.apache.jmeter.protocol.http.util.EncoderCache;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.extensions.TestSetup;
/**
* Class for performing actual samples for HTTPSampler and HTTPSampler2.
* The samples are executed against the HttpMirrorServer, which is
* started when the unit tests are executed.
*/
public class TestHTTPSamplersAgainstHttpMirrorServer extends TestCase {
private final static int HTTP_SAMPLER = 0;
private final static int HTTP_SAMPLER2 = 1;
/** The encodings used for http headers and control information */
private final static String ISO_8859_1 = "ISO-8859-1"; // $NON-NLS-1$
private static final String US_ASCII = "US-ASCII"; // $NON-NLS-1$
private static final byte[] CRLF = { 0x0d, 0x0A };
private static byte[] TEST_FILE_CONTENT;
private static File temporaryFile;
public TestHTTPSamplersAgainstHttpMirrorServer(String arg0) {
super(arg0);
}
public static Test suite(){
TestSetup setup = new TestSetup(new TestSuite(TestHTTPSamplersAgainstHttpMirrorServer.class)){
private int webServerPort = 8080;
private HttpMirrorControl webServerControl;
protected void setUp() throws Exception {
webServerControl = new HttpMirrorControl();
webServerControl.setPort(webServerPort);
webServerControl.startHttpMirror();
// Create the test file content
TEST_FILE_CONTENT = new String("some foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85").getBytes("UTF-8");
// create a temporary file to make sure we always have a file to give to the PostWriter
// Whereever we are or Whatever the current path is.
temporaryFile = File.createTempFile("TestHTTPSamplersAgainstHttpMirrorServer", "tmp");
OutputStream output = new FileOutputStream(temporaryFile);
output.write(TEST_FILE_CONTENT);
output.flush();
output.close();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}// Allow thread chance to fail
if (!webServerControl.isServerAlive()){
throw new Exception("Could not start mirror server");
}
}
protected void tearDown() throws Exception {
// Shutdown web server
webServerControl.stopHttpMirror();
//webServerControl = null;
// delete temporay file
temporaryFile.delete();
}
};
return setup;
};
public void testPostRequest_UrlEncoded() throws Exception {
testPostRequest_UrlEncoded(HTTP_SAMPLER, ISO_8859_1);
}
public void testPostRequest_UrlEncoded2() throws Exception {
testPostRequest_UrlEncoded(HTTP_SAMPLER2, US_ASCII);
}
public void testPostRequest_FormMultipart() throws Exception {
testPostRequest_FormMultipart(HTTP_SAMPLER, ISO_8859_1);
}
public void testPostRequest_FormMultipart2() throws Exception {
testPostRequest_FormMultipart(HTTP_SAMPLER2, US_ASCII);
}
public void testPostRequest_FileUpload() throws Exception {
testPostRequest_FileUpload(HTTP_SAMPLER, ISO_8859_1);
}
public void testPostRequest_FileUpload2() throws Exception {
testPostRequest_FileUpload(HTTP_SAMPLER2, US_ASCII);
}
public void testPostRequest_BodyFromParameterValues() throws Exception {
testPostRequest_BodyFromParameterValues(HTTP_SAMPLER, ISO_8859_1);
}
public void testPostRequest_BodyFromParameterValues2() throws Exception {
testPostRequest_BodyFromParameterValues(HTTP_SAMPLER2, US_ASCII);
}
public void testGetRequest() throws Exception {
testGetRequest(HTTP_SAMPLER);
}
public void testGetRequest2() throws Exception {
testGetRequest(HTTP_SAMPLER2);
}
public void testGetRequest_Parameters() throws Exception {
testGetRequest_Parameters(HTTP_SAMPLER);
}
public void testGetRequest_Parameters2() throws Exception {
testGetRequest_Parameters(HTTP_SAMPLER2);
}
private void testPostRequest_UrlEncoded(int samplerType, String samplerDefaultEncoding) throws Exception {
String titleField = "title";
String titleValue = "mytitle";
String descriptionField = "description";
String descriptionValue = "mydescription";
// Test sending data with default encoding
HTTPSamplerBase sampler = createHttpSampler(samplerType);
String contentEncoding = "";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
HTTPSampleResult res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as ISO-8859-1
sampler = createHttpSampler(samplerType);
contentEncoding = ISO_8859_1;
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as UTF-8
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as UTF-8, with values that will change when urlencoded
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle/=";
descriptionValue = "mydescription /\\";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, 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);
setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true);
// Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
descriptionValue = "mydescription";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, false);
// Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses,
// with values urlencoded, but the always encode set to false for the arguments
// This is how the HTTP Proxy server adds arguments to the sampler
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "%2FwEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ%2FrA%2B8DZ2dnZ2dnZ2d%2FGNDar6OshPwdJc%3D";
descriptionValue = "mydescription";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument)sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument)sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
assertFalse(((HTTPArgument)sampler.getArguments().getArgument(0)).isAlwaysEncoded());
assertFalse(((HTTPArgument)sampler.getArguments().getArgument(1)).isAlwaysEncoded());
checkPostRequestUrlEncoded(sampler, res, samplerDefaultEncoding, contentEncoding, 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);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
// Replace the variables in the sampler
replacer.replaceValues(sampler);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -