📄 postwritertest.java
字号:
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
byte[] expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, null, titleField, titleValue, descriptionField, descriptionValue, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as ISO-8859-1
establishConnection();
contentEncoding = "ISO-8859-1";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as ISO-8859-1, with values that need to be urlencoded
establishConnection();
titleValue = "mytitle+123 456&yes";
descriptionValue = "mydescription and some spaces";
contentEncoding = "ISO-8859-1";
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as UTF-8
establishConnection();
titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as UTF-8, with values that would have been urlencoded
// if it was not sent as multipart
establishConnection();
titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.sendPostData(URLConnection, HTTPSampler)'
* This method test sending only a formdata, as urlencoded data
*/
public void testSendFormData_Urlencoded() throws IOException {
String titleValue = "mytitle";
String descriptionValue = "mydescription";
setupFormData(sampler, titleValue, descriptionValue);
// Test sending data with default encoding
String contentEncoding = "";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
byte[] expectedUrl = ("title=" + titleValue + "&description=" + descriptionValue).getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), "ISO-8859-1"),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), "ISO-8859-1"));
connection.disconnect();
// Test sending data as ISO-8859-1
establishConnection();
contentEncoding = "ISO-8859-1";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedUrl = new String("title=" + titleValue + "&description=" + descriptionValue).getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
connection.disconnect();
// Test sending data as ISO-8859-1, with values that need to be urlencoded
establishConnection();
titleValue = "mytitle+123 456&yes";
descriptionValue = "mydescription and some spaces";
contentEncoding = "ISO-8859-1";
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
String expectedString = "title=" + URLEncoder.encode(titleValue, contentEncoding) + "&description=" + URLEncoder.encode(descriptionValue, contentEncoding);
expectedUrl = expectedString.getBytes(contentEncoding);
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
String unencodedString = "title=" + titleValue + "&description=" + descriptionValue;
byte[] unexpectedUrl = unencodedString.getBytes(UTF_8);
checkArraysHaveDifferentContent(unexpectedUrl, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as UTF-8
establishConnection();
titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedString = "title=" + URLEncoder.encode(titleValue, contentEncoding) + "&description=" + URLEncoder.encode(descriptionValue, contentEncoding);
expectedUrl = expectedString.getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
connection.disconnect();
// Test sending data as UTF-8, with values that needs to be urlencoded
establishConnection();
titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
setupFormData(sampler, titleValue, descriptionValue);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedString = "title=" + URLEncoder.encode(titleValue, UTF_8) + "&description=" + URLEncoder.encode(descriptionValue, UTF_8);
expectedUrl = expectedString.getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
unencodedString = "title=" + titleValue + "&description=" + descriptionValue;
unexpectedUrl = unencodedString.getBytes("US-ASCII");
checkArraysHaveDifferentContent(unexpectedUrl, connection.getOutputStreamContent());
connection.disconnect();
// Test sending parameters which are urlencoded beforehand
// The values must be URL encoded with UTF-8 encoding, because that
// is what the HTTPArgument assumes
// %C3%85 in UTF-8 is the same as %C5 in ISO-8859-1, which is the same as Å
titleValue = "mytitle%20and%20space%2Ftest%C3%85";
descriptionValue = "mydescription+and+plus+as+space%2Ftest%C3%85";
setupFormData(sampler, true, titleValue, descriptionValue);
// Test sending data with default encoding
establishConnection();
contentEncoding = "";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedUrl = new String("title=" + titleValue.replaceAll("%20", "+").replaceAll("%C3%85", "%C5") + "&description=" + descriptionValue.replaceAll("%C3%85", "%C5")).getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), "ISO-8859-1"), // HTTPSampler uses ISO-8859-1 as default encoding
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), "ISO-8859-1")); // HTTPSampler uses ISO-8859-1 as default encoding
connection.disconnect();
// Test sending data as ISO-8859-1
establishConnection();
contentEncoding = "ISO-8859-1";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedUrl = new String("title=" + titleValue.replaceAll("%20", "+").replaceAll("%C3%85", "%C5") + "&description=" + descriptionValue.replaceAll("%C3%85", "%C5")).getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
connection.disconnect();
// Test sending data as UTF-8
establishConnection();
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
expectedUrl = new String("title=" + titleValue.replaceAll("%20", "+") + "&description=" + descriptionValue).getBytes("US-ASCII");
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
assertEquals(
URLDecoder.decode(new String(expectedUrl, "US-ASCII"), contentEncoding),
URLDecoder.decode(new String(connection.getOutputStreamContent(), "US-ASCII"), contentEncoding));
connection.disconnect();
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.setHeaders(URLConnection, HTTPSampler)'
*/
public void testSetHeaders() throws IOException {
sampler.setMethod(HTTPSamplerBase.POST);
setupFilepart(sampler);
setupFormData(sampler);
postWriter.setHeaders(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.setHeaders(URLConnection, HTTPSampler)'
*/
public void testSetHeaders_NoFilename() throws IOException {
setupNoFilename(sampler);
setupFormData(sampler);
postWriter.setHeaders(connection, sampler);
checkContentTypeUrlEncoded(connection);
checkContentLength(connection, "title=mytitle&description=mydescription".length());
}
/**
* setup commons parts of HTTPSampler with a no filename.
*
* @param httpSampler
* @throws IOException
*/
private void setupNoFilename(HTTPSampler httpSampler) {
setupFilepart(sampler, "upload", null, "application/octet-stream");
}
/**
* Setup the filepart with default values
*
* @param httpSampler
*/
private void setupFilepart(HTTPSampler httpSampler) {
setupFilepart(sampler, "upload", temporaryFile, "text/plain");
}
/**
* Setup the filepart with specified values
*
* @param httpSampler
*/
private void setupFilepart(HTTPSampler httpSampler, String fileField, File file, String mimeType) {
httpSampler.setFileField(fileField);
if(file != null) {
httpSampler.setFilename(file.getAbsolutePath());
}
else {
httpSampler.setFilename("");
}
httpSampler.setMimetype(mimeType);
}
/**
* Setup the form data with default values
*
* @param httpSampler
*/
private void setupFormData(HTTPSampler httpSampler) {
setupFormData(httpSampler, "mytitle", "mydescription");
}
/**
* Setup the form data with specified values
*
* @param httpSampler
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -