📄 postwritertest.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.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.http.util.HTTPArgument;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
public class PostWriterTest extends TestCase {
private static final Logger log = LoggingManager.getLoggerForClass();
private static final String UTF_8 = "UTF-8";
private final static String HTTP_ENCODING = "ISO-8859-1";
private final static byte[] CRLF = { 0x0d, 0x0A };
private static byte[] TEST_FILE_CONTENT;
private StubURLConnection connection;
private HTTPSampler sampler;
private File temporaryFile;
PostWriter postWriter;
protected void setUp() throws Exception {
establishConnection();
sampler = new HTTPSampler();// This must be the original (Java) HTTP sampler
postWriter=new PostWriter();
// Create the test file content
TEST_FILE_CONTENT = "foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052".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("foo", "txt");
OutputStream output = new FileOutputStream(temporaryFile);
output.write(TEST_FILE_CONTENT);
output.flush();
output.close();
}
protected void tearDown() throws Exception {
// delete temporay file
temporaryFile.delete();
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.sendPostData(URLConnection, HTTPSampler)'
* This method test sending a request which contains both formdata and file content
*/
public void testSendPostData() throws IOException {
sampler.setMethod(HTTPSamplerBase.POST);
setupFilepart(sampler);
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);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
byte[] expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, null, titleValue, descriptionValue, TEST_FILE_CONTENT);
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 = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
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 = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending UTF-8 data with ISO-8859-1 content encoding
establishConnection();
contentEncoding = UTF_8;
sampler.setContentEncoding("ISO-8859-1");
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveDifferentContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.sendPostData(URLConnection, HTTPSampler)'
* This method test sending a HTTPSampler with form parameters, and only
* the filename of a file.
*/
public void testSendPostData_NoFilename() throws IOException {
setupNoFilename(sampler);
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=mytitle&description=mydescription".getBytes();
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
expectedUrl = "title=mytitle&description=mydescription".getBytes(UTF_8);
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, 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);
checkContentTypeUrlEncoded(connection);
expectedUrl = "title=mytitle&description=mydescription".getBytes(contentEncoding);
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
expectedUrl = "title=mytitle&description=mydescription".getBytes(UTF_8);
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
connection.disconnect();
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.sendPostData(URLConnection, HTTPSampler)'
* This method test sending file content as the only content of the post body
*/
public void testSendPostData_FileAsBody() throws IOException {
setupFilepart(sampler, "", temporaryFile, "");
// Check using default encoding
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentLength(connection, TEST_FILE_CONTENT.length);
checkArraysHaveSameContent(TEST_FILE_CONTENT, connection.getOutputStreamContent());
connection.disconnect();
// Check using a different encoding
String otherEncoding;
final String fileEncoding = System.getProperty( "file.encoding");// $NON-NLS-1$
log.info("file.encoding: "+fileEncoding);
if (UTF_8.equalsIgnoreCase(fileEncoding) || "UTF8".equalsIgnoreCase(fileEncoding)){// $NON-NLS-1$
otherEncoding="ISO-8859-1"; // $NON-NLS-1$
} else {
otherEncoding=UTF_8;
}
log.info("Using other encoding: "+otherEncoding);
establishConnection();
sampler.setContentEncoding(otherEncoding);
// File content is sent as binary, so the content encoding should not change the file data
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentLength(connection, TEST_FILE_CONTENT.length);
checkArraysHaveSameContent(TEST_FILE_CONTENT, connection.getOutputStreamContent());
// Check that other encoding is not the current encoding
checkArraysHaveDifferentContent(new String(TEST_FILE_CONTENT).getBytes(otherEncoding), connection.getOutputStreamContent());
// If we have both file as body, and form data, then only form data will be sent
setupFormData(sampler);
establishConnection();
sampler.setContentEncoding("");
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeUrlEncoded(connection);
byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes();
checkContentLength(connection, expectedUrl.length);
checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
}
/*
* Test method for 'org.apache.jmeter.protocol.http.sampler.postWriter.sendPostData(URLConnection, HTTPSampler)'
* This method test sending only a file multipart.
*/
public void testSendFileData_Multipart() throws IOException {
sampler.setMethod(HTTPSamplerBase.POST);
String fileField = "upload";
String mimeType = "text/plain";
File file = temporaryFile;
byte[] fileContent = TEST_FILE_CONTENT;
setupFilepart(sampler, fileField, file, mimeType);
// Test sending data with default encoding
String contentEncoding = "";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
byte[] expectedFormBody = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, 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 = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, true, true);
checkContentLength(connection, expectedFormBody.length);
checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
connection.disconnect();
// Test sending data as UTF-8
establishConnection();
fileField = "some_file_field";
mimeType = "image/png";
contentEncoding = UTF_8;
sampler.setContentEncoding(contentEncoding);
setupFilepart(sampler, fileField, file, mimeType);
postWriter.setHeaders(connection, sampler);
postWriter.sendPostData(connection, sampler);
checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
expectedFormBody = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, 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 a multipart/form-data request.
*/
public void testSendFormData_Multipart() throws IOException {
sampler.setMethod(HTTPSamplerBase.POST);
String titleField = "title";
String titleValue = "mytitle";
String descriptionField = "description";
String descriptionValue = "mydescription";
setupFormData(sampler, titleValue, descriptionValue);
// Tell sampler to do multipart, even if we have no files to upload
sampler.setDoMultipartPost(true);
// Test sending data with default encoding
String contentEncoding = "";
sampler.setContentEncoding(contentEncoding);
postWriter.setHeaders(connection, sampler);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -