You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by al...@apache.org on 2007/10/16 20:01:57 UTC

svn commit: r585210 - in /jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler: HTTPSampler2.java PostWriter.java

Author: alf
Date: Tue Oct 16 11:01:54 2007
New Revision: 585210

URL: http://svn.apache.org/viewvc?rev=585210&view=rev
Log:
Allow user to control the content-type request header for POST request via the MIME Type field if the body is constructed from the parameter values.

Modified:
    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java

Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java?rev=585210&r1=585209&r2=585210&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java (original)
+++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler2.java Tue Oct 16 11:01:54 2007
@@ -359,12 +359,8 @@
                 postedBody.append("<actual file content, not shown here>");
             }
             else {
-                // In an application/x-www-form-urlencoded request, we only support
+                // In a post request which is not multipart, we only support
                 // parameters, no file upload is allowed
-                if(!hasContentTypeHeader) {
-                    // Set the content type
-                    post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
-                }
 
                 // If a content encoding is specified, we set it as http parameter, so that
                 // the post body will be encoded in the specified content encoding
@@ -378,6 +374,13 @@
                 // If none of the arguments have a name specified, we
                 // just send all the values as the post body
                 if(getSendParameterValuesAsPostBody()) {
+                    // Allow the mimetype of the file to control the content type
+                    // This is not obvious in GUI if you are not uploading any files,
+                    // but just sending the content of nameless parameters
+                    if(!hasContentTypeHeader && getMimetype() != null && getMimetype().length() > 0) {
+                        post.setRequestHeader(HEADER_CONTENT_TYPE, getMimetype());
+                    }
+                    
                     // Just append all the non-empty parameter values, and use that as the post body
                     StringBuffer postBody = new StringBuffer();
                     PropertyIterator args = getArguments().iterator();
@@ -393,7 +396,15 @@
                     }
                     StringRequestEntity requestEntity = new StringRequestEntity(postBody.toString(), post.getRequestHeader(HEADER_CONTENT_TYPE).getValue(), post.getRequestCharSet());
                     post.setRequestEntity(requestEntity);
-                } else { // It is a normal post request, with parameter names and values
+                }
+                else {
+                    // It is a normal post request, with parameter names and values
+                    
+                    // Set the content type
+                    if(!hasContentTypeHeader) {
+                        post.setRequestHeader(HEADER_CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED);
+                    }
+                    // Add the parameters
                     PropertyIterator args = getArguments().iterator();
                     while (args.hasNext()) {
                         HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
@@ -929,7 +940,7 @@
             putBody.append("<actual file content, not shown here>");
         }
         // If none of the arguments have a name specified, we
-        // just send all the values as the post body
+        // just send all the values as the put body
         else if(getSendParameterValuesAsPostBody()) {
             hasPutBody = true;
 
@@ -992,7 +1003,7 @@
         else {
             return null;
         }        
-     }
+    }
 
 	// Implement locally, as current httpclient InputStreamRI implementation does not close file...
 	private class FileRequestEntity implements RequestEntity {

Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java?rev=585210&r1=585209&r2=585210&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java (original)
+++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java Tue Oct 16 11:01:54 2007
@@ -245,11 +245,6 @@
                 contentLength = inputFile.length();
             }
             else {
-                // Set the content type
-                if(!hasContentTypeHeader) {
-                    connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
-                }
-                
                 // We create the post body content now, so we know the size
                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
                 
@@ -257,10 +252,22 @@
                 // just send all the values as the post body
                 String postBody = null;
                 if(!sampler.getSendParameterValuesAsPostBody()) {
+                    // Set the content type
+                    if(!hasContentTypeHeader) {
+                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, HTTPSamplerBase.APPLICATION_X_WWW_FORM_URLENCODED);
+                    }
+
                     // It is a normal post request, with parameter names and values
                     postBody = sampler.getQueryString(contentEncoding);
                 }
                 else {
+                    // Allow the mimetype of the file to control the content type
+                    // This is not obvious in GUI if you are not uploading any files,
+                    // but just sending the content of nameless parameters
+                    if(!hasContentTypeHeader && sampler.getMimetype() != null && sampler.getMimetype().length() > 0) {
+                        connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, sampler.getMimetype());
+                    }
+                    
                     // Just append all the parameter values, and use that as the post body
                     StringBuffer postBodyBuffer = new StringBuffer();
                     PropertyIterator args = sampler.getArguments().iterator();



---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org