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:07:43 UTC

svn commit: r585212 - in /jakarta/jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/sampler/ xdocs/ xdocs/usermanual/

Author: alf
Date: Tue Oct 16 11:07:41 2007
New Revision: 585212

URL: http://svn.apache.org/viewvc?rev=585212&view=rev
Log:
Fix handling of HTTP PUT requests, fix for bug 43612.
Add unit tests for HTTP PUT and DELETE scenarios.
Document for HTTP Samplers how http methods works, and how arbitrary bodies can be sent for HTTP POST and PUT.

Added:
    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PutWriter.java
Modified:
    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
    jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PostWriter.java
    jakarta/jmeter/trunk/xdocs/changes.xml
    jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml

Modified: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java?rev=585212&r1=585211&r2=585212&view=diff
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java (original)
+++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java Tue Oct 16 11:07:41 2007
@@ -18,11 +18,9 @@
 
 import java.io.BufferedInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 
 import java.net.BindException;
 import java.net.HttpURLConnection;
@@ -95,30 +93,8 @@
 	}
 
     private void setPutHeaders(URLConnection conn) throws IOException {
-        postWriter = new PostWriter();
+        postWriter = new PutWriter();
         postWriter.setHeaders(conn, this);
-/*
-        boolean hasPutBody = false;
-        // Check if any files should be uploaded
-        if (hasUploadableFiles())
-        {
-            // Set content-type if we have a value for it
-            if(getMimetype() != null && getMimetype().trim().length() > 0) {
-                conn.setRequestProperty(HEADER_CONTENT_TYPE, getMimetype());
-            }
-            hasPutBody = true;
-        }
-        // Check if any parameters should be sent as body
-        if(getSendParameterValuesAsPostBody()) {
-            hasPutBody = true;
-        }
-        // If there is any files to upload, or other body content to be sent,
-        // we set the connection to accept output
-        if(hasPutBody) {
-            conn.setDoInput(true);
-            conn.setDoOutput(true);
-        }
-*/        
     }
 
 	/**
@@ -443,7 +419,7 @@
 		HttpURLConnection conn = null;
 
 		String urlStr = url.toString();
-		log.debug("Start : sample" + urlStr);
+		log.debug("Start : sample " + urlStr);
 
 		HTTPSampleResult res = new HTTPSampleResult();
 		res.setMonitor(isMonitor());

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=585212&r1=585211&r2=585212&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:07:41 2007
@@ -47,9 +47,9 @@
 	public static final String ENCODING = "ISO-8859-1"; // $NON-NLS-1$
 
     /** The form data that is going to be sent as url encoded */
-    private byte[] formDataUrlEncoded;    
+    protected byte[] formDataUrlEncoded;    
     /** The form data that is going to be sent in post body */
-    private byte[] formDataPostBody;
+    protected byte[] formDataPostBody;
     /** The start of the file multipart to be sent */
     private byte[] formDataFileStartMultipart;
     /** The boundary string for multipart */

Added: jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PutWriter.java
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PutWriter.java?rev=585212&view=auto
==============================================================================
--- jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PutWriter.java (added)
+++ jakarta/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/sampler/PutWriter.java Tue Oct 16 11:07:41 2007
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.jmeter.protocol.http.sampler;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.net.URLConnection;
+
+import org.apache.jmeter.protocol.http.util.HTTPArgument;
+import org.apache.jmeter.testelement.property.PropertyIterator;
+
+/**
+ * Class for setting the necessary headers for a PUT request, and sending the
+ * body of the PUT.
+ */
+public class PutWriter extends PostWriter {
+    /**
+     * Constructor for PutWriter.
+     */
+    public PutWriter() {
+        // Put request does not use multipart, so no need for boundary
+        super(null);
+    }
+    
+    public void setHeaders(URLConnection connection, HTTPSampler sampler) throws IOException {
+    	// Get the encoding to use for the request
+        String contentEncoding = sampler.getContentEncoding();
+        if(contentEncoding == null || contentEncoding.length() == 0) {
+            contentEncoding = ENCODING;
+        }
+        long contentLength = 0L;
+        boolean hasPutBody = false;
+
+        // Check if the header manager had a content type header
+        // This allows the user to specify his own content-type for a PUT request
+        String contentTypeHeader = connection.getRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE);
+        boolean hasContentTypeHeader = contentTypeHeader != null && contentTypeHeader.length() > 0; 
+
+        // If there are no arguments, we can send a file as the body of the request
+        if(sampler.getArguments() != null && sampler.getArguments().getArgumentCount() == 0 && sampler.getSendFileAsPostBody()) {
+            hasPutBody = true;
+            if(!hasContentTypeHeader) {
+                // Allow the mimetype of the file to control the content type
+                if(sampler.getMimetype() != null && sampler.getMimetype().length() > 0) {
+                    connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_TYPE, sampler.getMimetype());
+                }
+            }
+
+            // Create the content length we are going to write
+            File inputFile = new File(sampler.getFilename());
+            contentLength = inputFile.length();
+        }
+        else if(sampler.getSendParameterValuesAsPostBody()) {
+            hasPutBody = true;
+            // 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());
+            }
+
+            // We create the post body content now, so we know the size
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+            // Just append all the parameter values, and use that as the put body
+            StringBuffer postBodyBuffer = new StringBuffer();
+            PropertyIterator args = sampler.getArguments().iterator();
+            while (args.hasNext()) {
+                HTTPArgument arg = (HTTPArgument) args.next().getObjectValue();
+                postBodyBuffer.append(arg.getEncodedValue(contentEncoding));
+            }
+            String postBody = postBodyBuffer.toString();
+
+            // Query string should be encoded in UTF-8
+            bos.write(postBody.getBytes("UTF-8")); // $NON-NLS-1$
+            bos.flush();
+            bos.close();
+
+            // Keep the content, will be sent later
+            formDataUrlEncoded = bos.toByteArray();
+            contentLength = bos.toByteArray().length;
+        }
+        if(hasPutBody) {
+            // Set the content length
+            connection.setRequestProperty(HTTPSamplerBase.HEADER_CONTENT_LENGTH, Long.toString(contentLength));
+
+            // Make the connection ready for sending post data
+            connection.setDoOutput(true);
+        }
+    }
+}

Modified: jakarta/jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/changes.xml?rev=585212&r1=585211&r2=585212&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/changes.xml (original)
+++ jakarta/jmeter/trunk/xdocs/changes.xml Tue Oct 16 11:07:41 2007
@@ -49,6 +49,7 @@
 <li>If a POST body is built from parameter values only, these are now encoded if the checkbox is set.</li>
 <li>Bug 43584 - Assertion Failure Message contains a comma that is also used as the delimiter for CSV files</li>
 <li>HTTP Mirror Server now always returns the exact same content, it used to return wrong if UTF-8 encoding was used for HTTP POST body, for example</li>
+<li>Bug 43612 - HTTP PUT does not honor request parameters</li>
 </ul>
 
 <h4>Improvements</h4>
@@ -74,6 +75,7 @@
 <li>HTTP Mirror Server now supports blocking waiting for more data to appear, if content-length header is present in request</li>
 <li>HTTP Mirror Server GUI now has the Start and Stop buttons in a more visible place</li>
 <li>Server mode now creates the RMI registry; to disable set the JMeter property server.rmi.create=false</li>
+<li>HTTP Sampler now supports using MIME Type field to specify content-type request header when body is constructed from parameter values</li>
 </ul>
 
 <h4>Non-functional Improvements</h4>

Modified: jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml
URL: http://svn.apache.org/viewvc/jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml?rev=585212&r1=585211&r2=585212&view=diff
==============================================================================
--- jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml (original)
+++ jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml Tue Oct 16 11:07:41 2007
@@ -188,8 +188,8 @@
         be generated from the list of parameters you provide.  Each parameter has a <i>name</i> and
         <i>value</i>, the options to encode the parameter, and an option to include or exclude an equals sign (some applications
         don't expect an equals when the value is the empty string).  The query string will be generated in the correct fashion, depending on
-        the choice of "Method" you made (ie if you chose GET, the query string will be
-        appended to the URL, if POST, then it will be sent separately).  Also, if you are
+        the choice of "Method" you made (ie if you chose GET or DELETE, the query string will be
+        appended to the URL, if POST or PUT, then it will be sent separately).  Also, if you are
         sending a file using a multipart form, the query string will be created using the
         multipart form specifications.
         <b>See below for some further information on parameter handling.</b>
@@ -200,11 +200,19 @@
         <property name="Filename" required="No">Name of the file to send.  If left blank, JMeter
         does not send a file, if filled in, JMeter automatically sends the request as
         a multipart form request.
-        <b>In versions of JMeter after 2.2: if both the 'name' atribute and MIME type (below) are omitted, then the file is sent as the body
-        of the POST request. This allows arbitrary POST bodies to be sent.</b>
+        <p>
+        If it is a POST or PUT request and the 'name' atribute (below) are omitted, then the file is sent as the body
+        of the request. This allows arbitrary bodies to be sent. This functionality is present for POST requests
+        after version 2.2, and also for PUT requests after version 2.3.
+        <b>See below for some further information on parameter handling.</b>
+        </p>
         </property>
         <property name="Value for 'name' attribute" required="No">Value of the "name" web request parameter.</property>
-        <property name="MIME Type" required="No">MIME type (for example, text/plain).</property>
+        <property name="MIME Type" required="No">MIME type (for example, text/plain).
+        If it is a POST or PUT request and either the 'name' atribute (below) are omitted or the request body is
+        constructed from parameter values only, then the value of this field is used as the value of the
+        content-type request header.
+        </property>
         <property name="Retrieve All Embedded Resources from HTML Files" required="No">Tell JMeter to parse the HTML file
 and send HTTP/HTTPS requests for all images, Java applets, JavaScript files, CSSs, etc. referenced in the file.
         See below for more details.
@@ -228,13 +236,20 @@
 </p>
 <p>
 Parameter Handling:<br></br>
-For the POST method, if there is no file to send, and the name(s) of the parameter(s) are omitted,
-then the body is created by concatenating all the value(s) of the parameters. 
+For the POST and PUT method, if there is no file to send, and the name(s) of the parameter(s) are omitted,
+then the body is created by concatenating all the value(s) of the parameters.
+This allows arbitrary bodies to be sent.
 The values are encoded if the encoding flag is set (versions of JMeter after 2.3).
+See also the MIME Type above how you can control the content-type request header that are sent.
 <br></br>
 For other methods, if the name of the parameter is missing,
 then the parameter is ignored. This allows the use of optional parameters defined by variables.
 (versions of JMeter after 2.3)
+</p>
+<p>
+Method Handling:<br></br>
+The POST and PUT request methods work similar, except that the PUT method does not support multipart requests.
+The GET and DELETE request methods work similar.
 </p>
 <p>Upto and including JMeter 2.1.1, only responses with the content-type "text/html" were scanned for
 embedded resources. Other content-types were assumed to be something other than HTML.



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