8228508: [TESTBUG] java/net/httpclient/SmokeTest.java fails on Windows7
authoraefimov
Tue, 13 Aug 2019 16:11:28 +0100
changeset 57730 0ec272e1822e
parent 57729 9c0715c5bbf3
child 57731 a414a1b8884a
8228508: [TESTBUG] java/net/httpclient/SmokeTest.java fails on Windows7 Reviewed-by: dfuchs, chegar
test/jdk/com/sun/net/httpserver/EchoHandler.java
test/jdk/java/net/httpclient/SmokeTest.java
--- a/test/jdk/com/sun/net/httpserver/EchoHandler.java	Tue Aug 13 16:05:58 2019 +0200
+++ b/test/jdk/com/sun/net/httpserver/EchoHandler.java	Tue Aug 13 16:11:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,37 +21,19 @@
  * questions.
  */
 
-import java.util.*;
-import java.util.concurrent.*;
-import java.util.logging.*;
-import java.io.*;
-import java.net.*;
-import java.security.*;
-import javax.net.ssl.*;
-import com.sun.net.httpserver.*;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
 
 /**
  * Implements a basic static EchoHandler for an HTTP server
  */
 public class EchoHandler implements HttpHandler {
-
-    byte[] read(InputStream is) throws IOException {
-        byte[] buf = new byte[1024];
-        byte[] result = new byte[0];
-
-        while (true) {
-            int n = is.read(buf);
-            if (n > 0) {
-                byte[] b1 = new byte[result.length + n];
-                System.arraycopy(result, 0, b1, 0, result.length);
-                System.arraycopy(buf, 0, b1, result.length, n);
-                result = b1;
-            } else if (n == -1) {
-                return result;
-            }
-        }
-    }
-
     public void handle (HttpExchange t)
         throws IOException
     {
@@ -61,32 +43,20 @@
 
         // return the number of bytes received (no echo)
         String summary = map.getFirst ("XSummary");
-        if (fixedrequest != null && summary == null)  {
-            byte[] in = read(is);
-            t.sendResponseHeaders(200, in.length);
-            OutputStream os = t.getResponseBody();
-            os.write(in);
-            close(t, os);
-            close(t, is);
+        OutputStream os = t.getResponseBody();
+        byte[] in;
+        in = is.readAllBytes();
+        if (summary != null) {
+            in = Integer.toString(in.length).getBytes(StandardCharsets.UTF_8);
+        }
+        if (fixedrequest != null) {
+            t.sendResponseHeaders(200, in.length == 0 ? -1 : in.length);
         } else {
-            OutputStream os = t.getResponseBody();
-            byte[] buf = new byte[64 * 1024];
             t.sendResponseHeaders(200, 0);
-            int n, count=0;;
-
-            while ((n = is.read(buf)) != -1) {
-                if (summary == null) {
-                    os.write(buf, 0, n);
-                }
-                count += n;
-            }
-            if (summary != null) {
-                String s = Integer.toString(count);
-                os.write(s.getBytes());
-            }
-            close(t, os);
-            close(t, is);
         }
+        os.write(in);
+        close(t, os);
+        close(t, is);
     }
 
     protected void close(OutputStream os) throws IOException {
--- a/test/jdk/java/net/httpclient/SmokeTest.java	Tue Aug 13 16:05:58 2019 +0200
+++ b/test/jdk/java/net/httpclient/SmokeTest.java	Tue Aug 13 16:11:28 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,7 +30,6 @@
  * @library /test/lib /
  * @build jdk.test.lib.net.SimpleSSLContext ProxyServer
  * @compile ../../../com/sun/net/httpserver/LogFilter.java
- * @compile ../../../com/sun/net/httpserver/EchoHandler.java
  * @compile ../../../com/sun/net/httpserver/FileServerHandler.java
  * @run main/othervm
  *      -Djdk.internal.httpclient.debug=true
@@ -50,7 +49,10 @@
 import java.net.InetAddress;
 import java.net.Proxy;
 import java.net.SocketAddress;
+import java.net.http.HttpHeaders;
+import java.nio.charset.StandardCharsets;
 import java.util.Collections;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.net.InetSocketAddress;
@@ -135,6 +137,22 @@
     static Path smallFile;
     static String fileroot;
 
+    static class HttpEchoHandler implements HttpHandler {
+
+        @Override
+        public void handle(HttpExchange exchange) throws IOException {
+            try (InputStream is = exchange.getRequestBody();
+                 OutputStream os = exchange.getResponseBody()) {
+                byte[] bytes = is.readAllBytes();
+                long responseLength = bytes.length == 0 ? -1 : bytes.length;
+                boolean fixedLength = "yes".equals(exchange.getRequestHeaders()
+                        .getFirst("XFixed"));
+                exchange.sendResponseHeaders(200, fixedLength ? responseLength : 0);
+                os.write(bytes);
+            }
+        }
+    }
+
     static String getFileContent(String path) throws IOException {
         FileInputStream fis = new FileInputStream(path);
         byte[] buf = new byte[2000];
@@ -257,6 +275,8 @@
 
         HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
 
+        checkResponseContentLength(response.headers(), fixedLen);
+
         String body = response.body();
         if (!body.equals("This is foo.txt\r\n")) {
             throw new RuntimeException("Did not get expected body: "
@@ -504,6 +524,8 @@
 
         HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
 
+        checkResponseContentLength(response.headers(), fixedLen);
+
         String body = response.body();
 
         if (!body.equals(requestBody)) {
@@ -529,6 +551,8 @@
 
         HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
 
+        checkResponseContentLength(response.headers(), fixedLen);
+
         if (response.statusCode() != 200) {
             throw new RuntimeException(
                     "Expected 200, got [ " + response.statusCode() + " ]");
@@ -694,7 +718,7 @@
 
         try {
             HttpResponse<String> response = cf.join();
-            throw new RuntimeException("Exepected Completion Exception");
+            throw new RuntimeException("Expected Completion Exception");
         } catch (CompletionException e) {
             //System.out.println(e);
         }
@@ -739,12 +763,12 @@
 
         HttpContext c1 = s1.createContext("/files", h);
         HttpContext c2 = s2.createContext("/files", h);
-        HttpContext c3 = s1.createContext("/echo", new EchoHandler());
+        HttpContext c3 = s1.createContext("/echo", new HttpEchoHandler());
         redirectHandler = new RedirectHandler("/redirect");
         redirectHandlerSecure = new RedirectHandler("/redirect");
         HttpContext c4 = s1.createContext("/redirect", redirectHandler);
         HttpContext c41 = s2.createContext("/redirect", redirectHandlerSecure);
-        HttpContext c5 = s2.createContext("/echo", new EchoHandler());
+        HttpContext c5 = s2.createContext("/echo", new HttpEchoHandler());
         HttpContext c6 = s1.createContext("/keepalive", new KeepAliveHandler());
         redirectErrorHandler = new RedirectErrorHandler("/redirecterror");
         redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror");
@@ -776,6 +800,19 @@
         System.out.println("Proxy port = " + proxyPort);
     }
 
+    static void checkResponseContentLength(HttpHeaders responseHeaders, boolean fixedLen) {
+        Optional<String> transferEncoding = responseHeaders.firstValue("transfer-encoding");
+        Optional<String> contentLength = responseHeaders.firstValue("content-length");
+        if (fixedLen) {
+            assert contentLength.isPresent();
+            assert !transferEncoding.isPresent();
+        } else {
+            assert !contentLength.isPresent();
+            assert transferEncoding.isPresent();
+            assert "chunked".equals(transferEncoding.get());
+        }
+    }
+
     static class RedirectHandler implements HttpHandler {
         private final String root;
         private volatile int count = 0;
@@ -786,9 +823,8 @@
 
         @Override
         public synchronized void handle(HttpExchange t) throws IOException {
-            byte[] buf = new byte[2048];
             try (InputStream is = t.getRequestBody()) {
-                while (is.read(buf) != -1) ;
+                is.readAllBytes();
             }
 
             Headers responseHeaders = t.getResponseHeaders();
@@ -1010,14 +1046,13 @@
                 System.out.println(result);
             }
         }
-        byte[] buf = new byte[2048];
 
         try (InputStream is = t.getRequestBody()) {
-            while (is.read(buf) != -1) ;
+            is.readAllBytes();
         }
         t.sendResponseHeaders(200, result.length());
         OutputStream o = t.getResponseBody();
-        o.write(result.getBytes("US-ASCII"));
+        o.write(result.getBytes(StandardCharsets.UTF_8));
         t.close();
         nparallel.getAndDecrement();
     }