Skip to content

Commit

Permalink
8228508: [TESTBUG] java/net/httpclient/SmokeTest.java fails on Windows7
Browse files Browse the repository at this point in the history
Backport-of: b4a7fb8
  • Loading branch information
GoeLin committed Sep 22, 2021
1 parent d78b4c7 commit 133de42
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 60 deletions.
70 changes: 20 additions & 50 deletions test/jdk/com/sun/net/httpserver/EchoHandler.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
{
Expand All @@ -61,32 +43,20 @@ public void handle (HttpExchange t)

// 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 {
Expand Down
55 changes: 45 additions & 10 deletions test/jdk/java/net/httpclient/SmokeTest.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,7 +30,6 @@
* @library /lib/testlibrary/ /
* @build jdk.testlibrary.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
Expand All @@ -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;
Expand Down Expand Up @@ -135,6 +137,22 @@ public class SmokeTest {
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];
Expand Down Expand Up @@ -257,6 +275,8 @@ static void test1(String target, boolean fixedLen) throws Exception {

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: "
Expand Down Expand Up @@ -504,6 +524,8 @@ static void test5(String target, boolean fixedLen) throws Exception {

HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

checkResponseContentLength(response.headers(), fixedLen);

String body = response.body();

if (!body.equals(requestBody)) {
Expand All @@ -529,6 +551,8 @@ static void test6(String target, boolean fixedLen) throws Exception {

HttpResponse<String> response = client.send(request, BodyHandlers.ofString());

checkResponseContentLength(response.headers(), fixedLen);

if (response.statusCode() != 200) {
throw new RuntimeException(
"Expected 200, got [ " + response.statusCode() + " ]");
Expand Down Expand Up @@ -694,7 +718,7 @@ static void test10(String s) throws Exception {

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);
}
Expand Down Expand Up @@ -739,12 +763,12 @@ static void initServer() throws Exception {

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");
Expand Down Expand Up @@ -776,6 +800,19 @@ static void initServer() throws Exception {
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;
Expand All @@ -786,9 +823,8 @@ static class RedirectHandler implements HttpHandler {

@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();
Expand Down Expand Up @@ -1010,14 +1046,13 @@ public void handle (HttpExchange t)
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();
}
Expand Down

1 comment on commit 133de42

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.