http-client-branch: review comment add more checks to tests for redirect http-client-branch
authorchegar
Sat, 25 Nov 2017 16:24:26 +0000
branchhttp-client-branch
changeset 55874 ee17449ff241
parent 55873 b5e6a3201081
child 55875 39e9e9883c4e
http-client-branch: review comment add more checks to tests for redirect
test/jdk/java/net/httpclient/SmokeTest.java
test/jdk/java/net/httpclient/http2/RedirectTest.java
--- a/test/jdk/java/net/httpclient/SmokeTest.java	Sat Nov 25 16:23:20 2017 +0000
+++ b/test/jdk/java/net/httpclient/SmokeTest.java	Sat Nov 25 16:24:26 2017 +0000
@@ -316,6 +316,11 @@
             throw new RuntimeException(
                 "Expected 200, got [ " + response.statusCode() + " ]");
         }
+        // no redirection, etc, should be no previous response
+        if (response.previousResponse().isPresent()) {
+            throw new RuntimeException(
+                "Unexpected previous response: " + response.previousResponse().get());
+        }
         Path reply = response.body();
         //System.out.println("Reply stored in " + reply.toString());
         cmpFileContent(reply, p);
@@ -348,7 +353,7 @@
         if (Files.size(downloaded) != Files.size(midSizedFile)) {
             throw new RuntimeException("Size mismatch");
         }
-
+        checkPreviousRedirectResponses(request, response);
         System.out.printf(" (count: %d) ", handler.count());
         // repeat with async api
 
@@ -369,10 +374,46 @@
         if (Files.size(downloaded) != Files.size(midSizedFile)) {
             throw new RuntimeException("Size mismatch 2");
         }
+
+        checkPreviousRedirectResponses(request, response);
         System.out.printf(" (count: %d) ", handler.count());
         System.out.println(" OK");
     }
 
+    static void checkPreviousRedirectResponses(HttpRequest initialRequest,
+                                               HttpResponse<?> finalResponse) {
+        // there must be at least one previous response
+        finalResponse.previousResponse()
+                .orElseThrow(() -> new RuntimeException("no previous response"));
+
+        HttpResponse<?> response = finalResponse;
+        do {
+            URI uri = response.uri();
+            response = response.previousResponse().get();
+            check(300 <= response.statusCode() && response.statusCode() <= 309,
+                  "Expected 300 <= code <= 309, got:" + response.statusCode());
+            check(response.body() == null, "Unexpected body: " + response.body());
+            String locationHeader = response.headers().firstValue("Location")
+                    .orElseThrow(() -> new RuntimeException("no previous Location"));
+            check(uri.toString().endsWith(locationHeader),
+                  "URI: " + uri + ", Location: " + locationHeader);
+        } while (response.previousResponse().isPresent());
+
+        // initial
+        check(initialRequest.equals(response.request()),
+              "Expected initial request [%s] to equal last prev req [%s]",
+              initialRequest, response.request());
+    }
+
+    static void check(boolean cond, Object... msg) {
+        if (cond)
+            return;
+        StringBuilder sb = new StringBuilder();
+        for (Object o : msg)
+            sb.append(o);
+        throw new RuntimeException(sb.toString());
+    }
+
     /**
      * A Proxy Selector that wraps a ProxySelector.of(), and counts the number
      * of times its select method has been invoked. This can be used to ensure
--- a/test/jdk/java/net/httpclient/http2/RedirectTest.java	Sat Nov 25 16:23:20 2017 +0000
+++ b/test/jdk/java/net/httpclient/http2/RedirectTest.java	Sat Nov 25 16:24:26 2017 +0000
@@ -35,11 +35,11 @@
 
 import java.net.*;
 import jdk.incubator.http.*;
+import java.util.Optional;
 import java.util.concurrent.*;
 import java.util.function.*;
 import java.util.Arrays;
 import java.util.Iterator;
-import java.util.Objects;
 import org.testng.annotations.Test;
 import static jdk.incubator.http.HttpClient.Version.HTTP_2;
 import static jdk.incubator.http.HttpRequest.BodyPublisher.fromString;
@@ -165,6 +165,15 @@
         }
     }
 
+    static void check(boolean cond, Object... msg) {
+        if (cond)
+            return;
+        StringBuilder sb = new StringBuilder();
+        for (Object o : msg)
+            sb.append(o);
+        throw new RuntimeException(sb.toString());
+    }
+
     static final String SIMPLE_STRING = "Hello world Goodbye world";
 
     static void simpleTest() throws Exception {
@@ -192,7 +201,33 @@
             .orElseThrow(() -> new RuntimeException("no previous response"));
         checkURIs(prev.uri(), httpURI);
 
+        checkPreviousRedirectResponses(req, response);
+
         System.err.println("DONE");
-        Thread.sleep (6000);
+    }
+
+    static void checkPreviousRedirectResponses(HttpRequest initialRequest,
+                                               HttpResponse<?> finalResponse) {
+        // there must be at least one previous response
+        finalResponse.previousResponse()
+                .orElseThrow(() -> new RuntimeException("no previous response"));
+
+        HttpResponse<?> response = finalResponse;
+        do {
+            URI uri = response.uri();
+            response = response.previousResponse().get();
+            check(300 <= response.statusCode() && response.statusCode() <= 309,
+                    "Expected 300 <= code <= 309, got:" + response.statusCode());
+            check(response.body() == null, "Unexpected body: " + response.body());
+            String locationHeader = response.headers().firstValue("Location")
+                    .orElseThrow(() -> new RuntimeException("no previous Location"));
+            check(uri.toString().endsWith(locationHeader),
+                    "URI: " + uri + ", Location: " + locationHeader);
+        } while (response.previousResponse().isPresent());
+
+        // initial
+        check(initialRequest.equals(response.request()),
+                "Expected initial request [%s] to equal last prev req [%s]",
+                initialRequest, response.request());
     }
 }