You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by fs...@apache.org on 2021/05/25 18:35:53 UTC

[jmeter] branch master updated: HTTP 308 Permanent Redirect is not supported

This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new fe04aff  HTTP 308 Permanent Redirect is not supported
fe04aff is described below

commit fe04aff269f32a6d11a50f0c881765251e457dd7
Author: Felix Schumacher <fe...@internetallee.de>
AuthorDate: Tue May 25 20:28:41 2021 +0200

    HTTP 308 Permanent Redirect is not supported
    
    Contributed by Baptiste Gaillard (baptiste.gaillard at gmail.com)
    
    Bugzilla Id: 65328
    Closes #666
---
 .../protocol/http/sampler/HTTPSampleResult.java    | 12 ++-
 .../protocol/http/util/HTTPConstantsInterface.java |  1 +
 .../protocol/http/sampler/TestRedirects.java       | 88 ++++++++++++++++++++++
 xdocs/changes.xml                                  |  3 +
 4 files changed, 102 insertions(+), 2 deletions(-)

diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSampleResult.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSampleResult.java
index 37dca2c..0b30eae 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSampleResult.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/sampler/HTTPSampleResult.java
@@ -102,7 +102,14 @@ public class HTTPSampleResult extends SampleResult {
 
     /**
      * Determine whether this result is a redirect.
-     * Returns true for: 301,302,303 and 307(GET or HEAD)
+     *
+     * <p>
+     * If status is {@code 307}, the request has to be a HTTP method of {@code GET} or
+     * {@code HEAD}, to be considered a redirect. For all other status codes, the
+     * HTTP method will not be checked.
+     * </p>
+     * Returns true for: 301, 302, 303, 307 (GET or HEAD) and 308
+     *
      * @return true iff res is an HTTP redirect response
      */
     public boolean isRedirect() {
@@ -115,7 +122,8 @@ public class HTTPSampleResult extends SampleResult {
          */
         final String[] redirectCodes = { HTTPConstants.SC_MOVED_PERMANENTLY,
                 HTTPConstants.SC_MOVED_TEMPORARILY,
-                HTTPConstants.SC_SEE_OTHER };
+                HTTPConstants.SC_SEE_OTHER,
+                HTTPConstants.SC_PERMANENT_REDIRECT };
         String code = getResponseCode();
         for (String redirectCode : redirectCodes) {
             if (redirectCode.equals(code)) {
diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPConstantsInterface.java b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPConstantsInterface.java
index c4b2d95..fffb369 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPConstantsInterface.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/util/HTTPConstantsInterface.java
@@ -25,6 +25,7 @@ public interface HTTPConstantsInterface { // CHECKSTYLE IGNORE InterfaceIsType
     String SC_MOVED_TEMPORARILY = "302";
     String SC_SEE_OTHER = "303";
     String SC_TEMPORARY_REDIRECT = "307";
+    String SC_PERMANENT_REDIRECT = "308";
 
     int DEFAULT_HTTPS_PORT = 443;
     String DEFAULT_HTTPS_PORT_STRING = "443"; // $NON-NLS-1$
diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestRedirects.java b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestRedirects.java
new file mode 100644
index 0000000..849ab03
--- /dev/null
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/sampler/TestRedirects.java
@@ -0,0 +1,88 @@
+/*
+ * 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 static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
+import static com.github.tomakehurst.wiremock.client.WireMock.any;
+import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import com.github.tomakehurst.wiremock.WireMockServer;
+import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
+
+class TestRedirects {
+
+    public static List<Arguments> redirectionParams() {
+        List<Arguments> res = new ArrayList<>();
+        // Nested for depth is 2 (max allowed is 1). [NestedForDepth]
+        List<String> httpMethods = Arrays.asList("HEAD", "GET", "POST", "PUT", "DELETE");
+        Arrays.stream(HTTPSamplerFactory.getImplementations()).forEach(httpImpl -> {
+            for (int statusCode : Arrays.asList(301, 302, 303, 307, 308)) {
+                for (String method : httpMethods) {
+                    boolean shouldRedirect = statusCode != 307 || ("GET".equals(method) || "HEAD".equals(method));
+                    res.add(Arguments.of(httpImpl, statusCode, shouldRedirect, method));
+                }
+            }
+            for (int statusCode : Arrays.asList(300, 304, 305, 306)) {
+                for (String method : httpMethods) {
+                    res.add(Arguments.of(httpImpl, statusCode, false, method));
+                }
+            }
+        });
+        return res;
+    }
+
+    @ParameterizedTest
+    @MethodSource("redirectionParams")
+    void testRedirect(String httpImpl, int redirectCode, boolean shouldRedirect, String method)
+            throws MalformedURLException {
+        WireMockServer server = createServer();
+        server.start();
+        try {
+            HTTPSamplerBase http = HTTPSamplerFactory.newInstance(httpImpl);
+            http.setAutoRedirects(false);
+            server.stubFor(any(urlPathEqualTo("/some-location")).willReturn(
+                    aResponse().withHeader("Location", server.url("/redirected")).withStatus(redirectCode)));
+            HTTPSampleResult res = http.sample(new URL(server.url("/some-location")), method, false, 1);
+            if (shouldRedirect) {
+                Assertions.assertEquals(server.url("/redirected"), res.getRedirectLocation());
+            } else {
+                Assertions.assertEquals(null, res.getRedirectLocation());
+            }
+            Assertions.assertEquals("" + redirectCode, res.getResponseCode());
+        } finally {
+            server.stop();
+        }
+    }
+
+    private WireMockServer createServer() {
+        WireMockConfiguration configuration = WireMockConfiguration.wireMockConfig().dynamicPort();
+        return new WireMockServer(configuration);
+    }
+
+}
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index ec0ea04..49b53ed 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -81,6 +81,8 @@ Summary
   <li><bug>65027</bug>Detect mime-type for files automatically when adding files to HTTP Sampler</li>
   <li><bug>65020</bug>HTTP Sampler/Files upload tab - add missing buttons</li>
   <li><pr>650</pr>HTTP Sampler timestamp fix when exception is caught. Contributed by Konstantin Kalinin (konstantin at kkalinin.pro)</li>
+  <li><bug>65328</bug><pr>666</pr>HTTP 308 Permanent Redirect is not supported. Contributed by
+    Baptiste Gaillard (baptiste.gaillard at gmail.com)</li>
 </ul>
 
 <h3>Other samplers</h3>
@@ -211,6 +213,7 @@ Summary
   <li>Ori Marko (orimarko at gmail.com)</li>
   <li>BugKing (wangzhen at fit2cloud.com)</li>
   <li>Till Neunast (github.com/tilln)</li>
+  <li>Baptiste Gaillard (baptiste.gaillard at gmail.com)</li>
 </ul>
 <p>We also thank bug reporters who helped us improve JMeter.</p>
 <ul>