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; |
24 |
import java.util.Collections; |
25 |
import java.util.HashMap; |
26 |
import java.util.Map; |
27 |
|
23 |
import org.apache.jmeter.config.Arguments; |
28 |
import org.apache.jmeter.config.Arguments; |
24 |
import org.apache.jmeter.junit.JMeterTestCase; |
29 |
import org.apache.jmeter.junit.JMeterTestCase; |
25 |
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; |
30 |
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase; |
Lines 31-145
Link Here
|
31 |
} |
36 |
} |
32 |
|
37 |
|
33 |
public void testRepeatedArguments() throws Exception { |
38 |
public void testRepeatedArguments() throws Exception { |
|
|
39 |
String url = "http://localhost/matrix.html"; |
34 |
// A HTTP GET request |
40 |
// A HTTP GET request |
35 |
String TEST_GET_REQ = |
41 |
String contentEncoding = "UTF-8"; |
36 |
"GET http://localhost/matrix.html" |
42 |
String testGetRequest = |
|
|
43 |
"GET " + url |
37 |
+ "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= " |
44 |
+ "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= " |
38 |
+ "HTTP/1.0\n\n"; |
45 |
+ "HTTP/1.0\r\n\r\n"; |
39 |
HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8"); |
46 |
HTTPSamplerBase s = getSamplerForRequest(url, testGetRequest, contentEncoding); |
40 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
47 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
41 |
|
48 |
assertEquals(contentEncoding, s.getContentEncoding()); |
42 |
// Check arguments |
49 |
// Check arguments |
43 |
Arguments arguments = s.getArguments(); |
50 |
Arguments arguments = s.getArguments(); |
44 |
assertEquals(13, arguments.getArgumentCount()); |
51 |
assertEquals(13, arguments.getArgumentCount()); |
45 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", "UTF-8", false); |
52 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", contentEncoding, false); |
46 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", "UTF-8", false); |
53 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", contentEncoding, false); |
47 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", "UTF-8", false); |
54 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", contentEncoding, false); |
48 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", "UTF-8", false); |
55 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", contentEncoding, false); |
49 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", "UTF-8", false); |
56 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", contentEncoding, false); |
50 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", "UTF-8", false); |
57 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", contentEncoding, false); |
51 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", "UTF-8", false); |
58 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", contentEncoding, false); |
52 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", "UTF-8", false); |
59 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", contentEncoding, false); |
53 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", "UTF-8", false); |
60 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", contentEncoding, false); |
54 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", "UTF-8", false); |
61 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", contentEncoding, false); |
55 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", "UTF-8", false); |
62 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", contentEncoding, false); |
56 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", "UTF-8", false); |
63 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", contentEncoding, false); |
57 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", "UTF-8", false); |
64 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", contentEncoding, false); |
58 |
|
65 |
|
59 |
// A HTTP POST request |
66 |
// A HTTP POST request |
|
|
67 |
contentEncoding = "UTF-8"; |
60 |
String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d="; |
68 |
String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d="; |
61 |
String TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
69 |
String testPostRequest = "POST " + url + " HTTP/1.0\n" |
62 |
+ "Content-type: " |
70 |
+ "Content-type: " |
63 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
71 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
64 |
+ "; charset=UTF-8\r\n" |
72 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
65 |
+ "Content-length: " + postBody.length() + "\r\n" |
|
|
66 |
+ "\r\n" |
73 |
+ "\r\n" |
67 |
+ postBody; |
74 |
+ postBody; |
68 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
75 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
69 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
76 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
70 |
assertFalse(s.getDoMultipartPost()); |
77 |
assertFalse(s.getDoMultipartPost()); |
71 |
|
78 |
assertEquals(contentEncoding, s.getContentEncoding()); |
72 |
// Check arguments |
79 |
// Check arguments |
73 |
arguments = s.getArguments(); |
80 |
arguments = s.getArguments(); |
74 |
assertEquals(13, arguments.getArgumentCount()); |
81 |
assertEquals(13, arguments.getArgumentCount()); |
75 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", "UTF-8", false); |
82 |
checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", "yes", contentEncoding, false); |
76 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", "UTF-8", false); |
83 |
checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", "1", contentEncoding, false); |
77 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", "UTF-8", false); |
84 |
checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", "2", contentEncoding, false); |
78 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", "UTF-8", false); |
85 |
checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", "", contentEncoding, false); |
79 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", "UTF-8", false); |
86 |
checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", "", contentEncoding, false); |
80 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", "UTF-8", false); |
87 |
checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", "", contentEncoding, false); |
81 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", "UTF-8", false); |
88 |
checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", "", contentEncoding, false); |
82 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", "UTF-8", false); |
89 |
checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", "", contentEncoding, false); |
83 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", "UTF-8", false); |
90 |
checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", "1", contentEncoding, false); |
84 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", "UTF-8", false); |
91 |
checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", "2", contentEncoding, false); |
85 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", "UTF-8", false); |
92 |
checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", "1", contentEncoding, false); |
86 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", "UTF-8", false); |
93 |
checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", "", contentEncoding, false); |
87 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", "UTF-8", false); |
94 |
checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", "", contentEncoding, false); |
88 |
|
95 |
|
89 |
// A HTTP POST request, with content-type text/plain |
96 |
// A HTTP POST request, with content-type text/plain |
90 |
TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n" |
97 |
contentEncoding = "UTF-8"; |
91 |
+ "Content-type: text/plain; charset=UTF-8\n" |
98 |
postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=\uc385&d="; |
|
|
99 |
testPostRequest = "POST " + url + " HTTP/1.1\r\n" |
100 |
+ "Content-type: text/plain\r\n" |
101 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
102 |
+ "\r\n" |
92 |
+ postBody; |
103 |
+ postBody; |
93 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
104 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
94 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
105 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
95 |
assertFalse(s.getDoMultipartPost()); |
106 |
assertFalse(s.getDoMultipartPost()); |
96 |
|
107 |
assertEquals(contentEncoding, s.getContentEncoding()); |
97 |
// Check arguments |
108 |
// Check arguments |
98 |
// We should have one argument, with the value equal to the post body |
109 |
// We should have one argument, with the value equal to the post body |
99 |
arguments = s.getArguments(); |
110 |
arguments = s.getArguments(); |
100 |
assertEquals(1, arguments.getArgumentCount()); |
111 |
assertEquals(1, arguments.getArgumentCount()); |
101 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, postBody, "UTF-8", false); |
112 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, postBody, contentEncoding, false); |
|
|
113 |
|
114 |
// A HTTP POST request, with content-type text/plain; charset=UTF-8 |
115 |
// The encoding should be picked up from the header we send with the request |
116 |
contentEncoding = "UTF-8"; |
117 |
postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=\uc385&d="; |
118 |
testPostRequest = "POST " + url + " HTTP/1.1\r\n" |
119 |
+ "Content-type: text/plain; charset=" + contentEncoding + "\r\n" |
120 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
121 |
+ "\r\n" |
122 |
+ postBody; |
123 |
// Use null for url to simulate that HttpRequestHdr do not |
124 |
// know the encoding for the page. Specify contentEncoding, so the |
125 |
// request is "sent" using that encoding |
126 |
s = getSamplerForRequest(null, testPostRequest, contentEncoding); |
127 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
128 |
assertFalse(s.getDoMultipartPost()); |
129 |
assertEquals(contentEncoding, s.getContentEncoding()); |
130 |
// Check arguments |
131 |
// We should have one argument, with the value equal to the post body |
132 |
arguments = s.getArguments(); |
133 |
assertEquals(1, arguments.getArgumentCount()); |
134 |
checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, postBody, contentEncoding, false); |
102 |
} |
135 |
} |
103 |
|
136 |
|
104 |
// TODO: will need changing if arguments can be saved in decoded form |
|
|
105 |
public void testEncodedArguments() throws Exception { |
137 |
public void testEncodedArguments() throws Exception { |
106 |
// A HTTP GET request |
138 |
String url = "http://localhost/matrix.html"; |
107 |
String TEST_GET_REQ = "GET http://localhost:80/matrix.html" |
139 |
// A HTTP GET request, with encoding not known |
108 |
+ "?abc" |
140 |
String contentEncoding = ""; |
109 |
+ "?SPACE=a+b" |
141 |
String queryString = "abc%3FSPACE=a+b&space=a%20b&query=What%3F"; |
110 |
+ "&space=a%20b" |
142 |
String testGetRequest = "GET " + url |
111 |
+ "&query=What?" |
143 |
+ "?" + queryString |
112 |
+ " HTTP/1.1\n\n"; |
144 |
+ " HTTP/1.1\r\n\r\n"; |
113 |
HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8"); |
145 |
// Use null for url and contentEncoding, to simulate that HttpRequestHdr do not |
|
|
146 |
// know the encoding for the page |
147 |
HTTPSamplerBase s = getSamplerForRequest(null, testGetRequest, null); |
114 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
148 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
|
|
149 |
assertEquals(queryString, s.getQueryString()); |
150 |
assertEquals(contentEncoding, s.getContentEncoding()); |
115 |
|
151 |
|
116 |
// Check arguments |
152 |
// Check arguments |
117 |
Arguments arguments = s.getArguments(); |
153 |
Arguments arguments = s.getArguments(); |
118 |
assertEquals(3, arguments.getArgumentCount()); |
154 |
assertEquals(3, arguments.getArgumentCount()); |
119 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", "a+b", "UTF-8", false); |
155 |
// When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false |
120 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", "UTF-8", false); |
156 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc%3FSPACE", "a+b", "a+b", contentEncoding, false); |
121 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What?", "UTF-8", false); |
157 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", contentEncoding, false); |
|
|
158 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What%3F", "What%3F", contentEncoding, false); |
122 |
|
159 |
|
123 |
// A HTTP POST request |
160 |
// A HTTP GET request, with UTF-8 encoding |
124 |
String postBody = "abc?SPACE=a+b&space=a%20b&query=What?"; |
161 |
contentEncoding = "UTF-8"; |
125 |
String TEST_POST_REQ = "POST http://localhost:80/matrix.html HTTP/1.1\n" |
162 |
queryString = "abc%3FSPACE=a+b&space=a%20b&query=What%3F"; |
|
|
163 |
testGetRequest = "GET " + url |
164 |
+ "?" + queryString |
165 |
+ " HTTP/1.1\r\n\r\n"; |
166 |
s = getSamplerForRequest(url, testGetRequest, contentEncoding); |
167 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
168 |
String expectedQueryString = "abc%3FSPACE=a+b&space=a+b&query=What%3F"; |
169 |
assertEquals(expectedQueryString, s.getQueryString()); |
170 |
assertEquals(contentEncoding, s.getContentEncoding()); |
171 |
|
172 |
// Check arguments |
173 |
arguments = s.getArguments(); |
174 |
assertEquals(3, arguments.getArgumentCount()); |
175 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a b", "a+b", contentEncoding, true); |
176 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a b", "a+b", contentEncoding, true); |
177 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What%3F", contentEncoding, true); |
178 |
|
179 |
// A HTTP POST request, with unknown encoding |
180 |
contentEncoding = ""; |
181 |
String postBody = "abc%3FSPACE=a+b&space=a%20b&query=What%3F"; |
182 |
String testPostRequest = "POST " + url + " HTTP/1.1\r\n" |
126 |
+ "Content-type: " |
183 |
+ "Content-type: " |
127 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED |
184 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
128 |
+ "; charset=UTF-8\n" |
185 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
|
|
186 |
+ "\r\n" |
129 |
+ postBody; |
187 |
+ postBody; |
130 |
s = getSamplerForRequest(TEST_POST_REQ, "UTF-8"); |
188 |
// Use null for url and contentEncoding, to simulate that HttpRequestHdr do not |
|
|
189 |
// know the encoding for the page |
190 |
s = getSamplerForRequest(null, testPostRequest, null); |
131 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
191 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
192 |
assertEquals(queryString, s.getQueryString()); |
193 |
assertEquals(contentEncoding, s.getContentEncoding()); |
132 |
assertFalse(s.getDoMultipartPost()); |
194 |
assertFalse(s.getDoMultipartPost()); |
133 |
|
195 |
|
134 |
// Check arguments |
196 |
// Check arguments |
135 |
arguments = s.getArguments(); |
197 |
arguments = s.getArguments(); |
136 |
assertEquals(3, arguments.getArgumentCount()); |
198 |
assertEquals(3, arguments.getArgumentCount()); |
137 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", "a+b", "UTF-8", false); |
199 |
// When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false |
138 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", "UTF-8", false); |
200 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc%3FSPACE", "a+b", "a+b", contentEncoding, false); |
139 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What?", "UTF-8", false); |
201 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", "a%20b", contentEncoding, false); |
|
|
202 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What%3F", "What%3F", contentEncoding, false); |
203 |
|
204 |
// A HTTP POST request, with UTF-8 encoding |
205 |
contentEncoding = "UTF-8"; |
206 |
postBody = "abc?SPACE=a+b&space=a%20b&query=What?"; |
207 |
testPostRequest = "POST " + url + " HTTP/1.1\n" |
208 |
+ "Content-type: " |
209 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
210 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
211 |
+ "\r\n" |
212 |
+ postBody; |
213 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
214 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
215 |
expectedQueryString = "abc%3FSPACE=a+b&space=a+b&query=What%3F"; |
216 |
assertEquals(expectedQueryString, s.getQueryString()); |
217 |
assertEquals(contentEncoding, s.getContentEncoding()); |
218 |
assertFalse(s.getDoMultipartPost()); |
219 |
|
220 |
// Check arguments |
221 |
arguments = s.getArguments(); |
222 |
assertEquals(3, arguments.getArgumentCount()); |
223 |
checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a b", "a+b", contentEncoding, true); |
224 |
checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a b", "a+b", contentEncoding, true); |
225 |
checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", "What%3F", contentEncoding, true); |
140 |
} |
226 |
} |
141 |
|
227 |
|
|
|
228 |
public void testGetRequestEncodings() throws Exception { |
229 |
String url = "http://localhost/matrix.html"; |
230 |
// A HTTP GET request, with encoding not known |
231 |
String contentEncoding = ""; |
232 |
String param1Value = "yes"; |
233 |
String param2Value = "0+5 -\u00c5\uc385%C3%85"; |
234 |
String param2ValueEncoded = URLEncoder.encode(param2Value); |
235 |
String testGetRequest = |
236 |
"GET " + url |
237 |
+ "?param1=" + param1Value + "¶m2=" + param2ValueEncoded + " " |
238 |
+ "HTTP/1.1\r\n\r\n"; |
239 |
// Use null for url and contentEncoding, to simulate that HttpRequestHdr do not |
240 |
// know the encoding for the page |
241 |
HTTPSamplerBase s = getSamplerForRequest(null, testGetRequest, null); |
242 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
243 |
assertEquals(contentEncoding, s.getContentEncoding()); |
244 |
// Check arguments |
245 |
Arguments arguments = s.getArguments(); |
246 |
assertEquals(2, arguments.getArgumentCount()); |
247 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
248 |
// When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false |
249 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2ValueEncoded, param2ValueEncoded, contentEncoding, false); |
250 |
|
251 |
// A HTTP GET request, with UTF-8 encoding |
252 |
contentEncoding = "UTF-8"; |
253 |
param1Value = "yes"; |
254 |
param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85"; |
255 |
param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding); |
256 |
testGetRequest = |
257 |
"GET " + url |
258 |
+ "?param1=" + param1Value + "¶m2=" + param2ValueEncoded + " " |
259 |
+ "HTTP/1.1\r\n\r\n"; |
260 |
s = getSamplerForRequest(url, testGetRequest, contentEncoding); |
261 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
262 |
assertEquals(contentEncoding, s.getContentEncoding()); |
263 |
// Check arguments |
264 |
arguments = s.getArguments(); |
265 |
assertEquals(2, arguments.getArgumentCount()); |
266 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
267 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2Value, param2ValueEncoded, contentEncoding, true); |
268 |
|
269 |
// A HTTP GET request, with ISO-8859-1 encoding |
270 |
contentEncoding = "ISO-8859-1"; |
271 |
param1Value = "yes"; |
272 |
param2Value = "0+5 -\u00c5%C3%85"; |
273 |
param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding); |
274 |
testGetRequest = |
275 |
"GET " + url |
276 |
+ "?param1=" + param1Value + "¶m2=" + param2ValueEncoded + " " |
277 |
+ "HTTP/1.1\r\n\r\n"; |
278 |
s = getSamplerForRequest(url, testGetRequest, contentEncoding); |
279 |
assertEquals(HTTPSamplerBase.GET, s.getMethod()); |
280 |
assertEquals(contentEncoding, s.getContentEncoding()); |
281 |
// Check arguments |
282 |
arguments = s.getArguments(); |
283 |
assertEquals(2, arguments.getArgumentCount()); |
284 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
285 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2Value, param2ValueEncoded, contentEncoding, true); |
286 |
} |
287 |
|
288 |
public void testPostRequestEncodings() throws Exception { |
289 |
String url = "http://localhost/matrix.html"; |
290 |
// A HTTP POST request, with encoding not known |
291 |
String contentEncoding = ""; |
292 |
String param1Value = "yes"; |
293 |
String param2Value = "0+5 -\u00c5%C3%85"; |
294 |
String param2ValueEncoded = URLEncoder.encode(param2Value); |
295 |
String postBody = "param1=" + param1Value + "¶m2=" + param2ValueEncoded + "\r\n"; |
296 |
String testPostRequest = |
297 |
"POST " + url + " HTTP/1.1\r\n" |
298 |
+ "Content-type: " |
299 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
300 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
301 |
+ "\r\n" |
302 |
+ postBody; |
303 |
|
304 |
// Use null for url and contentEncoding, to simulate that HttpRequestHdr do not |
305 |
// know the encoding for the page |
306 |
HTTPSamplerBase s = getSamplerForRequest(null, testPostRequest, null); |
307 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
308 |
assertEquals(contentEncoding, s.getContentEncoding()); |
309 |
// Check arguments |
310 |
Arguments arguments = s.getArguments(); |
311 |
assertEquals(2, arguments.getArgumentCount()); |
312 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
313 |
// When the encoding is not known, the argument will get the encoded value, and the "encode?" set to false |
314 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2ValueEncoded, param2ValueEncoded, contentEncoding, false); |
315 |
|
316 |
// A HTTP POST request, with UTF-8 encoding |
317 |
contentEncoding = "UTF-8"; |
318 |
param1Value = "yes"; |
319 |
param2Value = "0+5 -\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052\uc385%C3%85"; |
320 |
param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding); |
321 |
postBody = "param1=" + param1Value + "¶m2=" + param2ValueEncoded + "\r\n"; |
322 |
testPostRequest = |
323 |
"POST " + url + " HTTP/1.1\r\n" |
324 |
+ "Content-type: " |
325 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
326 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
327 |
+ "\r\n" |
328 |
+ postBody; |
329 |
|
330 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
331 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
332 |
assertEquals(contentEncoding, s.getContentEncoding()); |
333 |
// Check arguments |
334 |
arguments = s.getArguments(); |
335 |
assertEquals(2, arguments.getArgumentCount()); |
336 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
337 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2Value, param2ValueEncoded, contentEncoding, true); |
338 |
|
339 |
// A HTTP POST request, with ISO-8859-1 encoding |
340 |
contentEncoding = "ISO-8859-1"; |
341 |
param1Value = "yes"; |
342 |
param2Value = "0+5 -\u00c5%C3%85"; |
343 |
param2ValueEncoded = URLEncoder.encode(param2Value, contentEncoding); |
344 |
postBody = "param1=" + param1Value + "¶m2=" + param2ValueEncoded + "\r\n"; |
345 |
testPostRequest = |
346 |
"POST " + url + " HTTP/1.1\r\n" |
347 |
+ "Content-type: " |
348 |
+ HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED + "\r\n" |
349 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + "\r\n" |
350 |
+ "\r\n" |
351 |
+ postBody; |
352 |
|
353 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
354 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
355 |
assertEquals(contentEncoding, s.getContentEncoding()); |
356 |
// Check arguments |
357 |
arguments = s.getArguments(); |
358 |
assertEquals(2, arguments.getArgumentCount()); |
359 |
checkArgument((HTTPArgument)arguments.getArgument(0), "param1", param1Value, param1Value, contentEncoding, false); |
360 |
checkArgument((HTTPArgument)arguments.getArgument(1), "param2", param2Value, param2ValueEncoded, contentEncoding, true); |
361 |
} |
362 |
|
142 |
public void testPostMultipartFormData() throws Exception { |
363 |
public void testPostMultipartFormData() throws Exception { |
|
|
364 |
String url = "http://localhost/matrix.html"; |
143 |
// A HTTP POST request, multipart/form-data, simple values, |
365 |
// A HTTP POST request, multipart/form-data, simple values, |
144 |
String contentEncoding = "UTF-8"; |
366 |
String contentEncoding = "UTF-8"; |
145 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
367 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
Lines 147-156
Link Here
|
147 |
String titleValue = "mytitle"; |
369 |
String titleValue = "mytitle"; |
148 |
String descriptionValue = "mydescription"; |
370 |
String descriptionValue = "mydescription"; |
149 |
String postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
371 |
String postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
150 |
String testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
372 |
String testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine); |
151 |
|
373 |
|
152 |
HTTPSamplerBase s = getSamplerForRequest(testPostRequest, "UTF-8"); |
374 |
HTTPSamplerBase s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
153 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
375 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
376 |
assertEquals(contentEncoding, s.getContentEncoding()); |
154 |
assertTrue(s.getDoMultipartPost()); |
377 |
assertTrue(s.getDoMultipartPost()); |
155 |
|
378 |
|
156 |
// Check arguments |
379 |
// Check arguments |
Lines 166-175
Link Here
|
166 |
titleValue = "mytitle"; |
389 |
titleValue = "mytitle"; |
167 |
descriptionValue = "mydescription"; |
390 |
descriptionValue = "mydescription"; |
168 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
391 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
169 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
392 |
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine); |
170 |
|
393 |
|
171 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
394 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
172 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
395 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
396 |
assertEquals(contentEncoding, s.getContentEncoding()); |
173 |
assertTrue(s.getDoMultipartPost()); |
397 |
assertTrue(s.getDoMultipartPost()); |
174 |
|
398 |
|
175 |
// Check arguments |
399 |
// Check arguments |
Lines 185-194
Link Here
|
185 |
titleValue = "mytitle"; |
409 |
titleValue = "mytitle"; |
186 |
descriptionValue = "mydescription"; |
410 |
descriptionValue = "mydescription"; |
187 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
411 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
188 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
412 |
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine); |
189 |
|
413 |
|
190 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
414 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
191 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
415 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
416 |
assertEquals(contentEncoding, s.getContentEncoding()); |
192 |
assertTrue(s.getDoMultipartPost()); |
417 |
assertTrue(s.getDoMultipartPost()); |
193 |
|
418 |
|
194 |
// Check arguments |
419 |
// Check arguments |
Lines 204-213
Link Here
|
204 |
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc="; |
429 |
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc="; |
205 |
descriptionValue = "mydescription"; |
430 |
descriptionValue = "mydescription"; |
206 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
431 |
postBody = createMultipartFormBody(titleValue, descriptionValue, contentEncoding, true, boundary, endOfLine); |
207 |
testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
432 |
testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine); |
208 |
|
433 |
|
209 |
s = getSamplerForRequest(testPostRequest, "UTF-8"); |
434 |
s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
210 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
435 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
436 |
assertEquals(contentEncoding, s.getContentEncoding()); |
211 |
assertTrue(s.getDoMultipartPost()); |
437 |
assertTrue(s.getDoMultipartPost()); |
212 |
|
438 |
|
213 |
// Check arguments |
439 |
// Check arguments |
Lines 218-224
Link Here
|
218 |
} |
444 |
} |
219 |
|
445 |
|
220 |
public void testPostMultipartFileUpload() throws Exception { |
446 |
public void testPostMultipartFileUpload() throws Exception { |
|
|
447 |
String url = "http://localhost/matrix.html"; |
221 |
// A HTTP POST request, multipart/form-data, simple values, |
448 |
// A HTTP POST request, multipart/form-data, simple values, |
|
|
449 |
String contentEncoding = "UTF-8"; |
222 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
450 |
String boundary = "xf8SqlDNvmn6mFYwrioJaeUR2_Z4cLRXOSmB"; |
223 |
String endOfLine = "\r\n"; |
451 |
String endOfLine = "\r\n"; |
224 |
String fileFieldValue = "test_file"; |
452 |
String fileFieldValue = "test_file"; |
Lines 226-235
Link Here
|
226 |
String mimeType = "text/plain"; |
454 |
String mimeType = "text/plain"; |
227 |
String fileContent = "somedummycontent\n\ndfgdfg\r\nfgdgdg\nContent-type:dfsfsfds"; |
455 |
String fileContent = "somedummycontent\n\ndfgdfg\r\nfgdgdg\nContent-type:dfsfsfds"; |
228 |
String postBody = createMultipartFileUploadBody(fileFieldValue, fileName, mimeType, fileContent, boundary, endOfLine); |
456 |
String postBody = createMultipartFileUploadBody(fileFieldValue, fileName, mimeType, fileContent, boundary, endOfLine); |
229 |
String testPostRequest = createMultipartFormRequest(postBody, boundary, endOfLine); |
457 |
String testPostRequest = createMultipartFormRequest(url, postBody, contentEncoding, boundary, endOfLine); |
230 |
|
458 |
|
231 |
HTTPSamplerBase s = getSamplerForRequest(testPostRequest, "UTF-8"); |
459 |
HTTPSamplerBase s = getSamplerForRequest(url, testPostRequest, contentEncoding); |
232 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
460 |
assertEquals(HTTPSamplerBase.POST, s.getMethod()); |
|
|
461 |
assertEquals(contentEncoding, s.getContentEncoding()); |
462 |
assertEquals("", s.getQueryString()); |
233 |
assertTrue(s.getDoMultipartPost()); |
463 |
assertTrue(s.getDoMultipartPost()); |
234 |
|
464 |
|
235 |
// Check arguments |
465 |
// Check arguments |
Lines 239-245
Link Here
|
239 |
assertEquals(fileName, s.getFilename()); |
469 |
assertEquals(fileName, s.getFilename()); |
240 |
assertEquals(mimeType, s.getMimetype()); |
470 |
assertEquals(mimeType, s.getMimetype()); |
241 |
} |
471 |
} |
242 |
|
472 |
|
243 |
private String createMultipartFormBody(String titleValue, String descriptionValue, String contentEncoding, boolean includeExtraHeaders, String boundary, String endOfLine) { |
473 |
private String createMultipartFormBody(String titleValue, String descriptionValue, String contentEncoding, boolean includeExtraHeaders, String boundary, String endOfLine) { |
244 |
// Title multipart |
474 |
// Title multipart |
245 |
String postBody = "--" + boundary + endOfLine |
475 |
String postBody = "--" + boundary + endOfLine |
Lines 276-299
Link Here
|
276 |
return postBody; |
506 |
return postBody; |
277 |
} |
507 |
} |
278 |
|
508 |
|
279 |
private String createMultipartFormRequest(String postBody, String boundary, String endOfLine) { |
509 |
private String createMultipartFormRequest(String url, String postBody, String contentEncoding, String boundary, String endOfLine) |
280 |
String postRequest = "POST http://localhost:80/matrix.html HTTP/1.1" + endOfLine |
510 |
throws IOException { |
|
|
511 |
String postRequest = "POST " + url + " HTTP/1.1" + endOfLine |
281 |
+ "Content-type: " |
512 |
+ "Content-type: " |
282 |
+ HTTPSamplerBase.MULTIPART_FORM_DATA |
513 |
+ HTTPSamplerBase.MULTIPART_FORM_DATA |
283 |
+ "; boundary=" + boundary + endOfLine |
514 |
+ "; boundary=" + boundary + endOfLine |
284 |
+ "Content-length: " + postBody.length() + endOfLine |
515 |
+ "Content-length: " + getBodyLength(postBody, contentEncoding) + endOfLine |
285 |
+ endOfLine |
516 |
+ endOfLine |
286 |
+ postBody; |
517 |
+ postBody; |
287 |
return postRequest; |
518 |
return postRequest; |
288 |
} |
519 |
} |
289 |
|
520 |
|
290 |
private HTTPSamplerBase getSamplerForRequest(String request, String contentEncoding) |
521 |
private HTTPSamplerBase getSamplerForRequest(String url, String request, String contentEncoding) |
291 |
throws IOException { |
522 |
throws IOException { |
292 |
HttpRequestHdr req = new HttpRequestHdr(); |
523 |
HttpRequestHdr req = new HttpRequestHdr(); |
293 |
ByteArrayInputStream bis = new ByteArrayInputStream(request.getBytes(contentEncoding)); |
524 |
ByteArrayInputStream bis = null; |
|
|
525 |
if(contentEncoding != null) { |
526 |
bis = new ByteArrayInputStream(request.getBytes(contentEncoding)); |
527 |
|
528 |
} |
529 |
else { |
530 |
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8 |
531 |
bis = new ByteArrayInputStream(request.getBytes("ISO-8859-1")); |
532 |
} |
294 |
req.parse(bis); |
533 |
req.parse(bis); |
295 |
bis.close(); |
534 |
bis.close(); |
296 |
return req.getSampler(); |
535 |
Map pageEncodings = Collections.synchronizedMap(new HashMap()); |
|
|
536 |
Map formEncodings = Collections.synchronizedMap(new HashMap()); |
537 |
if(url != null && contentEncoding != null) { |
538 |
pageEncodings.put(url, contentEncoding); |
539 |
} |
540 |
return req.getSampler(pageEncodings, formEncodings); |
297 |
} |
541 |
} |
298 |
|
542 |
|
299 |
private void checkArgument( |
543 |
private void checkArgument( |
Lines 304-311
Link Here
|
304 |
String contentEncoding, |
548 |
String contentEncoding, |
305 |
boolean expectedEncoded) throws IOException { |
549 |
boolean expectedEncoded) throws IOException { |
306 |
assertEquals(expectedName, arg.getName()); |
550 |
assertEquals(expectedName, arg.getName()); |
|
|
551 |
// System.out.println("expect " + URLEncoder.encode(expectedValue, "UTF-8")); |
552 |
// System.out.println("actual " + URLEncoder.encode(arg.getValue(), "UTF-8")); |
307 |
assertEquals(expectedValue, arg.getValue()); |
553 |
assertEquals(expectedValue, arg.getValue()); |
308 |
assertEquals(expectedEncodedValue, arg.getEncodedValue(contentEncoding)); |
554 |
if(contentEncoding != null && contentEncoding.length() > 0) { |
|
|
555 |
assertEquals(expectedEncodedValue, arg.getEncodedValue(contentEncoding)); |
556 |
} |
557 |
else { |
558 |
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8 |
559 |
assertEquals(expectedEncodedValue, arg.getEncodedValue("ISO-8859-1")); |
560 |
} |
309 |
assertEquals(expectedEncoded, arg.isAlwaysEncoded()); |
561 |
assertEquals(expectedEncoded, arg.isAlwaysEncoded()); |
310 |
} |
562 |
} |
|
|
563 |
|
564 |
private int getBodyLength(String postBody, String contentEncoding) throws IOException { |
565 |
if(contentEncoding != null && contentEncoding.length() > 0) { |
566 |
return postBody.getBytes(contentEncoding).length; |
567 |
} |
568 |
else { |
569 |
// Most browsers use ISO-8859-1 as default encoding, even if spec says UTF-8 |
570 |
return postBody.getBytes().length; |
571 |
} |
572 |
} |
311 |
} |
573 |
} |