Lines 20-25
Link Here
|
20 |
|
20 |
|
21 |
import java.io.ByteArrayInputStream; |
21 |
import java.io.ByteArrayInputStream; |
22 |
import java.io.IOException; |
22 |
import java.io.IOException; |
|
|
23 |
import java.net.URLEncoder; |
23 |
|
24 |
|
24 |
import org.apache.jmeter.config.Arguments; |
25 |
import org.apache.jmeter.config.Arguments; |
25 |
import org.apache.jmeter.junit.JMeterTestCase; |
26 |
import org.apache.jmeter.junit.JMeterTestCase; |
Lines 43-89
Link Here
|
43 |
// Check arguments |
44 |
// Check arguments |
44 |
Arguments arguments = s.getArguments(); |
45 |
Arguments arguments = s.getArguments(); |
45 |
assertEquals(13, arguments.getArgumentCount()); |
46 |
assertEquals(13, arguments.getArgumentCount()); |
46 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false); |
47 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", "UTF-8", false); |
47 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false); |
48 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", "UTF-8", false); |
48 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false); |
49 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", "UTF-8", false); |
49 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false); |
50 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", "UTF-8", false); |
50 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false); |
51 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", "UTF-8", false); |
51 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false); |
52 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", "UTF-8", false); |
52 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false); |
53 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", "UTF-8", false); |
53 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false); |
54 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", "UTF-8", false); |
54 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false); |
55 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", "UTF-8", false); |
55 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false); |
56 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", "UTF-8", false); |
56 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false); |
57 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", "UTF-8", false); |
57 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false); |
58 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", "UTF-8", false); |
58 |
// I see that the value gets trimmed, not sure if that is correct |
59 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", "UTF-8", false); |
59 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", false); |
|
|
60 |
|
60 |
|
61 |
// A HTTP POST request |
61 |
// A HTTP POST request |
62 |
String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= "; |
62 |
String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d="; |
63 |
String TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
63 |
String TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
64 |
+ "Content-type: " |
64 |
+ "Content-type: " |
65 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
65 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
66 |
+ "; charset=UTF-8\n" |
66 |
+ "; charset=UTF-8\r\n" |
|
|
67 |
+ "Content-length: " + postBody.length() + "\r\n" |
68 |
+ "\r\n" |
67 |
+ postBody; |
69 |
+ postBody; |
68 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
70 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
69 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
71 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
72 |
assertFalse(s.getDoMultipartPost()); |
70 |
|
73 |
|
71 |
// Check arguments |
74 |
// Check arguments |
72 |
arguments = s.getArguments(); |
75 |
arguments = s.getArguments(); |
73 |
assertEquals(13, arguments.getArgumentCount()); |
76 |
assertEquals(13, arguments.getArgumentCount()); |
74 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false); |
77 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", "UTF-8", false); |
75 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false); |
78 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", "UTF-8", false); |
76 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false); |
79 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", "UTF-8", false); |
77 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false); |
80 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", "UTF-8", false); |
78 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false); |
81 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", "UTF-8", false); |
79 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false); |
82 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", "UTF-8", false); |
80 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false); |
83 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", "UTF-8", false); |
81 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false); |
84 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", "UTF-8", false); |
82 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false); |
85 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", "UTF-8", false); |
83 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false); |
86 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", "UTF-8", false); |
84 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false); |
87 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", "UTF-8", false); |
85 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false); |
88 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", "UTF-8", false); |
86 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", " ", false); |
89 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", "UTF-8", false); |
87 |
|
90 |
|
88 |
// A HTTP POST request, with content-type text/plain |
91 |
// A HTTP POST request, with content-type text/plain |
89 |
TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
92 |
TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
Lines 91-102
Link Here
|
91 |
+ postBody; |
94 |
+ postBody; |
92 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
95 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
93 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
96 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
97 |
assertFalse(s.getDoMultipartPost()); |
94 |
|
98 |
|
95 |
// Check arguments |
99 |
// Check arguments |
96 |
// We should have one argument, with the value equal to the post body |
100 |
// We should have one argument, with the value equal to the post body |
97 |
arguments = s.getArguments(); |
101 |
arguments = s.getArguments(); |
98 |
assertEquals(1, arguments.getArgumentCount()); |
102 |
assertEquals(1, arguments.getArgumentCount()); |
99 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, false); |
103 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, postBody, "UTF-8", false); |
100 |
} |
104 |
} |
101 |
|
105 |
|
102 |
// TODO: will need changing if arguments can be saved in decoded form |
106 |
// TODO: will need changing if arguments can be saved in decoded form |
Lines 114-122
Link Here
|
114 |
// Check arguments |
118 |
// Check arguments |
115 |
Arguments arguments = s.getArguments(); |
119 |
Arguments arguments = s.getArguments(); |
116 |
assertEquals(3, arguments.getArgumentCount()); |
120 |
assertEquals(3, arguments.getArgumentCount()); |
117 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false); |
121 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", "a+b", "UTF-8", false); |
118 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false); |
122 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", "UTF-8", false); |
119 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false); |
123 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What?", "UTF-8", false); |
120 |
|
124 |
|
121 |
// A HTTP POST request |
125 |
// A HTTP POST request |
122 |
String postBody = "abc?SPACE=a+b&space=a%20b&query=What?"; |
126 |
String postBody = "abc?SPACE=a+b&space=a%20b&query=What?"; |
Lines 127-141
Link Here
|
127 |
+ postBody; |
131 |
+ postBody; |
128 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
132 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
129 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
133 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
134 |
assertFalse(s.getDoMultipartPost()); |
130 |
|
135 |
|
131 |
// Check arguments |
136 |
// Check arguments |
132 |
arguments = s.getArguments(); |
137 |
arguments = s.getArguments(); |
133 |
assertEquals(3, arguments.getArgumentCount()); |
138 |
assertEquals(3, arguments.getArgumentCount()); |
134 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false); |
139 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", "a+b", "UTF-8", false); |
135 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false); |
140 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", "UTF-8", false); |
136 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false); |
141 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What?", "UTF-8", false); |
137 |
} |
142 |
} |
|
|
143 |
|
144 |
public void testPostMultipartFormData() throws Exception { |
145 |
// A HTTP POST request, multipart/form-data, simple values, |
146 |
String contentEncoding = "UTF-8"; |
147 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
148 |
String endOfLine = "\r\n"; |
149 |
String titleValue = "mytitle"; |
150 |
String descriptionValue = "mydescription"; |
151 |
String postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
152 |
String testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
138 |
|
153 |
|
|
|
154 |
HTTPSamplerBase s = getSamplerForRequest(testPostRequest, "UTF-8"); |
155 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
156 |
assertTrue(s.getDoMultipartPost()); |
157 |
|
158 |
// Check arguments |
159 |
Arguments arguments = s.getArguments(); |
160 |
assertEquals(2, arguments.getArgumentCount()); |
161 |
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false); |
162 |
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false); |
163 |
|
164 |
// A HTTP POST request, multipart/form-data, simple values, |
165 |
// with \r\n as end of line, which is according to spec, |
166 |
// and with more headers in each multipart |
167 |
endOfLine = "\r\n"; |
168 |
titleValue = "mytitle"; |
169 |
descriptionValue = "mydescription"; |
170 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
171 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
172 |
|
173 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
174 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
175 |
assertTrue(s.getDoMultipartPost()); |
176 |
|
177 |
// Check arguments |
178 |
arguments = s.getArguments(); |
179 |
assertEquals(2, arguments.getArgumentCount()); |
180 |
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false); |
181 |
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false); |
182 |
|
183 |
// A HTTP POST request, multipart/form-data, simple values, |
184 |
// with \n as end of line, which should also be handled, |
185 |
// and with more headers in each multipart |
186 |
endOfLine = "\n"; |
187 |
titleValue = "mytitle"; |
188 |
descriptionValue = "mydescription"; |
189 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
190 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
191 |
|
192 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
193 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
194 |
assertTrue(s.getDoMultipartPost()); |
195 |
|
196 |
// Check arguments |
197 |
arguments = s.getArguments(); |
198 |
assertEquals(2, arguments.getArgumentCount()); |
199 |
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false); |
200 |
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false); |
201 |
|
202 |
// A HTTP POST request, multipart/form-data, with value that will change |
203 |
// if they are url encoded |
204 |
// Values are similar to __VIEWSTATE parameter that .net uses |
205 |
endOfLine = "\r\n"; |
206 |
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc="; |
207 |
descriptionValue = "mydescription"; |
208 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
209 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
210 |
|
211 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
212 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
213 |
assertTrue(s.getDoMultipartPost()); |
214 |
|
215 |
// Check arguments |
216 |
arguments = s.getArguments(); |
217 |
assertEquals(2, arguments.getArgumentCount()); |
218 |
checkArgument((HTTPArgument)arguments.getArgument(0), "title", titleValue, titleValue, contentEncoding, false); |
219 |
checkArgument((HTTPArgument)arguments.getArgument(1), "description", descriptionValue, descriptionValue, contentEncoding, false); |
220 |
} |
221 |
|
222 |
public void testPostMultipartFileUpload() throws Exception { |
223 |
// A HTTP POST request, multipart/form-data, simple values, |
224 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
225 |
String endOfLine = "\r\n"; |
226 |
String fileFieldValue = "test_file"; |
227 |
String fileName = "somefilename.txt"; |
228 |
String mimeType = "text/plain"; |
229 |
String fileContent = "somedummycontent\n\ndfgdfg\r\nfgdgdg\nContent-type:dfsfsfds"; |
230 |
String postBody = createMultipartFileUploadBody(fileFieldValue, fileName, mimeType, fileContent, boundary, endOfLine); |
231 |
String testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
232 |
|
233 |
HTTPSamplerBase s = getSamplerForRequest(testPostRequest, "UTF-8"); |
234 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
235 |
assertTrue(s.getDoMultipartPost()); |
236 |
|
237 |
// Check arguments |
238 |
Arguments arguments = s.getArguments(); |
239 |
assertEquals(0, arguments.getArgumentCount()); |
240 |
assertEquals(fileFieldValue, s.getFileField()); |
241 |
assertEquals(fileName, s.getFilename()); |
242 |
assertEquals(mimeType, s.getMimetype()); |
243 |
} |
244 |
|
245 |
private String createMultipartFormBody(String titleValue, String descriptionValue, String contentEncoding, boolean includeExtraHeaders, String boundary, String endOfLine) { |
246 |
// Title multipart |
247 |
String postBody = "--" + boundary + endOfLine |
248 |
+ "Content-Disposition: form-data; name=\"title\"" + endOfLine; |
249 |
if(includeExtraHeaders) { |
250 |
postBody += "Content-Type: text/plain; charset=" + contentEncoding + endOfLine |
251 |
+ "Content-Transfer-Encoding: 8bit" + endOfLine; |
252 |
} |
253 |
postBody += endOfLine |
254 |
+ titleValue + endOfLine |
255 |
+ "--" + boundary + endOfLine; |
256 |
// Description multipart |
257 |
postBody += "Content-Disposition: form-data; name=\"description\"" + endOfLine; |
258 |
if(includeExtraHeaders) { |
259 |
postBody += "Content-Type: text/plain; charset=" + contentEncoding + endOfLine |
260 |
+ "Content-Transfer-Encoding: 8bit" + endOfLine; |
261 |
} |
262 |
postBody += endOfLine |
263 |
+ descriptionValue + endOfLine |
264 |
+ "--" + boundary + "--" + endOfLine; |
265 |
|
266 |
return postBody; |
267 |
} |
268 |
|
269 |
private String createMultipartFileUploadBody(String fileField, String fileName, String fileMimeType, String fileContent, String boundary, String endOfLine) { |
270 |
// File upload multipart |
271 |
String postBody = "--" + boundary + endOfLine |
272 |
+ "Content-Disposition: form-data; name=\"" + fileField + "\" filename=\"" + fileName + "\"" + endOfLine |
273 |
+ "Content-Type: " + fileMimeType + endOfLine |
274 |
+ "Content-Transfer-Encoding: binary" + endOfLine |
275 |
+ endOfLine |
276 |
+ fileContent + endOfLine |
277 |
+ "--" + boundary + "--" + endOfLine; |
278 |
return postBody; |
279 |
} |
280 |
|
281 |
private String createMultipartFormRequest(String postBody, String boundary, String endOfLine) { |
282 |
String postRequest = "POST http://localhost:80/matrix.html HTTP/1.1" + endOfLine |
283 |
+ "Content-type: " |
284 |
+ HTTPSamplerBase.MULTIPART_FORM_DATA |
285 |
+ "; boundary=" + boundary + endOfLine |
286 |
+ "Content-length: " + postBody.length() + endOfLine |
287 |
+ endOfLine |
288 |
+ postBody; |
289 |
return postRequest; |
290 |
} |
291 |
|
139 |
private HTTPSamplerBase getSamplerForRequest(String request, String contentEncoding) |
292 |
private HTTPSamplerBase getSamplerForRequest(String request, String contentEncoding) |
140 |
throws IOException { |
293 |
throws IOException { |
141 |
HttpRequestHdr req = new HttpRequestHdr(); |
294 |
HttpRequestHdr req = new HttpRequestHdr(); |
Lines 149-157
Link Here
|
149 |
HTTPArgument arg, |
302 |
HTTPArgument arg, |
150 |
String expectedName, |
303 |
String expectedName, |
151 |
String expectedValue, |
304 |
String expectedValue, |
152 |
boolean expectedEncoded) { |
305 |
String expectedEncodedValue, |
|
|
306 |
String contentEncoding, |
307 |
boolean expectedEncoded) throws IOException { |
153 |
assertEquals(expectedName, arg.getName()); |
308 |
assertEquals(expectedName, arg.getName()); |
154 |
assertEquals(expectedValue, arg.getValue()); |
309 |
assertEquals(expectedValue, arg.getValue()); |
|
|
310 |
assertEquals(expectedEncodedValue, arg.getEncodedValue(contentEncoding)); |
155 |
assertEquals(expectedEncoded, arg.isAlwaysEncoded()); |
311 |
assertEquals(expectedEncoded, arg.isAlwaysEncoded()); |
156 |
} |
312 |
} |
157 |
} |
313 |
} |