View | Details | Raw Unified | Return to bug 42173
Collapse All | Expand All

(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/test/src/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java (-82 / +344 lines)
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 + "&param2=" + 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 + "&param2=" + 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 + "&param2=" + 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 + "&param2=" + 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 + "&param2=" + 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 + "&param2=" + 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
}
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Proxy.java (-8 / +108 lines)
Lines 25-32 Link Here
25
import java.io.OutputStream;
25
import java.io.OutputStream;
26
import java.net.Socket;
26
import java.net.Socket;
27
import java.net.UnknownHostException;
27
import java.net.UnknownHostException;
28
import java.net.URL;
29
import java.util.Map;
28
30
29
import org.apache.jmeter.protocol.http.control.HeaderManager;
31
import org.apache.jmeter.protocol.http.control.HeaderManager;
32
import org.apache.jmeter.protocol.http.parser.HTMLParseException;
30
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
33
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
31
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
34
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerFactory;
32
import org.apache.jmeter.samplers.SampleResult;
35
import org.apache.jmeter.samplers.SampleResult;
Lines 61-66 Link Here
61
	/** Whether to try to spoof as https **/
64
	/** Whether to try to spoof as https **/
62
	private boolean httpsSpoof;
65
	private boolean httpsSpoof;
63
66
67
    /** Reference to Deamon's Map of url string to page character encoding of that page */
68
    private Map pageEncodings;
69
    /** Reference to Deamon's Map of url string to character encoding for the form */
70
    private Map formEncodings;
71
64
	/**
72
	/**
65
	 * Default constructor - used by newInstance call in Daemon
73
	 * Default constructor - used by newInstance call in Daemon
66
	 */
74
	 */
Lines 88-98 Link Here
88
	 *            the ProxyControl which will receive the generated sampler
96
	 *            the ProxyControl which will receive the generated sampler
89
	 */
97
	 */
90
	void configure(Socket _clientSocket, ProxyControl _target) {
98
	void configure(Socket _clientSocket, ProxyControl _target) {
91
		this.target = _target;
99
        configure(_clientSocket, _target, null, null);
92
		this.clientSocket = _clientSocket;
100
    }
93
		this.captureHttpHeaders = _target.getCaptureHttpHeaders();
101
    
94
		this.httpsSpoof = target.getHttpsSpoof();
102
    /**
95
	}
103
     * Configure the Proxy.
104
     * 
105
     * @param clientSocket
106
     *            the socket connection to the client
107
     * @param target
108
     *            the ProxyControl which will receive the generated sampler
109
     * @param pageEncodings
110
     *            reference to the Map of Deamon, with mappings from page urls to encoding used
111
     * @param formEncodingsEncodings
112
     *            reference to the Map of Deamon, with mappings from form action urls to encoding used
113
     */
114
    void configure(Socket clientSocket, ProxyControl target, Map pageEncodings, Map formEncodings) {
115
        this.target = target;
116
        this.clientSocket = clientSocket;
117
        this.captureHttpHeaders = target.getCaptureHttpHeaders();
118
        this.httpsSpoof = target.getHttpsSpoof();
119
        this.pageEncodings = pageEncodings;
120
        this.formEncodings = formEncodings;
121
    }
96
122
97
	/**
123
	/**
98
	 * Main processing method for the Proxy object
124
	 * Main processing method for the Proxy object
Lines 118-124 Link Here
118
144
119
			// Populate the sampler. It is the same sampler as we sent into
145
			// Populate the sampler. It is the same sampler as we sent into
120
			// the constructor of the HttpRequestHdr instance above 
146
			// the constructor of the HttpRequestHdr instance above 
121
			request.getSampler();
147
            request.getSampler(pageEncodings, formEncodings);
122
148
123
			/*
149
			/*
124
			 * Create a Header Manager to ensure that the browsers headers are
150
			 * Create a Header Manager to ensure that the browsers headers are
Lines 145-151 Link Here
145
				String noHttpsResult = new String(result.getResponseData());
171
				String noHttpsResult = new String(result.getResponseData());
146
				result.setResponseData(noHttpsResult.replaceAll("https", "http").getBytes());
172
				result.setResponseData(noHttpsResult.replaceAll("https", "http").getBytes());
147
			}
173
			}
148
				
174
175
            // Find the page encoding and possibly encodings for forms in the page
176
            // in the response from the web server
177
            String pageEncoding = addPageEncoding(result);
178
            addFormEncodings(result, pageEncoding);
179
149
			writeToClient(result, new BufferedOutputStream(clientSocket.getOutputStream()));
180
			writeToClient(result, new BufferedOutputStream(clientSocket.getOutputStream()));
150
			/*
181
			/*
151
			 * We don't want to store any cookies in the generated test plan
182
			 * We don't want to store any cookies in the generated test plan
Lines 241-244 Link Here
241
			log.warn("Exception while writing error", e);
272
			log.warn("Exception while writing error", e);
242
		}
273
		}
243
	}
274
	}
244
}
275
276
    /**
277
     * Add the page encoding of the sample result to the Map with page encodings
278
     * 
279
     * @param result the sample result to check
280
     * @return the page encoding found for the sample result, or null
281
     */
282
    private String addPageEncoding(SampleResult result) {
283
        String pageEncoding = getContentEncoding(result);
284
        if(pageEncoding != null) {
285
            String urlWithoutQuery = getUrlWithoutQuery(result.getURL());
286
            synchronized(pageEncodings) {
287
                pageEncodings.put(urlWithoutQuery, pageEncoding);
288
            }
289
        }
290
        return pageEncoding;
291
    }
292
    
293
    /**
294
     * Add the form encodings for all forms in the sample result
295
     * 
296
     * @param result the sample result to check
297
     * @param pageEncoding the encoding used for the sample result page
298
     */
299
    private void addFormEncodings(SampleResult result, String pageEncoding) {
300
        FormCharSetFinder finder = new FormCharSetFinder();
301
        try {
302
            finder.addFormActionsAndCharSet(result.getResponseDataAsString(), formEncodings, pageEncoding);
303
        }
304
        catch (HTMLParseException parseException) {
305
            log.debug("Unable to parse response, could not find any form character set encodings");
306
        }
307
    }
308
309
    /**
310
     * Get the value of the charset of the content-type header of the sample result
311
     * 
312
     * @param res the sample result to find the charset for
313
     * @return the charset found, or null
314
     */
315
    private String getContentEncoding(SampleResult res) {
316
        String contentTypeHeader = res.getContentType();
317
        String charSet = null;
318
        if (contentTypeHeader != null) {
319
            int charSetStartPos = contentTypeHeader.toLowerCase().indexOf("charset=");
320
            if (charSetStartPos > 0) {
321
                charSet = contentTypeHeader.substring(charSetStartPos + "charset=".length());
322
                if (charSet != null) {
323
                    if (charSet.trim().length() > 0) {
324
                        charSet = charSet.trim();
325
                    } else {
326
                        charSet = null;
327
                    }
328
                }
329
            }
330
        }
331
        return charSet;
332
    }
333
334
    private String getUrlWithoutQuery(URL url) {
335
        String fullUrl = url.toString();
336
        String urlWithoutQuery = fullUrl;
337
        String query = url.getQuery();
338
        if(query != null) {
339
            // Get rid of the query and the ?
340
            urlWithoutQuery = urlWithoutQuery.substring(0, urlWithoutQuery.length() - query.length() - 1);
341
        }
342
        return urlWithoutQuery;
343
    }
344
}
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/proxy/Daemon.java (-1 / +17 lines)
Lines 22-27 Link Here
22
import java.io.InterruptedIOException;
22
import java.io.InterruptedIOException;
23
import java.net.ServerSocket;
23
import java.net.ServerSocket;
24
import java.net.Socket;
24
import java.net.Socket;
25
import java.util.Collections;
26
import java.util.HashMap;
27
import java.util.Map;
25
28
26
import org.apache.jorphan.logging.LoggingManager;
29
import org.apache.jorphan.logging.LoggingManager;
27
import org.apache.log.Logger;
30
import org.apache.log.Logger;
Lines 67-72 Link Here
67
	 */
70
	 */
68
	private Class proxyClass = Proxy.class;
71
	private Class proxyClass = Proxy.class;
69
72
73
    /** A Map of url string to page character encoding of that page */
74
    private Map pageEncodings;
75
    /** A Map of url string to character encoding for the form */
76
    private Map formEncodings;
77
70
	/**
78
	/**
71
	 * Default constructor.
79
	 * Default constructor.
72
	 */
80
	 */
Lines 161-166 Link Here
161
		running = true;
169
		running = true;
162
		ServerSocket mainSocket = null;
170
		ServerSocket mainSocket = null;
163
171
172
        // Maps to contain page and form encodings
173
        pageEncodings = Collections.synchronizedMap(new HashMap());
174
        formEncodings = Collections.synchronizedMap(new HashMap());
175
        
164
		try {
176
		try {
165
			log.info("Creating Daemon Socket... on port " + daemonPort);
177
			log.info("Creating Daemon Socket... on port " + daemonPort);
166
			mainSocket = new ServerSocket(daemonPort);
178
			mainSocket = new ServerSocket(daemonPort);
Lines 174-180 Link Here
174
					if (running) {
186
					if (running) {
175
						// Pass request to new proxy thread
187
						// Pass request to new proxy thread
176
						Proxy thd = (Proxy) proxyClass.newInstance();
188
						Proxy thd = (Proxy) proxyClass.newInstance();
177
						thd.configure(clientSocket, target);
189
                        thd.configure(clientSocket, target, pageEncodings, formEncodings);
178
						thd.start();
190
						thd.start();
179
					} else {
191
					} else {
180
						// The socket was accepted after we were told to stop.
192
						// The socket was accepted after we were told to stop.
Lines 199-204 Link Here
199
			} catch (Exception exc) {
211
			} catch (Exception exc) {
200
			}
212
			}
201
		}
213
		}
214
        
215
        // Clear maps
216
        pageEncodings = null;
217
        formEncodings = null;
202
	}
218
	}
203
219
204
	/**
220
	/**
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/proxy/FormCharSetFinder.java (+131 lines)
Line 0 Link Here
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 * 
17
 */
18
19
package org.apache.jmeter.protocol.http.proxy;
20
21
import java.util.Map;
22
23
import org.apache.jorphan.logging.LoggingManager;
24
import org.apache.jorphan.util.JOrphanUtils;
25
import org.apache.log.Logger;
26
import org.apache.jmeter.protocol.http.parser.HTMLParseException;
27
import org.htmlparser.Node;
28
import org.htmlparser.Parser;
29
import org.htmlparser.Tag;
30
import org.htmlparser.tags.CompositeTag;
31
import org.htmlparser.tags.FormTag;
32
import org.htmlparser.util.NodeIterator;
33
import org.htmlparser.util.ParserException;
34
35
/**
36
 * A parser for html, to find the form tags, and their accept-charset value
37
 */
38
class FormCharSetFinder {
39
    private static final Logger log = LoggingManager.getLoggerForClass();
40
41
    protected FormCharSetFinder() {
42
		super();
43
        log.info("Using htmlparser version: "+Parser.getVersion());
44
	}
45
46
    /**
47
     * Add form action urls and their corresponding encodings for all forms on the page
48
     * 
49
     * @param html the html to parse for form encodings
50
     * @param formEncodings the Map where form encodings should be added
51
     * @param pageEncoding the encoding used for the whole page
52
     * @throws HTMLParseException
53
     */
54
	public void addFormActionsAndCharSet(String html, Map formEncodings, String pageEncoding)
55
            throws HTMLParseException {
56
        if (log.isDebugEnabled()) {
57
            log.debug("Parsing html of: " + html);
58
        }
59
        
60
        Parser htmlParser = null;
61
		try {
62
			htmlParser = new Parser();
63
            htmlParser.setInputHTML(html);
64
		} catch (Exception e) {
65
			throw new HTMLParseException(e);
66
		}
67
68
		// Now parse the DOM tree
69
		try {
70
			// we start to iterate through the elements
71
			parseNodes(htmlParser.elements(), formEncodings, pageEncoding);
72
			log.debug("End   : parseNodes");
73
		} catch (ParserException e) {
74
			throw new HTMLParseException(e);
75
		}
76
	}
77
    
78
    /**
79
     * Recursively parse all nodes to pick up all form encodings
80
     * 
81
     * @param e the nodes to be parsed
82
     * @param formEncodings the Map where we should add form encodings found
83
     * @param pageEncoding the encoding used for the page where the nodes are present
84
     */
85
    private void parseNodes(final NodeIterator e, Map formEncodings, String pageEncoding)
86
        throws HTMLParseException, ParserException {
87
        while(e.hasMoreNodes()) {
88
            Node node = e.nextNode();
89
            // a url is always in a Tag.
90
            if (!(node instanceof Tag)) {
91
                continue;
92
            }
93
            Tag tag = (Tag) node;
94
95
            // Only check form tags
96
            if (tag instanceof FormTag) {
97
                // Find the action / form url
98
                String action = tag.getAttribute("action");
99
                String acceptCharSet = tag.getAttribute("accept-charset");
100
                if(action != null && action.length() > 0) {
101
                    // We use the page encoding where the form resides, as the
102
                    // default encoding for the form
103
                    String formCharSet = pageEncoding;
104
                    // Check if we found an accept-charset attribute on the form
105
                    if(acceptCharSet != null) {
106
                        String[] charSets = JOrphanUtils.split(acceptCharSet, ",");
107
                        // Just use the first one of the possible many charsets
108
                        if(charSets.length > 0) {
109
                            formCharSet = charSets[0].trim();
110
                            if(formCharSet.length() == 0) {
111
                                formCharSet = null;
112
                            }
113
                        }
114
                    }
115
                    if(formCharSet != null) {
116
                        synchronized (formEncodings) {
117
                            formEncodings.put(action, formCharSet);
118
                        }
119
                    }
120
                }
121
            }
122
123
            // second, if the tag was a composite tag,
124
            // recursively parse its children.
125
            if (tag instanceof CompositeTag) {
126
                CompositeTag composite = (CompositeTag) tag;
127
                parseNodes(composite.elements(), formEncodings, pageEncoding);
128
            }
129
        }
130
    }
131
}
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/proxy/HttpRequestHdr.java (-19 / +137 lines)
Lines 21-26 Link Here
21
import java.io.ByteArrayOutputStream;
21
import java.io.ByteArrayOutputStream;
22
import java.io.IOException;
22
import java.io.IOException;
23
import java.io.InputStream;
23
import java.io.InputStream;
24
import java.io.UnsupportedEncodingException;
25
import java.net.MalformedURLException;
26
import java.net.ProtocolException;
27
import java.net.URL;
24
import java.util.HashMap;
28
import java.util.HashMap;
25
import java.util.Iterator;
29
import java.util.Iterator;
26
import java.util.Map;
30
import java.util.Map;
Lines 38-43 Link Here
38
import org.apache.jmeter.testelement.TestElement;
42
import org.apache.jmeter.testelement.TestElement;
39
import org.apache.jmeter.util.JMeterUtils;
43
import org.apache.jmeter.util.JMeterUtils;
40
import org.apache.jorphan.logging.LoggingManager;
44
import org.apache.jorphan.logging.LoggingManager;
45
import org.apache.jorphan.util.JOrphanUtils;
41
import org.apache.log.Logger;
46
import org.apache.log.Logger;
42
47
43
//For unit tests, @see TestHttpRequestHdr
48
//For unit tests, @see TestHttpRequestHdr
Lines 71-77 Link Here
71
	 */
76
	 */
72
	private String version = ""; // NOTREAD // $NON-NLS-1$
77
	private String version = ""; // NOTREAD // $NON-NLS-1$
73
78
74
	private String postData = ""; // $NON-NLS-1$
79
    private byte[] rawPostData;
75
80
76
	private Map headers = new HashMap();
81
	private Map headers = new HashMap();
77
82
Lines 134-142 Link Here
134
				readLength++;
139
				readLength++;
135
			}
140
			}
136
		}
141
		}
137
		postData = line.toString();
142
        // Keep the raw post data
143
        rawPostData = line.toByteArray();
144
138
        if (log.isDebugEnabled()){
145
        if (log.isDebugEnabled()){
139
    		log.debug("postData: " + postData);
146
            log.debug("rawPostData in default JRE encoding: " + new String(rawPostData));
140
    		log.debug("Request: " + clientRequest.toString());
147
    		log.debug("Request: " + clientRequest.toString());
141
        }
148
        }
142
		return clientRequest.toByteArray();
149
		return clientRequest.toByteArray();
Lines 198-204 Link Here
198
		return headerManager;
205
		return headerManager;
199
	}
206
	}
200
207
201
	public HTTPSamplerBase getSampler() {
208
    public HTTPSamplerBase getSampler(Map pageEncodings, Map formEncodings)
209
            throws MalformedURLException, IOException, ProtocolException {
202
		// Damn! A whole new GUI just to instantiate a test element?
210
		// Damn! A whole new GUI just to instantiate a test element?
203
		// Isn't there a beter way?
211
		// Isn't there a beter way?
204
		HttpTestSampleGui tempGui = null;
212
		HttpTestSampleGui tempGui = null;
Lines 210-216 Link Here
210
			tempGui = new HttpTestSampleGui();
218
			tempGui = new HttpTestSampleGui();
211
		}
219
		}
212
		sampler.setProperty(TestElement.GUI_CLASS, tempGui.getClass().getName());
220
		sampler.setProperty(TestElement.GUI_CLASS, tempGui.getClass().getName());
213
		populateSampler();
221
222
        // Populate the sampler
223
        populateSampler(pageEncodings, formEncodings);
214
		
224
		
215
		tempGui.configure(sampler);
225
		tempGui.configure(sampler);
216
		tempGui.modifyTestElement(sampler);
226
		tempGui.modifyTestElement(sampler);
Lines 222-227 Link Here
222
    		log.debug("getSampler: sampler path = " + sampler.getPath());
232
    		log.debug("getSampler: sampler path = " + sampler.getPath());
223
		return sampler;
233
		return sampler;
224
	}
234
	}
235
    
236
    /**
237
     * 
238
     * @return
239
     * @throws MalformedURLException
240
     * @throws IOException
241
     * @throws ProtocolException
242
     * @deprecated use the getSampler(HashMap pageEncodings, HashMap formEncodings) instead, since
243
     * that properly handles the encodings of the page
244
     */
245
    public HTTPSamplerBase getSampler() throws MalformedURLException, IOException, ProtocolException {
246
        return getSampler(null, null);
247
    }
225
248
226
	private String getContentType() {
249
	private String getContentType() {
227
		Header contentTypeHeader = (Header) headers.get(CONTENT_TYPE);
250
		Header contentTypeHeader = (Header) headers.get(CONTENT_TYPE);
Lines 231-236 Link Here
231
        return null;
254
        return null;
232
	}
255
	}
233
256
257
    private String getContentEncoding() {
258
        String contentType = getContentType();
259
        if(contentType != null) {
260
            int charSetStartPos = contentType.toLowerCase().indexOf("charset="); 
261
            if(charSetStartPos >= 0) {
262
                String charSet = contentType.substring(charSetStartPos + "charset=".length());
263
                if(charSet != null && charSet.length() > 0) {
264
                    // Remove quotes if present 
265
                    charSet = JOrphanUtils.replaceAllChars(charSet, '"', "");
266
                    if(charSet.length() > 0) {
267
                        return charSet;
268
                    }
269
                }
270
            }
271
        }
272
        return null;
273
    }
274
234
    private boolean isMultipart(String contentType) {
275
    private boolean isMultipart(String contentType) {
235
        if (contentType != null && contentType.startsWith(HTTPSamplerBase.MULTIPART_FORM_DATA)) {
276
        if (contentType != null && contentType.startsWith(HTTPSamplerBase.MULTIPART_FORM_DATA)) {
236
            return true;
277
            return true;
Lines 250-279 Link Here
250
        }
291
        }
251
    }
292
    }
252
293
253
	private void populateSampler() {
294
    private void populateSampler(Map pageEncodings, Map formEncodings) 
254
		sampler.setDomain(serverName());
295
            throws MalformedURLException, UnsupportedEncodingException {        
296
        sampler.setDomain(serverName());
255
        if (log.isDebugEnabled())
297
        if (log.isDebugEnabled())
256
    		log.debug("Proxy: setting server: " + sampler.getDomain());
298
    		log.debug("Proxy: setting server: " + sampler.getDomain());
257
		sampler.setMethod(method);
299
		sampler.setMethod(method);
258
		log.debug("Proxy: method server: " + sampler.getMethod());
300
		log.debug("Proxy: method server: " + sampler.getMethod());
259
		sampler.setPath(serverUrl());
260
        if (log.isDebugEnabled())
301
        if (log.isDebugEnabled())
261
    		log.debug("Proxy: setting path: " + sampler.getPath());
262
		if (numberRequests) {
263
			requestNumber++;
264
			sampler.setName(requestNumber + " " + sampler.getPath());
265
		} else {
266
			sampler.setName(sampler.getPath());
267
		}
268
		sampler.setPort(serverPort());
269
        if (log.isDebugEnabled())
270
            log.debug("Proxy: setting port: " + sampler.getPort());
302
            log.debug("Proxy: setting port: " + sampler.getPort());
271
		if (url.indexOf("//") > -1) {
303
		if (url.indexOf("//") > -1) {
272
			String protocol = url.substring(0, url.indexOf(":"));
304
			String protocol = url.substring(0, url.indexOf(":"));
273
            if (log.isDebugEnabled())
305
            if (log.isDebugEnabled())
274
    			log.debug("Proxy: setting protocol to : " + protocol);
306
    			log.debug("Proxy: setting protocol to : " + protocol);
275
			sampler.setProtocol(protocol);
307
			sampler.setProtocol(protocol);
276
		} else if (sampler.getPort() == 443) {
308
		} else if (sampler.getPort() == HTTPSamplerBase.DEFAULT_HTTPS_PORT) {
277
			sampler.setProtocol(HTTPS);
309
			sampler.setProtocol(HTTPS);
278
            if (log.isDebugEnabled())
310
            if (log.isDebugEnabled())
279
    			log.debug("Proxy: setting protocol to https");
311
    			log.debug("Proxy: setting protocol to https");
Lines 283-288 Link Here
283
			sampler.setProtocol(HTTP);
315
			sampler.setProtocol(HTTP);
284
		}
316
		}
285
        
317
        
318
        URL pageUrl = null;
319
        if(sampler.isProtocolDefaultPort()) {            
320
            pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), serverUrl());
321
        }
322
        else {
323
            pageUrl = new URL(sampler.getProtocol(), sampler.getDomain(), sampler.getPort(), serverUrl());
324
        }
325
        String urlWithoutQuery = getUrlWithoutQuery(pageUrl);
326
        
327
328
        // Check if the request itself tells us what the encoding is
329
        String contentEncoding = null;
330
        String requestContentEncoding = getContentEncoding();
331
        if(requestContentEncoding != null) {
332
            contentEncoding = requestContentEncoding;
333
        }
334
        else {        
335
            // Check if we know the encoding of the page
336
            if (pageEncodings != null) {
337
                synchronized (pageEncodings) {
338
                    contentEncoding = (String) pageEncodings.get(urlWithoutQuery);
339
                }
340
            }
341
            // Check if we know the encoding of the form
342
            if (formEncodings != null) {
343
                synchronized (formEncodings) {
344
                    String formEncoding = (String) formEncodings.get(urlWithoutQuery);
345
                    // Form encoding has priority over page encoding
346
                    if (formEncoding != null) {
347
                        contentEncoding = formEncoding;
348
                    }
349
                }
350
            }
351
        }
352
353
        // Get the post data using the content encoding of the request
354
        String postData = null;
355
        if (log.isDebugEnabled()) {
356
            if(contentEncoding != null) {
357
                log.debug("Using encoding " + contentEncoding + " for request body");
358
            }
359
            else {
360
                log.debug("No encoding found, using JRE default encoding for request body");
361
            }
362
        }
363
        if (contentEncoding != null) {
364
            postData = new String(rawPostData, contentEncoding);
365
        } else {
366
            // Use default encoding
367
            postData = new String(rawPostData);
368
        }
369
370
        if(contentEncoding != null) {
371
            sampler.setPath(serverUrl(), contentEncoding);
372
        }
373
        else {
374
            // Although the spec says UTF-8 should be used for encoding URL parameters,
375
            // most browser use ISO-8859-1 for default if encoding is not known.
376
            // We use null for contentEncoding, then the url parameters will be added
377
            // with the value in the URL, and the "encode?" flag set to false
378
            sampler.setPath(serverUrl(), null);
379
        }
380
        if (log.isDebugEnabled())
381
            log.debug("Proxy: setting path: " + sampler.getPath());
382
        if (numberRequests) {
383
            requestNumber++;
384
            sampler.setName(requestNumber + " " + sampler.getPath());
385
        } else {
386
            sampler.setName(sampler.getPath());
387
        }
388
        
389
        // Set the content encoding
390
        if(contentEncoding != null) {
391
            sampler.setContentEncoding(contentEncoding);
392
        }
393
        
286
        // If it was a HTTP GET request, then all parameters in the URL
394
        // If it was a HTTP GET request, then all parameters in the URL
287
        // has been handled by the sampler.setPath above, so we just need
395
        // has been handled by the sampler.setPath above, so we just need
288
        // to do parse the rest of the request if it is not a GET request
396
        // to do parse the rest of the request if it is not a GET request
Lines 316-322 Link Here
316
                // It is the most common post request, with parameter name and values
424
                // It is the most common post request, with parameter name and values
317
                // We also assume this if no content type is present, to be most backwards compatible,
425
                // We also assume this if no content type is present, to be most backwards compatible,
318
                // but maybe we should only parse arguments if the content type is as expected
426
                // but maybe we should only parse arguments if the content type is as expected
319
                sampler.parseArguments(postData); //standard name=value postData
427
                sampler.parseArguments(postData.trim(), contentEncoding); //standard name=value postData
320
            } else if (postData != null && postData.length() > 0) {
428
            } else if (postData != null && postData.length() > 0) {
321
                // Just put the whole postbody as the value of a parameter
429
                // Just put the whole postbody as the value of a parameter
322
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
430
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
Lines 431-434 Link Here
431
		return strBuff.toString();
539
		return strBuff.toString();
432
	}
540
	}
433
541
542
    private String getUrlWithoutQuery(URL url) {
543
        String fullUrl = url.toString();
544
        String urlWithoutQuery = fullUrl;
545
        String query = url.getQuery();
546
        if(query != null) {
547
            // Get rid of the query and the ?
548
            urlWithoutQuery = urlWithoutQuery.substring(0, urlWithoutQuery.length() - query.length() - 1);
549
        }
550
        return urlWithoutQuery;
551
    }
434
}
552
}
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (-46 / +96 lines)
Lines 360-377 Link Here
360
	 *            The new Path value
360
	 *            The new Path value
361
	 */
361
	 */
362
	public void setPath(String path) {
362
	public void setPath(String path) {
363
		if (GET.equals(getMethod())) {
363
        // We know that URL arguments should always be encoded in UTF-8 according to spec
364
			int index = path.indexOf(QRY_PFX);
364
        setPath(path, EncoderCache.URL_ARGUMENT_ENCODING);
365
			if (index > -1) {
365
    }
366
				setProperty(PATH, path.substring(0, index));
366
    
367
				parseArguments(path.substring(index + 1));
367
    /**
368
			} else {
368
     * Sets the Path attribute of the UrlConfig object Also calls parseArguments
369
				setProperty(PATH, path);
369
     * to extract and store any query arguments
370
			}
370
     * 
371
		} else {
371
     * @param path
372
			setProperty(PATH, path);
372
     *            The new Path value
373
		}
373
     * @param contentEncoding
374
	}
374
     *            The encoding used for the querystring parameter values
375
     */
376
    public void setPath(String path, String contentEncoding) {
377
        if (GET.equals(getMethod())) {
378
            int index = path.indexOf(QRY_PFX);
379
            if (index > -1) {
380
                setProperty(PATH, path.substring(0, index));
381
                // Parse the arguments in querystring, assuming specified encoding for values
382
                parseArguments(path.substring(index + 1), contentEncoding);
383
            } else {
384
                setProperty(PATH, path);
385
            }
386
        } else {
387
            setProperty(PATH, path);
388
        }
389
    }
375
390
376
	public String getPath() {
391
	public String getPath() {
377
		String p = getPropertyAsString(PATH);
392
		String p = getPropertyAsString(PATH);
Lines 462-481 Link Here
462
        this.addEncodedArgument(name, value, ARG_VAL_SEP);
477
        this.addEncodedArgument(name, value, ARG_VAL_SEP);
463
    }
478
    }
464
479
465
	public void addEncodedArgument(String name, String value, String metaData) {
480
	public void addEncodedArgument(String name, String value, String metaData, String contentEncoding) {
466
        if (log.isDebugEnabled()){
481
        if (log.isDebugEnabled()){
467
		    log.debug("adding argument: name: " + name + " value: " + value + " metaData: " + metaData);
482
		    log.debug("adding argument: name: " + name + " value: " + value + " metaData: " + metaData + " contentEncoding: " + contentEncoding);
468
        }
483
        }
469
470
        
484
        
471
		HTTPArgument arg = new HTTPArgument(name, value, metaData, true);
485
		HTTPArgument arg = null;
486
        if(contentEncoding != null) {
487
            arg = new HTTPArgument(name, value, metaData, true, contentEncoding);
488
        }
489
        else {
490
            arg = new HTTPArgument(name, value, metaData, true);
491
        }
472
492
473
		if (arg.getName().equals(arg.getEncodedName()) && arg.getValue().equals(arg.getEncodedValue())) {
493
        // Check if there are any difference between name and value and their encoded name and value
494
        String valueEncoded = null;
495
        if(contentEncoding != null) {
496
            try {
497
                valueEncoded = arg.getEncodedValue(contentEncoding);
498
            }
499
            catch (UnsupportedEncodingException e) {
500
                log.warn("Unable to get encoded value using encoding " + contentEncoding);
501
                valueEncoded = arg.getEncodedValue();
502
            }
503
        }
504
        else {
505
            valueEncoded = arg.getEncodedValue();
506
        }
507
        // If there is no difference, we mark it as not needing encoding
508
		if (arg.getName().equals(arg.getEncodedName()) && arg.getValue().equals(valueEncoded)) {
474
			arg.setAlwaysEncoded(false);
509
			arg.setAlwaysEncoded(false);
475
		}
510
		}
476
		this.getArguments().addArgument(arg);
511
		this.getArguments().addArgument(arg);
477
	}
512
	}
478
    
513
514
    public void addEncodedArgument(String name, String value, String metaData) {
515
        this.addEncodedArgument(name, value, metaData, null);
516
    }
517
479
    public void addNonEncodedArgument(String name, String value, String metadata) {
518
    public void addNonEncodedArgument(String name, String value, String metadata) {
480
        HTTPArgument arg = new HTTPArgument(name, value, metadata, false);
519
        HTTPArgument arg = new HTTPArgument(name, value, metadata, false);
481
        arg.setAlwaysEncoded(false);
520
        arg.setAlwaysEncoded(false);
Lines 757-778 Link Here
757
		return buf.toString();
796
		return buf.toString();
758
	}
797
	}
759
798
760
	// Mark Walsh 2002-08-03, modified to also parse a parameter name value
799
    // Mark Walsh 2002-08-03, modified to also parse a parameter name value
761
	// string, where string contains only the parameter name and no equal sign.
800
    // string, where string contains only the parameter name and no equal sign.
762
	/**
801
    /**
763
	 * This method allows a proxy server to send over the raw text from a
802
     * This method allows a proxy server to send over the raw text from a
764
	 * browser's output stream to be parsed and stored correctly into the
803
     * browser's output stream to be parsed and stored correctly into the
765
	 * UrlConfig object.
804
     * UrlConfig object.
766
	 * 
805
     * 
767
	 * For each name found, addArgument() is called
806
     * For each name found, addArgument() is called
768
	 * 
807
     * 
769
	 * @param queryString -
808
     * @param queryString -
770
	 *            the query string
809
     *            the query string
771
	 * 
810
     * @param contentEncoding -
772
	 */
811
     *            the content encoding of the query string. The query string might
773
	public void parseArguments(String queryString) {
812
     *            actually be the post body of a http post request.
774
		String[] args = JOrphanUtils.split(queryString, QRY_SEP);
813
     */
775
		for (int i = 0; i < args.length; i++) {
814
    public void parseArguments(String queryString, String contentEncoding) {
815
        String[] args = JOrphanUtils.split(queryString, QRY_SEP);
816
        for (int i = 0; i < args.length; i++) {
776
			// need to handle four cases: 
817
			// need to handle four cases: 
777
            // - string contains name=value
818
            // - string contains name=value
778
			// - string contains name=
819
			// - string contains name=
Lines 793-812 Link Here
793
				metaData = "";
834
				metaData = "";
794
                name=args[i];
835
                name=args[i];
795
                value="";
836
                value="";
796
			}
797
			if (name.length() > 0) {
798
                // The browser has already done the encoding, so save the values as is 
799
                HTTPArgument arg = new HTTPArgument(name, value, metaData, false);
800
                // and make sure they stay that way:
801
                arg.setAlwaysEncoded(false);
802
                // Note that URL.encode()/decode() do not follow RFC3986 entirely
803
				this.getArguments().addArgument(arg);
804
				// TODO: this leaves the arguments in encoded form, which may be difficult to read
805
                // if we can find proper coding methods, this could be tidied up 
806
            }
837
            }
807
		}
838
            if (name.length() > 0) {
808
	}
839
                // If we know the encoding, we can decode the argument value,
840
                // to make it easier to read for the user
841
                if(contentEncoding != null) {
842
                    addEncodedArgument(name, value, metaData, contentEncoding);
843
                }
844
                else {
845
                    // If we do not know the encoding, we just use the encoded value
846
                    // The browser has already done the encoding, so save the values as is
847
                    addNonEncodedArgument(name, value, metaData);
848
                }
849
            }
850
        }
851
    }
809
852
853
    public void parseArguments(String queryString) {
854
        // We do not know the content encoding of the query string
855
        parseArguments(queryString, null);
856
    }
857
810
	public String toString() {
858
	public String toString() {
811
		try {
859
		try {
812
			return this.getUrl().toString() + ((POST.equals(getMethod())) ? "\nQuery Data: " + getQueryString() : "");
860
			return this.getUrl().toString() + ((POST.equals(getMethod())) ? "\nQuery Data: " + getQueryString() : "");
Lines 1194-1199 Link Here
1194
				res.addSubResult(errorResult(new Exception("Maximum frame/iframe nesting depth exceeded."), res));
1242
				res.addSubResult(errorResult(new Exception("Maximum frame/iframe nesting depth exceeded."), res));
1195
			} else {
1243
			} else {
1196
				// If we followed redirects, we already have a container:
1244
				// If we followed redirects, we already have a container:
1245
                if(!areFollowingRedirect) {
1197
				HTTPSampleResult container = (HTTPSampleResult) (areFollowingRedirect ? res.getParent() : res);
1246
				HTTPSampleResult container = (HTTPSampleResult) (areFollowingRedirect ? res.getParent() : res);
1198
1247
1199
				// Only download page resources if we were not redirected.
1248
				// Only download page resources if we were not redirected.
Lines 1202-1207 Link Here
1202
				if(!wasRedirected) {
1251
				if(!wasRedirected) {
1203
					res = downloadPageResources(res, container, frameDepth);
1252
					res = downloadPageResources(res, container, frameDepth);
1204
				}
1253
				}
1254
                }
1205
			}
1255
			}
1206
		}
1256
		}
1207
		return res;
1257
		return res;
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/protocol/http/org/apache/jmeter/protocol/http/util/HTTPArgument.java (-28 / +56 lines)
Lines 88-125 Link Here
88
		this(name, value, false);
88
		this(name, value, false);
89
	}
89
	}
90
90
91
	public HTTPArgument(String name, String value, boolean alreadyEncoded) {
91
    public HTTPArgument(String name, String value, boolean alreadyEncoded) {
92
		setAlwaysEncoded(true);
92
        // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
93
		if (alreadyEncoded) {
93
        this(name, value, alreadyEncoded, EncoderCache.URL_ARGUMENT_ENCODING);
94
			try {
94
    }
95
				name = URLDecoder.decode(name, EncoderCache.URL_ARGUMENT_ENCODING);
96
				value = URLDecoder.decode(value, EncoderCache.URL_ARGUMENT_ENCODING);
97
			} catch (UnsupportedEncodingException e) {
98
				// UTF-8 unsupported? You must be joking!
99
				log.error("UTF-8 encoding not supported!");
100
				throw new Error(e.toString());
101
			}
102
		}
103
		setName(name);
104
		setValue(value);
105
		setMetaData("=");
106
	}
107
95
108
	public HTTPArgument(String name, String value, String metaData, boolean alreadyEncoded) {
96
    /**
109
		this(name, value, alreadyEncoded);
97
     * Construct a new HTTPArgument instance
110
		setMetaData(metaData);
98
     * 
111
	}
99
     * @param name the name of the parameter
100
     * @param value the value of the parameter
101
     * @param alreadyEncoded true if the name and value is already encoded
102
     * @param contentEncoding the encoding used for the parameter value
103
     */
104
    public HTTPArgument(String name, String value, boolean alreadyEncoded, String contentEncoding) {
105
        setAlwaysEncoded(true);
106
        if (alreadyEncoded) {
107
            try {
108
                // We assume the name is always encoded according to spec
109
                name = URLDecoder.decode(name, EncoderCache.URL_ARGUMENT_ENCODING);
110
                // The value is encoded in the specified encoding
111
                value = URLDecoder.decode(value, contentEncoding);
112
            } catch (UnsupportedEncodingException e) {
113
                log.error(contentEncoding + " encoding not supported!");
114
                throw new Error(e.toString());
115
            }
116
        }
117
        setName(name);
118
        setValue(value);
119
        setMetaData("=");
120
    }
112
121
113
	public HTTPArgument(Argument arg) {
122
    public HTTPArgument(String name, String value, String metaData, boolean alreadyEncoded) {
114
		this(arg.getName(), arg.getValue(), arg.getMetaData());
123
        // We assume the argument value is encoded according to the HTTP spec, i.e. UTF-8
115
	}
124
        this(name, value, metaData, alreadyEncoded, EncoderCache.URL_ARGUMENT_ENCODING);
125
    }
116
126
117
	/**
127
    /**
118
	 * Constructor for the Argument object
128
     * Construct a new HTTPArgument instance
119
	 */
129
     * 
120
	public HTTPArgument() {
130
     * @param name the name of the parameter
121
	}
131
     * @param value the value of the parameter
132
     * @param metaData the separator to use between name and value
133
     * @param alreadyEncoded true if the name and value is already encoded
134
     * @param contentEncoding the encoding used for the parameter value
135
     */
136
    public HTTPArgument(String name, String value, String metaData, boolean alreadyEncoded, String contentEncoding) {
137
        this(name, value, alreadyEncoded, contentEncoding);
138
        setMetaData(metaData);
139
    }
122
140
141
    public HTTPArgument(Argument arg) {
142
        this(arg.getName(), arg.getValue(), arg.getMetaData());
143
    }
144
145
    /**
146
     * Constructor for the Argument object
147
     */
148
    public HTTPArgument() {
149
    }
150
123
	/**
151
	/**
124
	 * Sets the Name attribute of the Argument object.
152
	 * Sets the Name attribute of the Argument object.
125
	 * 
153
	 * 
(-)C:/Documents and Settings/alf.hogemark/workspace/JMeter 2.2 official_2/src/core/org/apache/jmeter/util/JMeterVersion.java (-1 / +1 lines)
Lines 42-48 Link Here
42
	 * JMeterUtils This ensures that JMeterUtils always gets the correct
42
	 * JMeterUtils This ensures that JMeterUtils always gets the correct
43
	 * version, even if it is not re-compiled during the build.
43
	 * version, even if it is not re-compiled during the build.
44
	 */
44
	 */
45
	private static final String VERSION = "2.2.1";
45
	private static final String VERSION = "2.2.20070510";
46
46
47
	static final String COPYRIGHT = "Copyright (c) 1998-2007 The Apache Software Foundation";
47
	static final String COPYRIGHT = "Copyright (c) 1998-2007 The Apache Software Foundation";
48
48

Return to bug 42173