Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDK-8273142 : Remove dependancy of TestHttpServer, HttpTransaction, HttpCallback from open/test/jdk/sun/net/www/protocol/http/ tests #5352

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 36 additions & 16 deletions test/jdk/sun/net/www/protocol/http/B6296310.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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 @@ -24,17 +24,31 @@
/*
* @test
* @bug 6296310
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer HttpTransaction
* @library /test/lib
* @run main/othervm B6296310
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6296310
* @summary REGRESSION: AppletClassLoader.getResourceAsStream() behaviour is wrong in some cases
*/

import java.net.*;
import java.io.*;
import java.util.*;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

/*
* http server returns 200 and content-length=0
Expand All @@ -44,7 +58,7 @@
public class B6296310
{
static SimpleHttpTransaction httpTrans;
static TestHttpServer server;
static HttpServer server;

public static void main(String[] args) throws Exception
{
Expand All @@ -56,31 +70,37 @@ public static void main(String[] args) throws Exception
public static void startHttpServer() throws IOException {
httpTrans = new SimpleHttpTransaction();
InetAddress loopback = InetAddress.getLoopbackAddress();
server = new TestHttpServer(httpTrans, 1, 10, loopback, 0);
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", httpTrans);
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
}

public static void makeHttpCall() throws IOException {
try {
System.out.println("http server listen on: " + server.getLocalPort());
System.out.println("http server listen on: " + server.getAddress().getPort());
URL url = new URL("http" , InetAddress.getLoopbackAddress().getHostAddress(),
server.getLocalPort(), "/");
server.getAddress().getPort(), "/");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
System.out.println(uc.getResponseCode());
} finally {
server.terminate();
server.stop(1);
}
}
}

class SimpleHttpTransaction implements HttpCallback
class SimpleHttpTransaction implements HttpHandler
{
/*
* Our http server which simply retruns a file with no content
*/
public void request(HttpTransaction trans) {
@Override
public void handle(HttpExchange trans) {
try {
trans.setResponseEntityBody("");
trans.sendResponse(200, "OK");
trans.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(trans.getResponseBody())) {
pw.print("");
}
Copy link
Member

Choose a reason for hiding this comment

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

Not sure what the purpose of this block is, which apparently writes no bytes to the OutputStream?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @Michael-Mc-Mahon , In my understanding purpose of this block is to return a file with no contents.(Empty file).

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be simpler to replace lines 101-103 just with trans.close()

Copy link
Member

Choose a reason for hiding this comment

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

Strictly speaking to preserve the current behavior - that would be trans.getResponseBody().close(); isn't it?

Copy link
Member

Choose a reason for hiding this comment

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

HttpExchange.close() calls the close method on both streams and closing the output stream implicitly closes the input stream. So, they are equivalent.

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
72 changes: 45 additions & 27 deletions test/jdk/sun/net/www/protocol/http/RelativeRedirect.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2021, 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 @@ -24,20 +24,32 @@
/**
* @test
* @bug 4726087
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main RelativeRedirect
* @library /test/lib
* @run main/othervm RelativeRedirect
* @run main/othervm -Djava.net.preferIPv6Addresses=true RelativeRedirect
* @summary URLConnection cannot handle redirects
*/

import java.io.*;
import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.URL;
import java.util.concurrent.Executors;

public class RelativeRedirect implements HttpCallback {
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class RelativeRedirect implements HttpHandler {
static int count = 0;
static TestHttpServer server;
static HttpServer server;


static class MyAuthenticator extends Authenticator {
public MyAuthenticator () {
Expand All @@ -50,26 +62,29 @@ public PasswordAuthentication getPasswordAuthentication ()
}
}

void firstReply (HttpTransaction req) throws IOException {
req.addResponseHeader ("Connection", "close");
req.addResponseHeader ("Location", "/redirect/file.html");
req.sendResponse (302, "Moved Permamently");
req.orderlyClose();
void firstReply(HttpExchange req) throws IOException {
req.getResponseHeaders().set("Connection", "close");
req.getResponseHeaders().set("Location", "/redirect/file.html");
req.sendResponseHeaders(302, -1);
}

void secondReply (HttpTransaction req) throws IOException {
void secondReply (HttpExchange req) throws IOException {
if (req.getRequestURI().toString().equals("/redirect/file.html") &&
req.getRequestHeader("Host").equals(authority(server.getLocalPort()))) {
req.setResponseEntityBody ("Hello .");
req.sendResponse (200, "Ok");
req.getRequestHeaders().get("Host").get(0).equals(authority(server.getAddress().getPort()))) {
req.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print("Hello .");
}
} else {
req.setResponseEntityBody (req.getRequestURI().toString());
req.sendResponse (400, "Bad request");
req.sendResponseHeaders(400, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print(req.getRequestURI().toString());
}
}
req.orderlyClose();

}
public void request (HttpTransaction req) {

@Override
public void handle (HttpExchange req) {
try {
switch (count) {
case 0:
Expand Down Expand Up @@ -101,9 +116,12 @@ public static void main (String[] args) throws Exception {
MyAuthenticator auth = new MyAuthenticator ();
Authenticator.setDefault (auth);
try {
server = new TestHttpServer (new RelativeRedirect(), 1, 10, loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort());
URL url = new URL("http://" + authority(server.getLocalPort()));
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", new RelativeRedirect());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
System.out.println ("Server: listening on port: " + server.getAddress().getPort());
URL url = new URL("http://" + authority(server.getAddress().getPort()));
System.out.println ("client opening connection to: " + url);
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
InputStream is = urlc.getInputStream ();
Expand All @@ -112,7 +130,7 @@ public static void main (String[] args) throws Exception {
throw new RuntimeException(e);
} finally {
if (server != null) {
server.terminate();
server.stop(1);
}
}
}
Expand Down
68 changes: 43 additions & 25 deletions test/jdk/sun/net/www/protocol/http/ResponseCacheStream.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, 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 @@ -25,33 +25,48 @@
* @test
* @bug 6262486
* @library /test/lib
* @modules java.base/sun.net.www
* @library ../../httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main/othervm -Dhttp.keepAlive=false ResponseCacheStream
* @summary COMPATIBILITY: jagex_com - Monkey Puzzle applet fails to load
*/

import java.net.*;
import java.io.*;
import java.util.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import jdk.test.lib.net.URIBuilder;

public class ResponseCacheStream implements HttpCallback {
public class ResponseCacheStream implements HttpHandler {

void okReply (HttpTransaction req) throws IOException {
req.setResponseEntityBody ("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
req.sendResponse (200, "Ok");
System.out.println ("Server: sent response");
req.orderlyClose();
void okReply (HttpExchange req) throws IOException {
req.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print("Hello, This is the response body. Let's make it as long as possible since we need to test the cache mechanism.");
}
System.out.println ("Server: sent response");
}

public void request (HttpTransaction req) {
try {
okReply (req);
} catch (IOException e) {
e.printStackTrace();
}

@Override
public void handle(HttpExchange exchange) throws IOException {
okReply(exchange);
exchange.close();
}

static class MyCacheRequest extends CacheRequest {
Expand Down Expand Up @@ -94,19 +109,22 @@ public byte[] getBuffer() {
}
}

static TestHttpServer server;
static HttpServer server;

public static void main(String[] args) throws Exception {
MyResponseCache cache = new MyResponseCache();
try {
InetAddress loopback = InetAddress.getLoopbackAddress();
ResponseCache.setDefault(cache);
server = new TestHttpServer (new ResponseCacheStream(), loopback, 0);
System.out.println ("Server: listening on port: " + server.getLocalPort());
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", new ResponseCacheStream());
server.setExecutor(Executors.newSingleThreadExecutor());
server.start();
System.out.println("Server: listening on port: " + server.getAddress().getPort());
URL url = URIBuilder.newBuilder()
.scheme("http")
.loopback()
.port(server.getLocalPort())
.port(server.getAddress().getPort())
.path("/")
.toURL();
System.out.println ("Client: connecting to " + url);
Expand Down Expand Up @@ -149,10 +167,10 @@ public static void main(String[] args) throws Exception {
}
} catch (Exception e) {
if (server != null) {
server.terminate();
server.stop(1);
}
throw e;
}
server.terminate();
server.stop(1);
}
}