📄 testhttprequesthdr.java
字号:
// A HTTP POST request, multipart/form-data, simple values,
// with \r\n as end of line, which is according to spec,
// and with more headers in each multipart
endOfLine = "\r\n";
titleValue = "mytitle";
descriptionValue = "mydescription";
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine);
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine);
s = getSamplerForRequest(url, testPostRequest, contentEncoding);
assertEquals(HTTPSamplerBase.POST, s.getMethod());
assertEquals(contentEncoding, s.getContentEncoding());
assertTrue(s.getDoMultipartPost());
// Check arguments
arguments = s.getArguments();
assertEquals(2, arguments.getArgumentCount());
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false);
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false);
// A HTTP POST request, multipart/form-data, simple values,
// with \n as end of line, which should also be handled,
// and with more headers in each multipart
endOfLine = "\n";
titleValue = "mytitle";
descriptionValue = "mydescription";
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine);
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine);
s = getSamplerForRequest(url, testPostRequest, contentEncoding);
assertEquals(HTTPSamplerBase.POST, s.getMethod());
assertEquals(contentEncoding, s.getContentEncoding());
assertTrue(s.getDoMultipartPost());
// Check arguments
arguments = s.getArguments();
assertEquals(2, arguments.getArgumentCount());
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false);
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false);
// A HTTP POST request, multipart/form-data, with value that will change
// if they are url encoded
// Values are similar to __VIEWSTATE parameter that .net uses
endOfLine = "\r\n";
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
descriptionValue = "mydescription";
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine);
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine);
s = getSamplerForRequest(url, testPostRequest, contentEncoding);
assertEquals(HTTPSamplerBase.POST, s.getMethod());
assertEquals(contentEncoding, s.getContentEncoding());
assertTrue(s.getDoMultipartPost());
// Check arguments
arguments = s.getArguments();
assertEquals(2, arguments.getArgumentCount());
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false);
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false);
}
public void testPostMultipartFileUpload() throws Exception {
String url = "http://localhost/matrix.html";
// A HTTP POST request, multipart/form-data, simple values,
String contentEncoding = "UTF-8";
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB";
String endOfLine = "\r\n";
String fileFieldValue = "test_file";
String fileName = "somefilename.txt";
String mimeType = "text/plain";
String fileContent = "somedummycontent\n\ndfgdfg\r\nfgdgdg\nContent-type:dfsfsfds";
String postBody = createMultipartFileUploadBody(fileFieldValue, fileName, mimeType, fileContent, boundary, endOfLine);
String testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine);
HTTPSamplerBase s = getSamplerForRequest(url, testPostRequest, contentEncoding);
assertEquals(HTTPSamplerBase.POST, s.getMethod());
assertEquals(contentEncoding, s.getContentEncoding());
assertEquals("", s.getQueryString());
assertTrue(s.getDoMultipartPost());
// Check arguments
Arguments arguments = s.getArguments();
assertEquals(0, arguments.getArgumentCount());
assertEquals(fileFieldValue, s.getFileField());
assertEquals(fileName, s.getFilename());
assertEquals(mimeType, s.getMimetype());
}
private String createMultipartFormBody(String titleValue, String descriptionValue, String contentEncoding, boolean includeExtraHeaders, String boundary, String endOfLine) {
// Title multipart
String postBody = "--" + boundary + endOfLine
+ "Content-Disposition: form-data; name=\"title\"" + endOfLine;
if(includeExtraHeaders) {
postBody += "Content-Type: text/plain; charset=" + contentEncoding + endOfLine
+ "Content-Transfer-Encoding: 8bit" + endOfLine;
}
postBody += endOfLine
+ titleValue + endOfLine
+ "--" + boundary + endOfLine;
// Description multipart
postBody += "Content-Disposition: form-data; name=\"description\"" + endOfLine;
if(includeExtraHeaders) {
postBody += "Content-Type: text/plain; charset=" + contentEncoding + endOfLine
+ "Content-Transfer-Encoding: 8bit" + endOfLine;
}
postBody += endOfLine
+ descriptionValue + endOfLine
+ "--" + boundary + "--" + endOfLine;
return postBody;
}
private String createMultipartFileUploadBody(String fileField, String fileName, String fileMimeType, String fileContent, String boundary, String endOfLine) {
// File upload multipart
String postBody = "--" + boundary + endOfLine
+ "Content-Disposition: form-data; name=\"" + fileField + "\" filename=\"" + fileName + "\"" + endOfLine
+ "Content-Type: " + fileMimeType + endOfLine
+ "Content-Transfer-Encoding: binary" + endOfLine
+ endOfLine
+ fileContent + endOfLine
+ "--" + boundary + "--" + endOfLine;
return postBody;
}
private String createMultipartFormRequest(String url, String postBody, String contentEncoding, String boundary, String endOfLine)
throws IOException {
String postRequest = "POST " + url + " HTTP/1.1" + endOfLine
+ "Content-type: "
+ HTTPSamplerBase.MULTIPART_FORM_DATA
+ "; boundary=" + boundary + endOfLine
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + endOfLine
+ endOfLine
+ postBody;
return postRequest;
}
private HTTPSamplerBase getSamplerForRequest(String url, String request, String contentEncoding)
throws IOException {
HttpRequestHdr req = new HttpRequestHdr();
ByteArrayInputStream bis = null;
if(contentEncoding != null) {
bis = new ByteArrayInputStream(request.getBytes(contentEncoding));
}
else {
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
bis = new ByteArrayInputStream(request.getBytes("ISO-8859-1"));
}
req.parse(bis);
bis.close();
Map pageEncodings = Collections.synchronizedMap(new HashMap());
Map formEncodings = Collections.synchronizedMap(new HashMap());
if(url != null && contentEncoding != null) {
pageEncodings.put(url, contentEncoding);
}
return req.getSampler(pageEncodings, formEncodings);
}
private void checkArgument(
HTTPArgument arg,
String expectedName,
String expectedValue,
String expectedEncodedValue,
String contentEncoding,
boolean expectedEncoded) throws IOException {
assertEquals(expectedName, arg.getName());
// System.out.println("expect " + URLEncoder.encode(expectedValue, "UTF-8"));
// System.out.println("actual " + URLEncoder.encode(arg.getValue(), "UTF-8"));
assertEquals(expectedValue, arg.getValue());
if(contentEncoding != null && contentEncoding.length() > 0) {
assertEquals(expectedEncodedValue, arg.getEncodedValue(contentEncoding));
}
else {
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
assertEquals(expectedEncodedValue, arg.getEncodedValue("ISO-8859-1"));
}
assertEquals(expectedEncoded, arg.isAlwaysEncoded());
}
private int getBodyLength(String postBody, String contentEncoding) throws IOException {
if(contentEncoding != null && contentEncoding.length() > 0) {
return postBody.getBytes(contentEncoding).length;
}
else {
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8
return postBody.getBytes().length;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -