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

(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/test/src/org/apache/jmeter/protocol/http/sampler/PostWriterTest.java (-64 / +607 lines)
Lines 26-32 Link Here
26
import java.net.HttpURLConnection;
26
import java.net.HttpURLConnection;
27
import java.net.MalformedURLException;
27
import java.net.MalformedURLException;
28
import java.net.URL;
28
import java.net.URL;
29
import java.net.URLConnection;
29
import java.net.URLEncoder;
30
import java.util.HashMap;
30
import java.util.HashMap;
31
import java.util.Map;
31
import java.util.Map;
32
32
Lines 38-57 Link Here
38
public class PostWriterTest extends TestCase {
38
public class PostWriterTest extends TestCase {
39
    
39
    
40
    private final static byte[] CRLF = { 0x0d, 0x0A };
40
    private final static byte[] CRLF = { 0x0d, 0x0A };
41
    private static byte[] TEST_FILE_CONTENT;
41
    
42
    
42
    private URLConnection connection;
43
    private StubURLConnection connection;
43
    private HTTPSampler sampler;
44
    private HTTPSampler sampler;
44
    private File temporaryFile;
45
    private File temporaryFile;
45
    
46
    
46
    protected void setUp() throws Exception {
47
    protected void setUp() throws Exception {
47
        connection = new StubURLConnection("http://fake_url/test");
48
        establishConnection();
48
        sampler = new HTTPSampler();// This must be the original (Java) HTTP sampler
49
        sampler = new HTTPSampler();// This must be the original (Java) HTTP sampler
49
        
50
        
51
        // Create the test file content
52
        TEST_FILE_CONTENT = new String("foo content &?=01234+56789-\u007c\u2aa1\u266a\u0153\u20a1\u0115\u0364\u00c5\u2052").getBytes("UTF-8");
53
50
        // create a temporary file to make sure we always have a file to give to the PostWriter 
54
        // create a temporary file to make sure we always have a file to give to the PostWriter 
51
        // Whereever we are or Whatever the current path is.
55
        // Whereever we are or Whatever the current path is.
52
        temporaryFile = File.createTempFile("foo", "txt");
56
        temporaryFile = File.createTempFile("foo", "txt");
53
        OutputStream output = new FileOutputStream(temporaryFile);
57
        OutputStream output = new FileOutputStream(temporaryFile);
54
        output.write("foo content".getBytes());
58
        output.write(TEST_FILE_CONTENT);
55
        output.flush();
59
        output.flush();
56
        output.close();
60
        output.close();
57
    }
61
    }
Lines 65-76 Link Here
65
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
69
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
66
     */
70
     */
67
    public void testSendPostData() throws IOException {
71
    public void testSendPostData() throws IOException {
68
        setupFilename(sampler);
72
        setupFilepart(sampler);
69
        setupCommons(sampler);
73
        String titleValue = "mytitle";
74
        String descriptionValue = "mydescription";
75
        setupFormData(sampler, titleValue, descriptionValue);
70
        
76
        
77
        // Test sending data with default encoding
78
        String contentEncoding = "";
79
        sampler.setContentEncoding(contentEncoding);        
80
        PostWriter.setHeaders(connection, sampler);
71
        PostWriter.sendPostData(connection, sampler);
81
        PostWriter.sendPostData(connection, sampler);
82
83
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
84
        byte[] expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, null, titleValue, descriptionValue, TEST_FILE_CONTENT);
85
//        checkContentLength(connection, expectedFormBody.length);        
86
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
87
        connection.disconnect();
88
89
        // Test sending data as ISO-8859-1
90
        establishConnection();
91
        contentEncoding = "ISO-8859-1";
92
        sampler.setContentEncoding(contentEncoding);        
93
        PostWriter.setHeaders(connection, sampler);
94
        PostWriter.sendPostData(connection, sampler);
95
96
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
97
        expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
98
//        checkContentLength(connection, expectedFormBody.length);        
99
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
100
        connection.disconnect();
72
        
101
        
73
        assertEquals(createExpectedOutputStream().toString(), connection.getOutputStream().toString());
102
        // Test sending data as UTF-8
103
        establishConnection();
104
        titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
105
        descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
106
        contentEncoding = "UTF-8";
107
        sampler.setContentEncoding(contentEncoding);        
108
        setupFormData(sampler, titleValue, descriptionValue);
109
        PostWriter.setHeaders(connection, sampler);
110
        PostWriter.sendPostData(connection, sampler);
111
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
112
        expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
113
//        checkContentLength(connection, expectedFormBody.length);        
114
//        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
115
        connection.disconnect();
116
117
        // Test sending UTF-8 data with ISO-8859-1 content encoding
118
        establishConnection();
119
        contentEncoding = "UTF-8";
120
        sampler.setContentEncoding("ISO-8859-1");
121
        PostWriter.setHeaders(connection, sampler);
122
        PostWriter.sendPostData(connection, sampler);
123
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
124
        expectedFormBody = createExpectedOutput(PostWriter.BOUNDARY, contentEncoding, titleValue, descriptionValue, TEST_FILE_CONTENT);
125
//        checkContentLength(connection, expectedFormBody.length);        
126
        checkArraysHaveDifferentContent(expectedFormBody, connection.getOutputStreamContent());
127
        connection.disconnect();
74
    }
128
    }
75
129
76
    /*
130
    /*
Lines 78-100 Link Here
78
     */
132
     */
79
    public void testSendPostData_NoFilename() throws IOException {
133
    public void testSendPostData_NoFilename() throws IOException {
80
        setupNoFilename(sampler);
134
        setupNoFilename(sampler);
81
        setupCommons(sampler);
135
        String titleValue = "mytitle";
136
        String descriptionValue = "mydescription";
137
        setupFormData(sampler, titleValue, descriptionValue);
82
138
139
        // Test sending data with default encoding
140
        String contentEncoding = "";
141
        sampler.setContentEncoding(contentEncoding);        
142
        PostWriter.setHeaders(connection, sampler);
83
        PostWriter.sendPostData(connection, sampler);
143
        PostWriter.sendPostData(connection, sampler);
84
        
144
        
85
        assertEquals("title=mytitle&description=mydescription", connection.getOutputStream().toString());
145
        checkContentTypeUrlEncoded(connection);
146
        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes();
147
        checkContentLength(connection, expectedUrl.length);
148
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
149
        expectedUrl = "title=mytitle&description=mydescription".getBytes("UTF-8");
150
        checkContentLength(connection, expectedUrl.length);
151
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
152
        connection.disconnect();
153
154
        // Test sending data as ISO-8859-1
155
        establishConnection();
156
        contentEncoding = "ISO-8859-1";
157
        sampler.setContentEncoding(contentEncoding);        
158
        PostWriter.setHeaders(connection, sampler);
159
        PostWriter.sendPostData(connection, sampler);
160
        
161
        checkContentTypeUrlEncoded(connection);
162
        expectedUrl = "title=mytitle&description=mydescription".getBytes(contentEncoding);
163
        checkContentLength(connection, expectedUrl.length);
164
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
165
        expectedUrl = "title=mytitle&description=mydescription".getBytes("UTF-8");
166
        checkContentLength(connection, expectedUrl.length);
167
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
168
        connection.disconnect();
86
    }
169
    }
87
170
88
    /*
171
    /*
172
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
173
     */
174
    public void testSendPostData_FileAsBody() throws IOException {
175
        setupFilepart(sampler, "", temporaryFile, "");
176
        
177
        // Check using default encoding
178
        PostWriter.setHeaders(connection, sampler);
179
        PostWriter.sendPostData(connection, sampler);
180
181
//        checkContentLength(connection, TEST_FILE_CONTENT.length);        
182
        checkArraysHaveSameContent(TEST_FILE_CONTENT, connection.getOutputStreamContent());
183
        connection.disconnect();
184
        
185
        // Check using UTF-8 encoding
186
        establishConnection();
187
        sampler.setContentEncoding("UTF-8");
188
        // File content is sent as binary, so the content encoding should not change the file data
189
        PostWriter.setHeaders(connection, sampler);
190
        PostWriter.sendPostData(connection, sampler);
191
        
192
//        checkContentLength(connection, TEST_FILE_CONTENT.length);        
193
        checkArraysHaveSameContent(TEST_FILE_CONTENT, connection.getOutputStreamContent());
194
        checkArraysHaveDifferentContent(new String(TEST_FILE_CONTENT).getBytes("UTF-8"), connection.getOutputStreamContent());
195
        
196
        // If we have both file as body, and form data, then only form data will be sent
197
        setupFormData(sampler);
198
        establishConnection();
199
        sampler.setContentEncoding("");
200
        PostWriter.setHeaders(connection, sampler);
201
        PostWriter.sendPostData(connection, sampler);
202
        
203
        checkContentTypeUrlEncoded(connection);
204
        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes();
205
        checkContentLength(connection, expectedUrl.length);
206
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
207
    }
208
209
    /*
210
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
211
     */
212
    public void testSendFileData_Multipart() throws IOException {
213
        String fileField = "upload";
214
        String mimeType = "text/plain";
215
        File file = temporaryFile;
216
        byte[] fileContent = TEST_FILE_CONTENT;
217
        setupFilepart(sampler, fileField, file, mimeType);
218
        
219
        // Test sending data with default encoding
220
        String contentEncoding = "";
221
        sampler.setContentEncoding(contentEncoding);        
222
        PostWriter.setHeaders(connection, sampler);
223
        PostWriter.sendPostData(connection, sampler);
224
225
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
226
        byte[] expectedFormBody = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, true, true);
227
//        checkContentLength(connection, expectedFormBody.length);
228
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
229
        connection.disconnect();
230
231
        // Test sending data as ISO-8859-1
232
        establishConnection();
233
        contentEncoding = "ISO-8859-1";
234
        sampler.setContentEncoding(contentEncoding);        
235
        PostWriter.setHeaders(connection, sampler);
236
        PostWriter.sendPostData(connection, sampler);
237
238
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
239
        expectedFormBody = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, true, true);
240
//        checkContentLength(connection, expectedFormBody.length);        
241
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
242
        connection.disconnect();
243
        
244
        // Test sending data as UTF-8
245
        establishConnection();
246
        fileField = "some_file_field";
247
        mimeType = "image/png";
248
        contentEncoding = "UTF-8";
249
        sampler.setContentEncoding(contentEncoding);        
250
        setupFilepart(sampler, fileField, file, mimeType);
251
        PostWriter.setHeaders(connection, sampler);
252
        PostWriter.sendPostData(connection, sampler);
253
254
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
255
        expectedFormBody = createExpectedFilepartOutput(PostWriter.BOUNDARY, fileField, file, mimeType, fileContent, true, true);
256
//        checkContentLength(connection, expectedFormBody.length);        
257
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
258
        connection.disconnect();
259
    }
260
261
    /*
262
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
263
     */
264
    public void testSendFormData_Multipart() throws IOException {
265
        String titleField = "title";
266
        String titleValue = "mytitle";
267
        String descriptionField = "description";
268
        String descriptionValue = "mydescription";
269
        setupFormData(sampler, titleValue, descriptionValue);
270
        // Tell sampler to do multipart, even if we have no files to upload
271
//        sampler.setDoMultipartPost(true); this is not available in current version in svn
272
273
        // Test sending data with default encoding
274
        String contentEncoding = "";
275
        sampler.setContentEncoding(contentEncoding);        
276
        PostWriter.setHeaders(connection, sampler);
277
        PostWriter.sendPostData(connection, sampler);
278
279
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
280
        byte[] expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, null, titleField, titleValue, descriptionField, descriptionValue, true, true);
281
        checkContentLength(connection, expectedFormBody.length);
282
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
283
        connection.disconnect();
284
285
        // Test sending data as ISO-8859-1
286
        establishConnection();
287
        contentEncoding = "ISO-8859-1";
288
        sampler.setContentEncoding(contentEncoding);        
289
        PostWriter.setHeaders(connection, sampler);
290
        PostWriter.sendPostData(connection, sampler);
291
292
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
293
        expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
294
        checkContentLength(connection, expectedFormBody.length);
295
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
296
        connection.disconnect();
297
        
298
        // Test sending data as ISO-8859-1, with values that need to be urlencoded
299
        establishConnection();
300
        titleValue = "mytitle+123 456&yes";
301
        descriptionValue = "mydescription and some spaces";
302
        contentEncoding = "ISO-8859-1";
303
        sampler.setContentEncoding(contentEncoding);        
304
        setupFormData(sampler, titleValue, descriptionValue);
305
        PostWriter.setHeaders(connection, sampler);
306
        PostWriter.sendPostData(connection, sampler);
307
308
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
309
        expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
310
        checkContentLength(connection, expectedFormBody.length);
311
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
312
        connection.disconnect();
313
        
314
        // Test sending data as UTF-8
315
        establishConnection();
316
        titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
317
        descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
318
        contentEncoding = "UTF-8";
319
        sampler.setContentEncoding(contentEncoding);        
320
        setupFormData(sampler, titleValue, descriptionValue);
321
        PostWriter.setHeaders(connection, sampler);
322
        PostWriter.sendPostData(connection, sampler);
323
324
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
325
        expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
326
        checkContentLength(connection, expectedFormBody.length);
327
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
328
        connection.disconnect();
329
330
        // Test sending data as UTF-8, with values that needs to be urlencoded
331
        establishConnection();
332
        titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
333
        descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
334
        contentEncoding = "UTF-8";
335
        sampler.setContentEncoding(contentEncoding);        
336
        setupFormData(sampler, titleValue, descriptionValue);
337
        PostWriter.setHeaders(connection, sampler);
338
        PostWriter.sendPostData(connection, sampler);
339
340
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
341
        expectedFormBody = createExpectedFormdataOutput(PostWriter.BOUNDARY, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, true);
342
        checkContentLength(connection, expectedFormBody.length);
343
        checkArraysHaveSameContent(expectedFormBody, connection.getOutputStreamContent());
344
        connection.disconnect();
345
    }
346
    
347
    /*
348
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(URLConnection, HTTPSampler)'
349
     */
350
    public void testSendFormData_Urlencoded() throws IOException {
351
        String titleValue = "mytitle";
352
        String descriptionValue = "mydescription";
353
        setupFormData(sampler, titleValue, descriptionValue);
354
355
        // Test sending data with default encoding
356
        String contentEncoding = "";
357
        sampler.setContentEncoding(contentEncoding);        
358
        PostWriter.setHeaders(connection, sampler);
359
        PostWriter.sendPostData(connection, sampler);
360
        
361
        checkContentTypeUrlEncoded(connection);
362
        byte[] expectedUrl = "title=mytitle&description=mydescription".getBytes();
363
        checkContentLength(connection, expectedUrl.length);
364
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
365
        connection.disconnect();
366
367
        // Test sending data as ISO-8859-1
368
        establishConnection();
369
        contentEncoding = "ISO-8859-1";
370
        sampler.setContentEncoding(contentEncoding);        
371
        PostWriter.setHeaders(connection, sampler);
372
        PostWriter.sendPostData(connection, sampler);
373
        
374
        checkContentTypeUrlEncoded(connection);
375
        expectedUrl = "title=mytitle&description=mydescription".getBytes("ISO-8859-1");
376
        checkContentLength(connection, expectedUrl.length);
377
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
378
        expectedUrl = "title=mytitle&description=mydescription".getBytes("UTF-8");
379
        checkContentLength(connection, expectedUrl.length);
380
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
381
        connection.disconnect();
382
        
383
        // Test sending data as ISO-8859-1, with values that need to be urlencoded
384
        establishConnection();
385
        titleValue = "mytitle+123 456&yes";
386
        descriptionValue = "mydescription and some spaces";
387
        contentEncoding = "ISO-8859-1";
388
        sampler.setContentEncoding(contentEncoding);        
389
        setupFormData(sampler, titleValue, descriptionValue);
390
        PostWriter.setHeaders(connection, sampler);
391
        PostWriter.sendPostData(connection, sampler);
392
        
393
        checkContentTypeUrlEncoded(connection);
394
        String expectedString = "title=" + URLEncoder.encode(titleValue, "UTF-8") + "&description=" + URLEncoder.encode(descriptionValue, "UTF-8");
395
        expectedUrl = expectedString.getBytes("ISO-8859-1");
396
        checkContentLength(connection, expectedUrl.length);
397
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
398
        expectedUrl = expectedString.getBytes("UTF-8");
399
        checkContentLength(connection, expectedUrl.length);
400
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
401
        String unencodedString = "title=" + titleValue + "&description=" + descriptionValue;
402
        byte[] unexpectedUrl = unencodedString.getBytes("UTF-8");
403
        checkArraysHaveDifferentContent(unexpectedUrl, connection.getOutputStreamContent());
404
        connection.disconnect();
405
        
406
        // Test sending data as UTF-8
407
        establishConnection();
408
        titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
409
        descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
410
        contentEncoding = "UTF-8";
411
        sampler.setContentEncoding(contentEncoding);        
412
        setupFormData(sampler, titleValue, descriptionValue);
413
        PostWriter.setHeaders(connection, sampler);
414
        PostWriter.sendPostData(connection, sampler);
415
        
416
        checkContentTypeUrlEncoded(connection);
417
        expectedString = "title=" + URLEncoder.encode(titleValue, "UTF-8") + "&description=" + URLEncoder.encode(descriptionValue, "UTF-8");
418
        expectedUrl = expectedString.getBytes("UTF-8");
419
        checkContentLength(connection, expectedUrl.length);
420
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
421
        connection.disconnect();
422
423
        // Test sending data as UTF-8, with values that needs to be urlencoded
424
        establishConnection();
425
        titleValue = "mytitle\u0153+\u20a1 \u0115&yes\u00c5";
426
        descriptionValue = "mydescription \u0153 \u20a1 \u0115 \u00c5";
427
        contentEncoding = "UTF-8";
428
        sampler.setContentEncoding(contentEncoding);        
429
        setupFormData(sampler, titleValue, descriptionValue);
430
        PostWriter.setHeaders(connection, sampler);
431
        PostWriter.sendPostData(connection, sampler);
432
433
        checkContentTypeUrlEncoded(connection);
434
        expectedString = "title=" + URLEncoder.encode(titleValue, "UTF-8") + "&description=" + URLEncoder.encode(descriptionValue, "UTF-8");
435
        expectedUrl = expectedString.getBytes("UTF-8");
436
        checkContentLength(connection, expectedUrl.length);
437
        checkArraysHaveSameContent(expectedUrl, connection.getOutputStreamContent());
438
        unencodedString = "title=" + titleValue + "&description=" + descriptionValue;
439
        unexpectedUrl = unencodedString.getBytes("UTF-8");
440
        checkArraysHaveDifferentContent(unexpectedUrl, connection.getOutputStreamContent());
441
        connection.disconnect();
442
    }
443
444
    /*
89
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.setHeaders(URLConnection, HTTPSampler)'
445
     * Test method for 'org.apache.jmeter.protocol.http.sampler.PostWriter.setHeaders(URLConnection, HTTPSampler)'
90
     */
446
     */
91
    public void testSetHeaders() throws IOException {
447
    public void testSetHeaders() throws IOException {
92
        setupFilename(sampler);
448
        setupFilepart(sampler);
93
        setupCommons(sampler);
449
        setupFormData(sampler);
94
        
450
        
95
        PostWriter.setHeaders(connection, sampler);
451
        PostWriter.setHeaders(connection, sampler);
96
        
452
        checkContentTypeMultipart(connection, PostWriter.BOUNDARY);
97
        assertEquals("multipart/form-data; boundary=" + PostWriter.BOUNDARY, connection.getRequestProperty("Content-Type"));
98
    }
453
    }
99
454
100
    /*
455
    /*
Lines 102-113 Link Here
102
     */
457
     */
103
    public void testSetHeaders_NoFilename() throws IOException {
458
    public void testSetHeaders_NoFilename() throws IOException {
104
        setupNoFilename(sampler);
459
        setupNoFilename(sampler);
105
        setupCommons(sampler);
460
        setupFormData(sampler);
106
        
461
        
107
        PostWriter.setHeaders(connection, sampler);
462
        PostWriter.setHeaders(connection, sampler);
108
        
463
        checkContentTypeUrlEncoded(connection);
109
        assertEquals(HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED, connection.getRequestProperty("Content-Type"));
464
        checkContentLength(connection, "title=mytitle&description=mydescription".length());
110
        assertEquals("39", connection.getRequestProperty("Content-Length"));
111
    }
465
    }
112
466
113
    /**
467
    /**
Lines 117-212 Link Here
117
     * @throws IOException
471
     * @throws IOException
118
     */
472
     */
119
    private void setupNoFilename(HTTPSampler httpSampler) {
473
    private void setupNoFilename(HTTPSampler httpSampler) {
120
        httpSampler.setFilename("");
474
        setupFilepart(sampler, "upload", null, "application/octet-stream");
121
        httpSampler.setMimetype("application/octet-stream");
122
    }
475
    }
123
476
124
    /**
477
    /**
125
     * setup commons parts of HTTPSampler with a filename.
478
     * Setup the filepart with default values
126
     * 
479
     * 
127
     * @param httpSampler
480
     * @param httpSampler
128
     * @throws IOException
129
     */
481
     */
130
    private void setupFilename(HTTPSampler httpSampler) {
482
    private void setupFilepart(HTTPSampler httpSampler) {
131
        // httpSampler.setFilename("test/src/org/apache/jmeter/protocol/http/sampler/foo.txt");
483
        setupFilepart(sampler, "upload", temporaryFile, "text/plain");
132
        httpSampler.setFilename(temporaryFile.getAbsolutePath());
133
        httpSampler.setMimetype("text/plain");
134
    }
484
    }
135
485
136
    /**
486
    /**
137
     * setup commons parts of HTTPSampler form test* methods.
487
     * Setup the filepart with specified values
138
     * 
488
     * 
139
     * @param httpSampler
489
     * @param httpSampler
140
     * @throws IOException
141
     */
490
     */
142
    private void setupCommons(HTTPSampler httpSampler) {
491
    private void setupFilepart(HTTPSampler httpSampler, String fileField, File file, String mimeType) {
143
        httpSampler.setFileField("upload");
492
        httpSampler.setFileField(fileField);
493
        if(file != null) {
494
            httpSampler.setFilename(file.getAbsolutePath());
495
        }
496
        else {
497
            httpSampler.setFilename("");
498
        }
499
        httpSampler.setMimetype(mimeType);
500
    }
501
502
    /**
503
     * Setup the form data with default values
504
     * 
505
     * @param httpSampler
506
     */
507
    private void setupFormData(HTTPSampler httpSampler) {
508
        setupFormData(httpSampler, "mytitle", "mydescription");
509
    }
510
511
    /**
512
     * Setup the form data with specified values
513
     * 
514
     * @param httpSampler
515
     */
516
    private void setupFormData(HTTPSampler httpSampler, String titleValue, String descriptionValue) {
144
        Arguments args = new Arguments();
517
        Arguments args = new Arguments();
145
        args.addArgument(new HTTPArgument("title", "mytitle"));
518
        args.addArgument(new HTTPArgument("title", titleValue));
146
        args.addArgument(new HTTPArgument("description", "mydescription"));
519
        args.addArgument(new HTTPArgument("description", descriptionValue));
147
        httpSampler.setArguments(args);
520
        httpSampler.setArguments(args);
148
    }
521
    }
149
    
522
    
523
    private void establishConnection() throws MalformedURLException { 
524
        connection = new StubURLConnection("http://fake_url/test");
525
    }
526
    
150
    /**
527
    /**
151
     * Create the expected output with CRLF. 
528
     * Create the expected output post body for form data and file multiparts
529
     * with default values for field names
152
     */
530
     */
153
    private OutputStream createExpectedOutputStream() throws IOException {
531
    private byte[] createExpectedOutput(
154
        /*
532
            String boundaryString,
155
        -----------------------------7d159c1302d0y0
533
            String contentEncoding,
156
        Content-Disposition: form-data; name="title"
534
            String titleValue,
535
            String descriptionValue,
536
            byte[] fileContent) throws IOException {
537
        return createExpectedOutput(boundaryString, contentEncoding, "title", titleValue, "description", descriptionValue, "upload", fileContent);
538
    }
157
539
158
        mytitle
540
    /**
159
        -----------------------------7d159c1302d0y0
541
     * Create the expected output post body for form data and file multiparts
160
        Content-Disposition: form-data; name="description"
542
     * with specified values
543
     */
544
    private byte[] createExpectedOutput(
545
            String boundaryString,
546
            String contentEncoding,
547
            String titleField,
548
            String titleValue,
549
            String descriptionField,
550
            String descriptionValue,
551
            String fileField,
552
            byte[] fileContent) throws IOException {
553
        // Create the multiparts
554
        byte[] formdataMultipart = createExpectedFormdataOutput(boundaryString, contentEncoding, titleField, titleValue, descriptionField, descriptionValue, true, false);
555
        byte[] fileMultipart = createExpectedFilepartOutput(boundaryString, fileField, temporaryFile, "text/plain", fileContent, false, true);
556
        
557
        // Join the two multiparts
558
        ByteArrayOutputStream output = new ByteArrayOutputStream();
559
        output.write(formdataMultipart);
560
        output.write(fileMultipart);
561
        
562
        output.flush();
563
        output.close();
564
        
565
        return output.toByteArray();
566
    }
161
567
162
        mydescription
568
    /**
163
        -----------------------------7d159c1302d0y0
569
     * Create the expected output multipart/form-data, with only form data,
164
        Content-Disposition: form-data; name="upload"; filename="test/src/org/apache/jmeter/protocol/http/sampler/foo.txt"
570
     * and no file multipart
165
        Content-Type: plain/text
571
     * 
166
572
     * @param lastMultipart true if this is the last multipart in the request
167
        foo content
573
     */
168
        -----------------------------7d159c1302d0y0--
574
    private byte[] createExpectedFormdataOutput(
169
        */
575
            String boundaryString,
170
576
            String contentEncoding,
171
        final OutputStream output = new ByteArrayOutputStream();
577
            String titleField,
172
        output.write("-----------------------------7d159c1302d0y0".getBytes());
578
            String titleValue,
579
            String descriptionField,
580
            String descriptionValue,
581
            boolean firstMultipart,
582
            boolean lastMultipart) throws IOException {
583
        // The encoding used for http headers and control information
584
        final String httpEncoding = "ISO-8859-1";
585
        final byte[] DASH_DASH = new String("--").getBytes(httpEncoding);
586
        
587
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
588
        if(firstMultipart) {
589
            output.write(DASH_DASH);
590
            output.write(boundaryString.getBytes(httpEncoding));
591
            output.write(CRLF);
592
        }
593
        output.write("Content-Disposition: form-data; name=\"".getBytes(httpEncoding));
594
        output.write(titleField.getBytes(httpEncoding));
595
        output.write("\"".getBytes(httpEncoding));
173
        output.write(CRLF);
596
        output.write(CRLF);
174
        output.write("Content-Disposition: form-data; name=\"title\"".getBytes());
175
        output.write(CRLF);
597
        output.write(CRLF);
598
        if(contentEncoding != null) {
599
            output.write(titleValue.getBytes(contentEncoding));
600
        }
601
        else {
602
            output.write(titleValue.getBytes());
603
        }
176
        output.write(CRLF);
604
        output.write(CRLF);
177
        output.write("mytitle".getBytes());
605
        output.write(DASH_DASH);
606
        output.write(boundaryString.getBytes(httpEncoding));
178
        output.write(CRLF);
607
        output.write(CRLF);
179
        output.write("-----------------------------7d159c1302d0y0".getBytes());
608
        output.write("Content-Disposition: form-data; name=\"".getBytes(httpEncoding));
609
        output.write(descriptionField.getBytes(httpEncoding));
610
        output.write("\"".getBytes(httpEncoding));
180
        output.write(CRLF);
611
        output.write(CRLF);
181
        output.write("Content-Disposition: form-data; name=\"description\"".getBytes());
182
        output.write(CRLF);
612
        output.write(CRLF);
613
        if(contentEncoding != null) {
614
            output.write(descriptionValue.getBytes(contentEncoding));
615
        }
616
        else {
617
            output.write(descriptionValue.getBytes());
618
        }
183
        output.write(CRLF);
619
        output.write(CRLF);
184
        output.write("mydescription".getBytes());
620
        output.write(DASH_DASH);
621
        output.write(boundaryString.getBytes(httpEncoding));
622
        if(lastMultipart) {
623
            output.write(DASH_DASH);
624
        }
185
        output.write(CRLF);
625
        output.write(CRLF);
186
        output.write("-----------------------------7d159c1302d0y0".getBytes());
626
                
187
        output.write(CRLF);
627
        output.flush();
628
        output.close();
629
630
        return output.toByteArray();
631
    }
632
    
633
    /**
634
     * Create the expected file multipart
635
     * 
636
     * @param lastMultipart true if this is the last multipart in the request
637
     */
638
    private byte[] createExpectedFilepartOutput(
639
            String boundaryString,
640
            String fileField,
641
            File file,
642
            String mimeType,
643
            byte[] fileContent,
644
            boolean firstMultipart,
645
            boolean lastMultipart) throws IOException {
646
        // The encoding used for http headers and control information
647
        final String httpEncoding = "ISO-8859-1";
648
        final byte[] DASH_DASH = new String("--").getBytes(httpEncoding);
649
        
650
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
651
        if(firstMultipart) {
652
            output.write(DASH_DASH);
653
            output.write(boundaryString.getBytes(httpEncoding));
654
            output.write(CRLF);
655
        }
188
        // replace all backslash with double backslash
656
        // replace all backslash with double backslash
189
        String filename = temporaryFile.getAbsolutePath().replaceAll("\\\\","\\\\\\\\");
657
        String filename = file.getAbsolutePath().replaceAll("\\\\","\\\\\\\\");
190
        output.write(("Content-Disposition: form-data; name=\"upload\"; filename=\"" + filename + "\"").getBytes());
658
        output.write("Content-Disposition: form-data; name=\"".getBytes(httpEncoding));
659
        output.write(fileField.getBytes(httpEncoding));
660
        output.write(("\"; filename=\"" + filename + "\"").getBytes(httpEncoding));
191
        output.write(CRLF);
661
        output.write(CRLF);
192
        output.write("Content-Type: text/plain".getBytes());
662
        output.write("Content-Type: ".getBytes(httpEncoding));
663
        output.write(mimeType.getBytes(httpEncoding));
193
        output.write(CRLF);
664
        output.write(CRLF);
194
        output.write(CRLF);
665
        output.write(CRLF);
195
        output.write("foo content".getBytes());
666
        output.write(fileContent);
196
        output.write(CRLF);
667
        output.write(CRLF);
197
        output.write("-----------------------------7d159c1302d0y0--".getBytes());
668
        output.write(DASH_DASH);
669
        output.write(boundaryString.getBytes(httpEncoding));
670
        if(lastMultipart) {
671
            output.write(DASH_DASH);
672
        }
198
        output.write(CRLF);
673
        output.write(CRLF);
674
        
199
        output.flush();
675
        output.flush();
200
        output.close();
676
        output.close();
201
        return output;
677
678
        return output.toByteArray();
202
    }
679
    }
203
680
204
    /**
681
    /**
682
     * Check that the the two byte arrays have identical content
683
     * 
684
     * @param expected
685
     * @param actual
686
     */
687
    private void checkArraysHaveSameContent(byte[] expected, byte[] actual) {
688
        if(expected != null && actual != null) {
689
            if(expected.length != actual.length) {
690
                fail("arrays have different length, expected is " + expected.length + ", actual is " + actual.length);
691
            }
692
            else {
693
                for(int i = 0; i < expected.length; i++) {
694
                    if(expected[i] != actual[i]) {
695
                        fail("byte at position " + i + " is different, expected is " + expected[i] + ", actual is " + actual[i]);
696
                    }
697
                }
698
            }
699
        }
700
        else {
701
            fail("expected or actual byte arrays were null");
702
        }
703
    }
704
705
    /**
706
     * Check that the the two byte arrays different content
707
     * 
708
     * @param expected
709
     * @param actual
710
     */
711
    private void checkArraysHaveDifferentContent(byte[] expected, byte[] actual) {
712
        if(expected != null && actual != null) {
713
            if(expected.length == actual.length) {
714
                boolean allSame = true;
715
                for(int i = 0; i < expected.length; i++) {
716
                    if(expected[i] != actual[i]) {
717
                        allSame = false;
718
                        break;
719
                    }
720
                }
721
                if(allSame) {
722
                    fail("all bytes were equal");
723
                }
724
            }
725
        }
726
        else {
727
            fail("expected or actual byte arrays were null");
728
        }
729
    }
730
    
731
    private void checkContentTypeMultipart(HttpURLConnection connection, String boundaryString) {
732
        assertEquals("multipart/form-data; boundary=" + boundaryString, connection.getRequestProperty("Content-Type"));
733
    }
734
735
    private void checkContentTypeUrlEncoded(HttpURLConnection connection) {
736
        assertEquals(HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED, connection.getRequestProperty("Content-Type"));
737
    }
738
739
    private void checkContentLength(HttpURLConnection connection, int length) {
740
        assertEquals(Integer.toString(length), connection.getRequestProperty("Content-Length"));
741
    }
742
743
    /**
205
     * Mock an HttpURLConnection.
744
     * Mock an HttpURLConnection.
206
     * extends HttpURLConnection instead of just URLConnection because there is a cast in PostWriter.
745
     * extends HttpURLConnection instead of just URLConnection because there is a cast in PostWriter.
207
     */
746
     */
208
    private static class StubURLConnection extends HttpURLConnection {
747
    private static class StubURLConnection extends HttpURLConnection {
209
        private OutputStream output = new ByteArrayOutputStream();
748
        private ByteArrayOutputStream output = new ByteArrayOutputStream();
210
        private Map properties = new HashMap();
749
        private Map properties = new HashMap();
211
        
750
        
212
        public StubURLConnection(String url) throws MalformedURLException {
751
        public StubURLConnection(String url) throws MalformedURLException {
Lines 234-238 Link Here
234
        public void setRequestProperty(String key, String value) {
773
        public void setRequestProperty(String key, String value) {
235
            properties.put(key, value);
774
            properties.put(key, value);
236
        }
775
        }
776
        
777
        public byte[] getOutputStreamContent() {
778
            return output.toByteArray();
779
        }
237
    }
780
    }
238
}
781
}

Return to bug 27780