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

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

Return to bug 27780