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

(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/test/src/org/apache/jmeter/protocol/http/proxy/TestHttpRequestHdr.java (-36 / +128 lines)
Lines 18-65 Link Here
18
18
19
package org.apache.jmeter.protocol.http.proxy;
19
package org.apache.jmeter.protocol.http.proxy;
20
20
21
import java.io.ByteArrayInputStream;
22
import java.io.IOException;
23
21
import org.apache.jmeter.config.Arguments;
24
import org.apache.jmeter.config.Arguments;
22
import org.apache.jmeter.junit.JMeterTestCase;
25
import org.apache.jmeter.junit.JMeterTestCase;
23
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
26
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
24
import org.apache.jmeter.testelement.property.JMeterProperty;
27
import org.apache.jmeter.protocol.http.util.HTTPArgument;
25
import org.apache.jmeter.testelement.property.PropertyIterator;
26
28
27
public class TestHttpRequestHdr  extends JMeterTestCase {
29
public class TestHttpRequestHdr  extends JMeterTestCase {
28
		public TestHttpRequestHdr(String name) {
30
    public TestHttpRequestHdr(String name) {
29
			super(name);
31
        super(name);
30
		}
32
    }
31
33
32
		public void setUp() {
34
    public void testRepeatedArguments() throws Exception {
33
		}
35
        // A HTTP GET request
36
        String TEST_GET_REQ = 
37
            "GET http://localhost/matrix.html" 
38
            + "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= "
39
            + "HTTP/1.0\n\n";
40
        HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8");
41
        assertEquals(HTTPSamplerBase.GET, s.getMethod());
34
42
35
		public void testRepeatedArguments() throws Exception {
43
        // Check arguments
36
			String TEST_REQ = 
44
        Arguments arguments = s.getArguments();
37
                "GET http://localhost/matrix.html" 
45
        assertEquals(13, arguments.getArgumentCount());
38
                + "?update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= "
46
        checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false);
39
                + "HTTP/1.0\n\n";
47
        checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false);
40
			HttpRequestHdr req = new HttpRequestHdr();
48
        checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false);
41
			req.parse(new java.io.ByteArrayInputStream(TEST_REQ.getBytes()));
49
        checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false);
42
			HTTPSamplerBase s = req.getSampler();
50
        checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false);
43
			assertEquals(13,s.getArguments().getArguments().size());
51
        checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false);
44
		}
52
        checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false);
53
        checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false);
54
        checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false);
55
        checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false);
56
        checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false);
57
        checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false);
58
        // I see that the value gets trimmed, not sure if that is correct
59
        checkArgument((HTTPArgument)arguments.getArgument(12), "d", "", false);
60
61
        // A HTTP POST request
62
        String postBody = "update=yes&d=1&d=2&d=&d=&d=&d=&d=&d=1&d=2&d=1&d=&d= ";
63
        String TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n"
64
                + "Content-type: "
65
                + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
66
                + "; charset=UTF-8\n"
67
                + postBody;
68
        s = getSamplerForRequest(TEST_POST_REQ, "UTF-8");
69
        assertEquals(HTTPSamplerBase.POST, s.getMethod());
70
71
        // Check arguments
72
        arguments = s.getArguments();
73
        assertEquals(13, arguments.getArgumentCount());
74
        checkArgument((HTTPArgument)arguments.getArgument(0), "update", "yes", false);
75
        checkArgument((HTTPArgument)arguments.getArgument(1), "d", "1", false);
76
        checkArgument((HTTPArgument)arguments.getArgument(2), "d", "2", false);
77
        checkArgument((HTTPArgument)arguments.getArgument(3), "d", "", false);
78
        checkArgument((HTTPArgument)arguments.getArgument(4), "d", "", false);
79
        checkArgument((HTTPArgument)arguments.getArgument(5), "d", "", false);
80
        checkArgument((HTTPArgument)arguments.getArgument(6), "d", "", false);
81
        checkArgument((HTTPArgument)arguments.getArgument(7), "d", "", false);
82
        checkArgument((HTTPArgument)arguments.getArgument(8), "d", "1", false);
83
        checkArgument((HTTPArgument)arguments.getArgument(9), "d", "2", false);
84
        checkArgument((HTTPArgument)arguments.getArgument(10), "d", "1", false);
85
        checkArgument((HTTPArgument)arguments.getArgument(11), "d", "", false);
86
        checkArgument((HTTPArgument)arguments.getArgument(12), "d", " ", false);
87
88
        // A HTTP POST request, with content-type text/plain
89
        TEST_POST_REQ = "POST http://localhost/matrix.html HTTP/1.0\n"
90
                + "Content-type: text/plain; charset=UTF-8\n"
91
                + postBody;
92
        s = getSamplerForRequest(TEST_POST_REQ, "UTF-8");
93
        assertEquals(HTTPSamplerBase.POST, s.getMethod());
94
95
        // Check arguments
96
        // We should have one argument, with the value equal to the post body
97
        arguments = s.getArguments();
98
        assertEquals(1, arguments.getArgumentCount());
99
        checkArgument((HTTPArgument)arguments.getArgument(0), "", postBody, false);
100
    }
45
        
101
        
46
        // TODO: will need changing if arguments can be saved in decoded form 
102
    // TODO: will need changing if arguments can be saved in decoded form 
47
        public void testEncodedArguments() throws Exception {
103
    public void testEncodedArguments() throws Exception {
48
            String TEST_REQ = 
104
            // A HTTP GET request
49
                "GET http://localhost:80/matrix.html"
105
        String TEST_GET_REQ = "GET http://localhost:80/matrix.html"
50
                + "?abc"
106
            + "?abc"
51
                + "?SPACE=a+b"
107
            + "?SPACE=a+b"
52
                +"&space=a%20b"
108
            + "&space=a%20b"
53
                +"&query=What?"
109
            + "&query=What?"
54
                + " HTTP/1.1\n\n";
110
            + " HTTP/1.1\n\n";
55
            HttpRequestHdr req = new HttpRequestHdr();
111
        HTTPSamplerBase s = getSamplerForRequest(TEST_GET_REQ, "UTF-8");
56
            req.parse(new java.io.ByteArrayInputStream(TEST_REQ.getBytes()));
112
        assertEquals(HTTPSamplerBase.GET, s.getMethod());
57
            Arguments arguments = req.getSampler().getArguments();
113
58
            assertEquals(3,arguments.getArguments().size());
114
        // Check arguments
59
            PropertyIterator pi= arguments.iterator();
115
        Arguments arguments = s.getArguments();
60
            JMeterProperty next;
116
        assertEquals(3, arguments.getArgumentCount());
61
            next = pi.next(); assertEquals("abc?SPACE=a+b",next.getStringValue());
117
        checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false);
62
            next = pi.next(); assertEquals("space=a%20b",next.getStringValue());
118
        checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false);
63
            next = pi.next(); assertEquals("query=What?",next.getStringValue());
119
        checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false);
64
        }
120
121
        // A HTTP POST request
122
        String postBody = "abc?SPACE=a+b&space=a%20b&query=What?";
123
        String TEST_POST_REQ = "POST http://localhost:80/matrix.html HTTP/1.1\n"
124
            + "Content-type: "
125
            + HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED
126
            + "; charset=UTF-8\n"
127
            + postBody;
128
        s = getSamplerForRequest(TEST_POST_REQ, "UTF-8");
129
        assertEquals(HTTPSamplerBase.POST, s.getMethod());
130
        
131
        // Check arguments
132
        arguments = s.getArguments();
133
        assertEquals(3, arguments.getArgumentCount());
134
        checkArgument((HTTPArgument)arguments.getArgument(0), "abc?SPACE", "a+b", false);
135
        checkArgument((HTTPArgument)arguments.getArgument(1), "space", "a%20b", false);
136
        checkArgument((HTTPArgument)arguments.getArgument(2), "query", "What?", false);
137
    }
138
139
    private HTTPSamplerBase getSamplerForRequest(String request, String contentEncoding)
140
            throws IOException {
141
        HttpRequestHdr req = new HttpRequestHdr();
142
        ByteArrayInputStream bis = new ByteArrayInputStream(request.getBytes(contentEncoding));
143
        req.parse(bis);
144
        bis.close();
145
        return req.getSampler();
146
    }
147
    
148
    private void checkArgument(
149
            HTTPArgument arg,
150
            String expectedName,
151
            String expectedValue,
152
            boolean expectedEncoded) {
153
        assertEquals(expectedName, arg.getName());
154
        assertEquals(expectedValue, arg.getValue());
155
        assertEquals(expectedEncoded, arg.isAlwaysEncoded());
156
    }
65
}
157
}
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/proxy/HttpRequestHdr.java (-30 / +45 lines)
Lines 136-142 Link Here
136
				readLength++;
136
				readLength++;
137
			}
137
			}
138
		}
138
		}
139
		postData = line.toString().trim();
139
		postData = line.toString();
140
        if (log.isDebugEnabled()){
140
        if (log.isDebugEnabled()){
141
    		log.debug("postData: " + postData);
141
    		log.debug("postData: " + postData);
142
    		log.debug("Request: " + clientRequest.toString());
142
    		log.debug("Request: " + clientRequest.toString());
Lines 230-240 Link Here
230
		if (contentTypeHeader != null) {
230
		if (contentTypeHeader != null) {
231
			return contentTypeHeader.getValue();
231
			return contentTypeHeader.getValue();
232
		}
232
		}
233
		return ""; // $NON-NLS-1$
233
        else {
234
            return null;
235
        }
234
	}
236
	}
235
237
236
    private boolean isMultipart(String contentType) {
238
    private boolean isMultipart(String contentType) {
237
        if (contentType != null && contentType.startsWith(MultipartUrlConfig.MULTIPART_FORM)) {
239
        if (contentType != null && contentType.startsWith(HTTPSamplerBase.MULTIPART_FORM_DATA)) {
238
            return true;
240
            return true;
239
        } else {
241
        } else {
240
            return false;
242
            return false;
Lines 243-251 Link Here
243
245
244
    private MultipartUrlConfig getMultipartConfig(String contentType) {
246
    private MultipartUrlConfig getMultipartConfig(String contentType) {
245
        if(isMultipart(contentType)) {
247
        if(isMultipart(contentType)) {
246
            // Get the boundary string for the multiparts from the
248
            // Get the boundary string for the multiparts from the content type
247
            // content type
248
            //int startOfBoundaryValuePos = contentType.toLowerCase().substring(beginIndex)
249
            String boundaryString = contentType.substring(contentType.toLowerCase().indexOf("boundary=") + "boundary=".length());
249
            String boundaryString = contentType.substring(contentType.toLowerCase().indexOf("boundary=") + "boundary=".length());
250
            return new MultipartUrlConfig(boundaryString);
250
            return new MultipartUrlConfig(boundaryString);
251
        }
251
        }
Lines 286-316 Link Here
286
    			log.debug("Proxy setting default protocol to: http");
286
    			log.debug("Proxy setting default protocol to: http");
287
			sampler.setProtocol(HTTP);
287
			sampler.setProtocol(HTTP);
288
		}
288
		}
289
289
        
290
        // Check if it was a multipart http post request
290
        // If it was a HTTP GET request, then all parameters in the URL
291
        MultipartUrlConfig urlConfig = getMultipartConfig(getContentType());
291
        // has been handled by the sampler.setPath above, so we just need
292
        if (urlConfig != null) {
292
        // to do parse the rest of the request if it is not a GET request
293
            urlConfig.parseArguments(postData);
293
        if(!HTTPSamplerBase.GET.equals(method)) {
294
            // Tell the sampler to do a multipart post
294
            // Check if it was a multipart http post request
295
            sampler.setDoMultipartPost(true);
295
            final String contentType = getContentType();
296
            // Remove the header for content-type and content-length, since
296
            MultipartUrlConfig urlConfig = getMultipartConfig(contentType);
297
            // those values will most likely be incorrect when the sampler
297
            if (urlConfig != null) {
298
            // performs the multipart request, because the boundary string
298
                urlConfig.parseArguments(postData);
299
            // will change
299
                // Tell the sampler to do a multipart post
300
            getHeaderManager().removeHeaderNamed(CONTENT_TYPE);
300
                sampler.setDoMultipartPost(true);
301
            getHeaderManager().removeHeaderNamed(CONTENT_LENGTH);
301
                // Remove the header for content-type and content-length, since
302
                // those values will most likely be incorrect when the sampler
303
                // performs the multipart request, because the boundary string
304
                // will change
305
                getHeaderManager().removeHeaderNamed(CONTENT_TYPE);
306
                getHeaderManager().removeHeaderNamed(CONTENT_LENGTH);
302
            
307
            
303
            // Set the form data
308
                // Set the form data
304
            sampler.setArguments(urlConfig.getArguments());
309
                sampler.setArguments(urlConfig.getArguments());
305
            // Set the file uploads
310
                // Set the file uploads
306
			sampler.setFileField(urlConfig.getFileFieldName());
311
                sampler.setFileField(urlConfig.getFileFieldName());
307
			sampler.setFilename(urlConfig.getFilename());
312
                sampler.setFilename(urlConfig.getFilename());
308
			sampler.setMimetype(urlConfig.getMimeType());
313
                sampler.setMimetype(urlConfig.getMimeType());
309
        } else if (postData != null && postData.trim().startsWith("<?")) {
314
            } else if (postData != null && postData.trim().startsWith("<?")) {
310
            sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
315
                // Not sure if this is needed anymore. I assume these requests
311
		} else {
316
                // do not have HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED as content type,
312
			sampler.parseArguments(postData); //standard name=value postData
317
                // and they would therefore be catched by the last else if of these if else if tests
313
		}
318
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
319
            } else if (contentType == null || contentType.startsWith(HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED) ){
320
                // It is the most common post request, with parameter name and values
321
                // We also assume this if no content type is present, to be most backwards compatible,
322
                // but maybe we should only parse arguments if the content type is as expected
323
                sampler.parseArguments(postData); //standard name=value postData
324
            } else if (postData != null) {
325
                // Just put the whole postbody as the value of a parameter
326
                sampler.addNonEncodedArgument("", postData, ""); //used when postData is pure xml (ex. an xml-rpc call)
327
            }
328
        }
314
        if (log.isDebugEnabled())
329
        if (log.isDebugEnabled())
315
    		log.debug("sampler path = " + sampler.getPath());
330
    		log.debug("sampler path = " + sampler.getPath());
316
	}
331
	}
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java (-4 / +37 lines)
Lines 27-33 Link Here
27
import java.io.OutputStream;
27
import java.io.OutputStream;
28
import java.io.UnsupportedEncodingException;
28
import java.io.UnsupportedEncodingException;
29
import java.net.URLConnection;
29
import java.net.URLConnection;
30
import java.net.URLEncoder;
31
30
32
import org.apache.jmeter.protocol.http.util.HTTPArgument;
31
import org.apache.jmeter.protocol.http.util.HTTPArgument;
33
import org.apache.jmeter.testelement.property.PropertyIterator;
32
import org.apache.jmeter.testelement.property.PropertyIterator;
Lines 220-239 Link Here
220
            connection.setDoInput(true);
219
            connection.setDoInput(true);
221
        }
220
        }
222
        else {
221
        else {
223
            // Set the content type
222
            // Check if the header manager had a content type header
224
            connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
223
            // This allows the user to specify his own content-type for a POST request
224
            String contentTypeHeader = connection.getRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE);
225
            boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0; 
225
            
226
            
226
            // If there are no arguments, we can send a file as the body of the request
227
            // If there are no arguments, we can send a file as the body of the request
227
            if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
228
            if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
229
                if(!hasContentTypeHeader) {
230
                    // Allow the mimetype of the file to control the content type
231
                    if(sampler.getMimetype() != null && sampler.getMimetype().length() > 0) {
232
                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, sampler.getMimetype());
233
                    }
234
                    else {
235
                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
236
                    }
237
                }
238
                
228
                // Create the content length we are going to write
239
                // Create the content length we are going to write
229
                File inputFile = new File(sampler.getFilename());
240
                File inputFile = new File(sampler.getFilename());
230
                contentLength = inputFile.length();
241
                contentLength = inputFile.length();
231
            }
242
            }
232
            else {
243
            else {
244
                // Set the content type
245
                if(!hasContentTypeHeader) {
246
                    connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
247
                }
248
                
233
                // We create the post body content now, so we know the size
249
                // We create the post body content now, so we know the size
234
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
250
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
235
                
251
                
236
                String postBody = sampler.getQueryString(contentEncoding);
252
                // If none of the arguments have a name specified, we
253
                // just send all the values as the post body
254
                String postBody = null;
255
                if(!sampler.getSendParameterValuesAsPostBody()) {
256
                    // It is a normal post request, with parameter names and values
257
                    postBody = sampler.getQueryString(contentEncoding);
258
                }
259
                else {
260
                    // Just append all the parameter values, and use that as the post body
261
                    StringBuffer postBodyBuffer = new StringBuffer();
262
                    PropertyIterator args = sampler.getArguments().iterator();
263
                    while (args.hasNext()) {
264
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
265
                        postBodyBuffer.append(arg.getValue());
266
                    }
267
                    postBody = postBodyBuffer.toString();
268
                }
269
237
                // Query string should be encoded in UTF-8
270
                // Query string should be encoded in UTF-8
238
                bos.write(postBody.getBytes("UTF-8")); // $NON-NLS-1$
271
                bos.write(postBody.getBytes("UTF-8")); // $NON-NLS-1$
239
                bos.flush();
272
                bos.flush();
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java (-9 / +44 lines)
Lines 25-30 Link Here
25
import java.io.OutputStream;
25
import java.io.OutputStream;
26
import java.net.InetAddress;
26
import java.net.InetAddress;
27
import java.net.URL;
27
import java.net.URL;
28
import java.net.URLDecoder;
28
import java.net.UnknownHostException;
29
import java.net.UnknownHostException;
29
import java.util.ArrayList;
30
import java.util.ArrayList;
30
import java.util.HashMap;
31
import java.util.HashMap;
Lines 54-59 Link Here
54
import org.apache.commons.httpclient.methods.PostMethod;
55
import org.apache.commons.httpclient.methods.PostMethod;
55
import org.apache.commons.httpclient.methods.PutMethod;
56
import org.apache.commons.httpclient.methods.PutMethod;
56
import org.apache.commons.httpclient.methods.RequestEntity;
57
import org.apache.commons.httpclient.methods.RequestEntity;
58
import org.apache.commons.httpclient.methods.StringRequestEntity;
57
import org.apache.commons.httpclient.methods.TraceMethod;
59
import org.apache.commons.httpclient.methods.TraceMethod;
58
import org.apache.commons.httpclient.methods.multipart.FilePart;
60
import org.apache.commons.httpclient.methods.multipart.FilePart;
59
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
61
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
Lines 68-73 Link Here
68
import org.apache.jmeter.protocol.http.control.Authorization;
70
import org.apache.jmeter.protocol.http.control.Authorization;
69
import org.apache.jmeter.protocol.http.control.CookieManager;
71
import org.apache.jmeter.protocol.http.control.CookieManager;
70
import org.apache.jmeter.protocol.http.control.HeaderManager;
72
import org.apache.jmeter.protocol.http.control.HeaderManager;
73
import org.apache.jmeter.protocol.http.util.EncoderCache;
71
import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
74
import org.apache.jmeter.protocol.http.util.SlowHttpClientSocketFactory;
72
import org.apache.jmeter.protocol.http.util.HTTPArgument;
75
import org.apache.jmeter.protocol.http.util.HTTPArgument;
73
import org.apache.jmeter.testelement.property.CollectionProperty;
76
import org.apache.jmeter.testelement.property.CollectionProperty;
Lines 313-323 Link Here
313
            }
316
            }
314
        }
317
        }
315
        else {
318
        else {
316
            // Set the content type
319
            // Check if the header manager had a content type header
317
            post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
320
            // This allows the user to specify his own content-type for a POST request
321
            Header contentTypeHeader = post.getRequestHeader(HEADER_CONTENT_TYPE);
322
            boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.getValue() != null && contentTypeHeader.getValue().length() > 0; 
318
323
319
            // If there are no arguments, we can send a file as the body of the request
324
            // If there are no arguments, we can send a file as the body of the request
320
            if(getArguments().getArgumentCount() == 0 && getSendFileAsPostBody()) {
325
            if(getArguments().getArgumentCount() == 0 && getSendFileAsPostBody()) {
326
                if(!hasContentTypeHeader) {
327
                    // Allow the mimetype of the file to control the content type
328
                    if(getMimetype() != null && getMimetype().length() > 0) {
329
                        post.setRequestHeader(HEADER_CONTENT_TYPE, getMimetype());
330
                    }
331
                    else {
332
                        post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
333
                    }
334
                }
335
                
321
                FileRequestEntity fileRequestEntity = new FileRequestEntity(new File(getFilename()),null); 
336
                FileRequestEntity fileRequestEntity = new FileRequestEntity(new File(getFilename()),null); 
322
                post.setRequestEntity(fileRequestEntity);
337
                post.setRequestEntity(fileRequestEntity);
323
                
338
                
Lines 327-347 Link Here
327
            else {            
342
            else {            
328
                // In an application/x-www-form-urlencoded request, we only support
343
                // In an application/x-www-form-urlencoded request, we only support
329
                // parameters, no file upload is allowed
344
                // parameters, no file upload is allowed
330
            
345
                if(!hasContentTypeHeader) {
346
                    // Set the content type
347
                    post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
348
                }
349
331
                // If a content encoding is specified, we set it as http parameter, so that
350
                // If a content encoding is specified, we set it as http parameter, so that
332
                // the post body will be encoded in the specified content encoding
351
                // the post body will be encoded in the specified content encoding
333
                final String contentEncoding = getContentEncoding();
352
                final String contentEncoding = getContentEncoding();
334
                if(contentEncoding != null && contentEncoding.trim().length() > 0) {
353
                if(contentEncoding != null && contentEncoding.trim().length() > 0) {
335
                    post.getParams().setContentCharset(contentEncoding);
354
                    post.getParams().setContentCharset(contentEncoding);
336
                }
355
                }
337
            
356
                
338
                PropertyIterator args = getArguments().iterator();
357
                // If none of the arguments have a name specified, we
339
                while (args.hasNext()) {
358
                // just send all the values as the post body
340
                    HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
359
                if(!getSendParameterValuesAsPostBody()) {
341
                    post.addParameter(arg.getName(), arg.getValue());
360
                    // It is a normal post request, with parameter names and values
361
                    PropertyIterator args = getArguments().iterator();
362
                    while (args.hasNext()) {
363
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
364
                        post.addParameter(arg.getName(), arg.getValue());
365
                    }
342
                }
366
                }
367
                else {
368
                    // Just append all the parameter values, and use that as the post body
369
                    StringBuffer postBody = new StringBuffer();
370
                    PropertyIterator args = getArguments().iterator();
371
                    while (args.hasNext()) {
372
                        HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
373
                        postBody.append(arg.getValue());
374
                    }
375
                    StringRequestEntity requestEntity = new StringRequestEntity(postBody.toString(), post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), post.getRequestCharSet());
376
                    post.setRequestEntity(requestEntity);
377
                }
343
                
378
                
344
                // If the Multipart is repeatable, we can send it first to
379
                // If the request entity is repeatable, we can send it first to
345
                // our own stream, so we can return it
380
                // our own stream, so we can return it
346
                if(post.getRequestEntity().isRepeatable()) {
381
                if(post.getRequestEntity().isRepeatable()) {
347
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
382
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSamplerBase.java (-5 / +26 lines)
Lines 209-217 Link Here
209
209
210
    protected static final String HEADER_LOCATION = "Location"; // $NON-NLS-1$
210
    protected static final String HEADER_LOCATION = "Location"; // $NON-NLS-1$
211
211
212
	protected static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; // $NON-NLS-1$
212
	public static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded"; // $NON-NLS-1$
213
    
213
    
214
    protected static final String MULTIPART_FORM_DATA = "multipart/form-data"; // $NON-NLS-1$
214
    public static final String MULTIPART_FORM_DATA = "multipart/form-data"; // $NON-NLS-1$
215
    
215
    
216
    // Derive the mapping of content types to parsers
216
    // Derive the mapping of content types to parsers
217
    private static Map parsersForType = new HashMap();
217
    private static Map parsersForType = new HashMap();
Lines 290-300 Link Here
290
	 * i.e. without any additional wrapping
290
	 * i.e. without any additional wrapping
291
	 * 
291
	 * 
292
	 * @return true if specified file is to be sent as the body,
292
	 * @return true if specified file is to be sent as the body,
293
	 * i.e. both FileField and MimeType are blank
293
	 * i.e. both FileField is blank
294
	 */
294
	 */
295
	public boolean getSendFileAsPostBody(){
295
	public boolean getSendFileAsPostBody() {
296
		return getFileField().length()== 0 && getMimetype().length() == 0;
296
        // If no file field is specified, the file is sent as post body
297
		return getFileField().length()== 0 && getFilename().length() > 0;
297
	}
298
	}
299
    
300
    /**
301
     * Determine if none of the parameters have a name, and if that
302
     * is the case, it means that the parameter values should be sent
303
     * as the post body
304
     * 
305
     * @return true if none of the parameters have a name specified
306
     */
307
    public boolean getSendParameterValuesAsPostBody() {
308
        boolean noArgumentsHasName = true;
309
        PropertyIterator args = getArguments().iterator();
310
        while (args.hasNext()) {
311
            HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
312
            if(arg.getName() != null && arg.getName().length() > 0) {
313
                noArgumentsHasName = false;
314
                break;
315
            }
316
        }
317
        return noArgumentsHasName;
318
    }
298
319
299
    /**
320
    /**
300
     * Determine if we should use multipart/form-data or 
321
     * Determine if we should use multipart/form-data or 
(-)C:/Documents and Settings/alf.hogemark/workspace/Jmeter 2.2 official/src/protocol/http/org/apache/jmeter/protocol/http/config/MultipartUrlConfig.java (-1 / +1 lines)
Lines 29-35 Link Here
29
 * @version $Revision$
29
 * @version $Revision$
30
 */
30
 */
31
public class MultipartUrlConfig implements Serializable {
31
public class MultipartUrlConfig implements Serializable {
32
32
    /** @deprecated use HTTPSamplerBase.MULTIPART_FORM_DATA instead */
33
	public static final String MULTIPART_FORM = "multipart/form-data";
33
	public static final String MULTIPART_FORM = "multipart/form-data";
34
34
35
	private String boundary, filename, fileField, mimetype;
35
	private String boundary, filename, fileField, mimetype;

Return to bug 41518