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

Reviewed-by: michaelm
This commit is contained in:
Mahendra Chhipa 2021-09-30 11:29:20 +00:00 committed by Michael McMahon
parent 94e31e5ca5
commit 2f955d6f5b
4 changed files with 155 additions and 104 deletions

View File

@ -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
@ -24,17 +24,30 @@
/*
* @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.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
@ -44,7 +57,7 @@ import java.util.*;
public class B6296310
{
static SimpleHttpTransaction httpTrans;
static TestHttpServer server;
static HttpServer server;
public static void main(String[] args) throws Exception
{
@ -56,31 +69,35 @@ public class B6296310
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);
trans.close();
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -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
@ -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 () {
@ -50,26 +62,29 @@ public class RelativeRedirect implements HttpCallback {
}
}
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:
@ -101,9 +116,12 @@ public class RelativeRedirect implements HttpCallback {
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 ();
@ -112,7 +130,7 @@ public class RelativeRedirect implements HttpCallback {
throw new RuntimeException(e);
} finally {
if (server != null) {
server.terminate();
server.stop(1);
}
}
}

View File

@ -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
@ -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 {
@ -94,19 +109,22 @@ public class ResponseCacheStream implements HttpCallback {
}
}
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);
@ -149,10 +167,10 @@ public class ResponseCacheStream implements HttpCallback {
}
} catch (Exception e) {
if (server != null) {
server.terminate();
server.stop(1);
}
throw e;
}
server.terminate();
server.stop(1);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 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
@ -24,56 +24,54 @@
/**
* @test
* @bug 5049976
* @modules java.base/sun.net.www
* @library ../../httptest/
* @library /test/lib
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
* @run main SetChunkedStreamingMode
* @run main/othervm SetChunkedStreamingMode
* @summary Unspecified NPE is thrown when streaming output mode is enabled
*/
import java.io.*;
import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
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 SetChunkedStreamingMode implements HttpCallback {
public class SetChunkedStreamingMode implements HttpHandler {
void okReply (HttpTransaction req) throws IOException {
req.setResponseEntityBody ("Hello .");
req.sendResponse (200, "Ok");
System.out.println ("Server: sent response");
req.orderlyClose();
}
public void request (HttpTransaction req) {
try {
okReply (req);
} catch (IOException e) {
e.printStackTrace();
void okReply (HttpExchange req) throws IOException {
req.sendResponseHeaders(200, 0);
try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {
pw.print("Hello .");
}
System.out.println ("Server: sent response");
}
static void read (InputStream is) throws IOException {
int c;
System.out.println ("reading");
while ((c=is.read()) != -1) {
System.out.write (c);
}
System.out.println ("");
System.out.println ("finished reading");
@Override
public void handle(HttpExchange exchange) throws IOException {
okReply(exchange);
}
static TestHttpServer server;
static HttpServer server;
public static void main (String[] args) throws Exception {
try {
server = new TestHttpServer(new SetChunkedStreamingMode(), 1, 10,
InetAddress.getLoopbackAddress(), 0);
System.out.println ("Server: listening on port: " + server.getLocalPort());
InetAddress loopback = InetAddress.getLoopbackAddress();
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10);
server.createContext("/", new SetChunkedStreamingMode());
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);
@ -84,15 +82,15 @@ public class SetChunkedStreamingMode implements HttpCallback {
InputStream is = urlc.getInputStream();
} catch (Exception e) {
if (server != null) {
server.terminate();
server.stop(1);
}
throw e;
}
server.terminate();
server.stop(1);
}
public static void except (String s) {
server.terminate();
server.stop(1);
throw new RuntimeException (s);
}
}