📄 testhttpsamplersagainsthttpmirrorserver.java
字号:
return localMatcher.getMatch().group(1);
}
else {
return null;
}
}
private boolean checkRegularExpression(String stringToCheck, String regularExpression) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK);
return localMatcher.contains(stringToCheck, pattern);
}
private int getPositionOfBody(String stringToCheck) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
// The headers and body are divided by a blank line
String regularExpression = "^.$";
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
PatternMatcherInput input = new PatternMatcherInput(stringToCheck);
while(localMatcher.contains(input, pattern)) {
MatchResult match = localMatcher.getMatch();
return match.beginOffset(0);
}
// No divider was found
return -1;
}
private String getBoundaryStringFromContentType(String requestHeaders) {
Perl5Matcher localMatcher = JMeterUtils.getMatcher();
String regularExpression = "^" + HTTPSamplerBase.HEADER_CONTENT_TYPE + ": multipart/form-data; boundary=(.+)$";
Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
if(localMatcher.contains(requestHeaders, pattern)) {
MatchResult match = localMatcher.getMatch();
return match.group(1);
}
else {
return null;
}
}
private void setupUrl(HTTPSamplerBase sampler, String contentEncoding) {
String protocol = "http";
// String domain = "localhost";
String domain = "localhost";
String path = "/test/somescript.jsp";
int port = 8080;
sampler.setProtocol(protocol);
sampler.setMethod(HTTPSamplerBase.POST);
sampler.setPath(path);
sampler.setDomain(domain);
sampler.setPort(port);
sampler.setContentEncoding(contentEncoding);
}
/**
* Setup the form data with specified values
*
* @param httpSampler
*/
private void setupFormData(HTTPSamplerBase httpSampler, boolean isEncoded, String titleField, String titleValue, String descriptionField, String descriptionValue) {
if(isEncoded) {
httpSampler.addEncodedArgument(titleField, titleValue);
httpSampler.addEncodedArgument(descriptionField, descriptionValue);
}
else {
httpSampler.addArgument(titleField, titleValue);
httpSampler.addArgument(descriptionField, descriptionValue);
}
}
/**
* Setup the form data with specified values, and file to upload
*
* @param httpSampler
*/
private void setupFileUploadData(
HTTPSamplerBase httpSampler,
boolean isEncoded,
String titleField,
String titleValue,
String descriptionField,
String descriptionValue,
String fileField,
File fileValue,
String fileMimeType) {
// Set the form data
setupFormData(httpSampler, isEncoded, titleField, titleValue, descriptionField, descriptionValue);
// Set the file upload data
httpSampler.setFileField(fileField);
httpSampler.setFilename(fileValue.getAbsolutePath());
httpSampler.setMimetype(fileMimeType);
}
/**
* Check that the the two byte arrays have identical content
*
* @param expected
* @param actual
* @throws UnsupportedEncodingException
*/
private void checkArraysHaveSameContent(byte[] expected, byte[] actual) throws UnsupportedEncodingException {
if(expected != null && actual != null) {
if(expected.length != actual.length) {
System.out.println(">>>>>>>>>>>>>>>>>>>>");
System.out.println(new String(expected,"UTF-8"));
System.out.println("====================");
System.out.println(new String(actual,"UTF-8"));
System.out.println("<<<<<<<<<<<<<<<<<<<<");
fail("arrays have different length, expected is " + expected.length + ", actual is " + actual.length);
}
else {
for(int i = 0; i < expected.length; i++) {
if(expected[i] != actual[i]) {
System.out.println(">>>>>>>>>>>>>>>>>>>>");
System.out.println(new String(expected,0,i+1));
System.out.println("====================");
System.out.println(new String(actual,0,i+1));
System.out.println("<<<<<<<<<<<<<<<<<<<<");
/*
// Useful to when debugging
for(int j = 0; j < expected.length; j++) {
System.out.print(expected[j] + " ");
}
System.out.println();
for(int j = 0; j < actual.length; j++) {
System.out.print(actual[j] + " ");
}
System.out.println();
*/
fail("byte at position " + i + " is different, expected is " + expected[i] + ", actual is " + actual[i]);
}
}
}
}
else {
fail("expected or actual byte arrays were null");
}
}
/**
* Create the expected output multipart/form-data, with only form data,
* and no file multipart.
* This method is copied from the PostWriterTest class
*
* @param lastMultipart true if this is the last multipart in the request
*/
private byte[] createExpectedFormdataOutput(
String boundaryString,
String contentEncoding,
String titleField,
String titleValue,
String descriptionField,
String descriptionValue,
boolean firstMultipart,
boolean lastMultipart) throws IOException {
// The encoding used for http headers and control information
final byte[] DASH_DASH = new String("--").getBytes(ISO_8859_1);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
if(firstMultipart) {
output.write(DASH_DASH);
output.write(boundaryString.getBytes(ISO_8859_1));
output.write(CRLF);
}
output.write("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1));
output.write(titleField.getBytes(ISO_8859_1));
output.write("\"".getBytes(ISO_8859_1));
output.write(CRLF);
output.write("Content-Type: text/plain".getBytes(ISO_8859_1));
if(contentEncoding != null) {
output.write("; charset=".getBytes(ISO_8859_1));
output.write(contentEncoding.getBytes(ISO_8859_1));
}
output.write(CRLF);
output.write("Content-Transfer-Encoding: 8bit".getBytes(ISO_8859_1));
output.write(CRLF);
output.write(CRLF);
if(contentEncoding != null) {
output.write(titleValue.getBytes(contentEncoding));
}
else {
output.write(titleValue.getBytes());
}
output.write(CRLF);
output.write(DASH_DASH);
output.write(boundaryString.getBytes(ISO_8859_1));
output.write(CRLF);
output.write("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1));
output.write(descriptionField.getBytes(ISO_8859_1));
output.write("\"".getBytes(ISO_8859_1));
output.write(CRLF);
output.write("Content-Type: text/plain".getBytes(ISO_8859_1));
if(contentEncoding != null) {
output.write("; charset=".getBytes(ISO_8859_1));
output.write(contentEncoding.getBytes(ISO_8859_1));
}
output.write(CRLF);
output.write("Content-Transfer-Encoding: 8bit".getBytes(ISO_8859_1));
output.write(CRLF);
output.write(CRLF);
if(contentEncoding != null) {
output.write(descriptionValue.getBytes(contentEncoding));
}
else {
output.write(descriptionValue.getBytes());
}
output.write(CRLF);
output.write(DASH_DASH);
output.write(boundaryString.getBytes(ISO_8859_1));
if(lastMultipart) {
output.write(DASH_DASH);
}
output.write(CRLF);
output.flush();
output.close();
return output.toByteArray();
}
/**
* Create the expected file multipart
*
* @param lastMultipart true if this is the last multipart in the request
*/
private byte[] createExpectedFilepartOutput(
String boundaryString,
String fileField,
File file,
String mimeType,
byte[] fileContent,
boolean firstMultipart,
boolean lastMultipart) throws IOException {
final byte[] DASH_DASH = new String("--").getBytes(ISO_8859_1);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
if(firstMultipart) {
output.write(DASH_DASH);
output.write(boundaryString.getBytes(ISO_8859_1));
output.write(CRLF);
}
// replace all backslash with double backslash
String filename = file.getName();
output.write("Content-Disposition: form-data; name=\"".getBytes(ISO_8859_1));
output.write(fileField.getBytes(ISO_8859_1));
output.write(("\"; filename=\"" + filename + "\"").getBytes(ISO_8859_1));
output.write(CRLF);
output.write("Content-Type: ".getBytes(ISO_8859_1));
output.write(mimeType.getBytes(ISO_8859_1));
output.write(CRLF);
output.write("Content-Transfer-Encoding: binary".getBytes(ISO_8859_1));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -