jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1577903 - in /jmeter/trunk: src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java xdocs/changes.xml
Date Sat, 15 Mar 2014 17:15:41 GMT
Author: sebb
Date: Sat Mar 15 17:15:40 2014
New Revision: 1577903

URL: http://svn.apache.org/r1577903
Log:
MirrorServer should support query parameters for status and redirects
Bugzilla Id: 56272

Modified:
    jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java
    jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java?rev=1577903&r1=1577902&r2=1577903&view=diff
==============================================================================
--- jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java
(original)
+++ jmeter/trunk/src/protocol/http/org/apache/jmeter/protocol/http/control/HttpMirrorThread.java
Sat Mar 15 17:15:40 2014
@@ -23,6 +23,9 @@ import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.Socket;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.HashMap;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.jmeter.protocol.http.util.HTTPConstants;
@@ -45,6 +48,10 @@ import org.apache.oro.text.regex.Perl5Ma
  * X-ResponseLength - truncates the response to the stated length
  * X-SetCookie - set a cookie
  * X-Sleep - sleep before returning
+ *
+ * It also responds to some query strings:
+ * status=nnn Message (overrides X-ResponseStatus)
+ * redirect=location - sends a temporary redirect
  */
 public class HttpMirrorThread implements Runnable {
     private static final Logger log = LoggingManager.getLoggerForClass();
@@ -52,6 +59,10 @@ public class HttpMirrorThread implements
     private static final String ISO_8859_1 = "ISO-8859-1"; //$NON-NLS-1$
     private static final byte[] CRLF = { 0x0d, 0x0a };
 
+    private static final String REDIRECT = "redirect"; //$NON-NLS-1$
+
+    private static final String STATUS = "status"; //$NON-NLS-1$
+
     /** Socket to client. */
     private final Socket clientSocket;
 
@@ -93,12 +104,39 @@ public class HttpMirrorThread implements
 
             baos.close();
             final String headerString = headers.toString();
+            final String[] requestParts = headerString.split("\\s+",3);
+            final String requestMethod = requestParts[0];
+            final String requestPath = requestParts[1];
+            final HashMap<String, String> parameters = new HashMap<String, String>();
+            if (HTTPConstants.GET.equals(requestMethod)) {
+                try {
+                    URI uri = new URI(requestPath);
+                    String query = uri.getQuery();
+                    if (query != null) {
+                        String params[] = query.split("&");
+                        for(String param : params) {
+                            String parts[] = param.split("=",2);
+                            parameters.put(parts[0], parts[1]);
+                            System.err.println(parts[0]+ "=>" + parts[1]);
+                        }
+                    }
+                } catch (URISyntaxException e) {
+                    log.warn("error parsing "+requestPath, e);
+                }
+            }
 
             // Look for special Response Length header
             String responseStatusValue = getRequestHeaderValue(headerString, "X-ResponseStatus");
//$NON-NLS-1$
             if(responseStatusValue == null) {
                 responseStatusValue = "200 OK";
             }
+            // Do this before the status check so can override the status, e.g. with a different
redirect type
+            if (parameters.containsKey(REDIRECT)) {
+                responseStatusValue = "302 Temporary Redirect";
+            }
+            if (parameters.containsKey(STATUS)) {
+                responseStatusValue = parameters.get(STATUS);
+            }
 
             log.debug("Write headers");
             out = new BufferedOutputStream(clientSocket.getOutputStream());
@@ -108,16 +146,22 @@ public class HttpMirrorThread implements
             out.write("Content-Type: text/plain".getBytes(ISO_8859_1)); //$NON-NLS-1$
             out.write(CRLF);
 
-            // Look for special Cookie request
+            if (parameters.containsKey(REDIRECT)) {
+                out.write((HTTPConstants.HEADER_LOCATION + ": ").getBytes(ISO_8859_1)); //$NON-NLS-1$
+                out.write(parameters.get(REDIRECT).getBytes(ISO_8859_1));
+                out.write(CRLF);
+            }
+
+            // Look for special Header request
             String headersValue = getRequestHeaderValue(headerString, "X-SetHeaders"); //$NON-NLS-1$
             if (headersValue != null) {
                 String[] headersToSet = headersValue.split("\\|");
                 for (String string : headersToSet) {
                     out.write(string.getBytes(ISO_8859_1));
-                    out.write(CRLF);                    
+                    out.write(CRLF);
                 }
             }
-            
+
             // Look for special Response Length header
             String responseLengthValue = getRequestHeaderValue(headerString, "X-ResponseLength");
//$NON-NLS-1$
             int responseLength=-1;

Modified: jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java?rev=1577903&r1=1577902&r2=1577903&view=diff
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
(original)
+++ jmeter/trunk/test/src/org/apache/jmeter/protocol/http/control/TestHTTPMirrorThread.java
Sat Mar 15 17:15:40 2014
@@ -24,6 +24,7 @@ import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.net.HttpURLConnection;
 import java.net.Socket;
+import java.net.URI;
 import java.net.URL;
 
 import org.apache.commons.io.IOUtils;
@@ -344,6 +345,26 @@ public class TestHTTPMirrorThread extend
         assertEquals("Temporary Redirect", conn.getResponseMessage());
     }
 
+    public void testQueryStatus() throws Exception {
+        URL url = new URI("http",null,"localhost",HTTP_SERVER_PORT,"/path","status=303 See
Other",null).toURL();
+        System.err.println(url);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.connect();
+        assertEquals(303, conn.getResponseCode());
+        assertEquals("See Other", conn.getResponseMessage());
+    }
+
+    public void testQueryRedirect() throws Exception {
+        URL url = new URI("http",null,"localhost",HTTP_SERVER_PORT,"/path","redirect=/a/b/c/d?q",null).toURL();
+        System.err.println(url);
+        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+        conn.setInstanceFollowRedirects(false);
+        conn.connect();
+        assertEquals(302, conn.getResponseCode());
+        assertEquals("Temporary Redirect", conn.getResponseMessage());
+        assertEquals("/a/b/c/d?q",conn.getHeaderField("Location"));
+    }
+
     public void testHeaders() throws Exception {
         URL url = new URL("http", "localhost", HTTP_SERVER_PORT, "/");
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1577903&r1=1577902&r2=1577903&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Sat Mar 15 17:15:40 2014
@@ -173,8 +173,9 @@ A workaround is to use a Java 7 update 4
 <h3>HTTP Samplers and Test Script Recorder</h3>
 <ul>
 <li><bugzilla>55959</bugzilla> - improve error message when Test Script
Recorder fails due to I/O problem</li>
-<li><bugzilla>52013</bugzilla> -  Test Script Recorder's Child View Results
Tree does not take into account Test Script Recorder excluded/included URLs. Based on report
and analysis of James Liang</li>
+<li><bugzilla>52013</bugzilla> - Test Script Recorder's Child View Results
Tree does not take into account Test Script Recorder excluded/included URLs. Based on report
and analysis of James Liang</li>
 <li><bugzilla>56119</bugzilla> - File uploads fail every other attempt
using timers. Enable idle timeouts for servers that don't send Keep-Alive headers.</li>
+<li><bugzilla>56272</bugzilla> -  MirrorServer should support query parameters
for status and redirects</li>
 </ul>
 
 <h3>Other samplers</h3>



Mime
View raw message