Skip to content

Commit

Permalink
8304927: Update java/net/httpclient/BasicAuthTest.java to check basic…
Browse files Browse the repository at this point in the history
… auth over HTTP/2

Reviewed-by: jpai
  • Loading branch information
dfuch committed Mar 28, 2023
1 parent ca745cb commit 50a995f
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 191 deletions.
87 changes: 51 additions & 36 deletions test/jdk/java/net/httpclient/BasicAuthTest.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, 2023, 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,87 +24,102 @@
/**
* @test
* @bug 8087112
* @modules java.net.http
* jdk.httpserver
* @library /test/lib /test/jdk/java/net/httpclient/lib
* @build jdk.httpclient.test.lib.common.HttpServerAdapters jdk.test.lib.net.SimpleSSLContext
* @run main/othervm BasicAuthTest
* @summary Basic Authentication Test
*/

import com.sun.net.httpserver.BasicAuthenticator;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import jdk.httpclient.test.lib.common.HttpServerAdapters;
import jdk.test.lib.net.SimpleSSLContext;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLContext;

import static java.nio.charset.StandardCharsets.US_ASCII;

public class BasicAuthTest {
public class BasicAuthTest implements HttpServerAdapters {

static volatile boolean ok;
static final String RESPONSE = "Hello world";
static final String POST_BODY = "This is the POST body 123909090909090";

public static void main(String[] args) throws Exception {
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),0);
HttpServer server = HttpServer.create(addr, 10);
test(Version.HTTP_1_1, false);
test(Version.HTTP_2, false);
test(Version.HTTP_1_1, true);
test(Version.HTTP_2, true);
}

public static void test(Version version, boolean secure) throws Exception {

ExecutorService e = Executors.newCachedThreadPool();
Handler h = new Handler();
HttpContext serverContext = server.createContext("/test", h);
int port = server.getAddress().getPort();
System.out.println("Server port = " + port);

ClientAuth ca = new ClientAuth();
SSLContext sslContext = secure ? new SimpleSSLContext().get() : null;
HttpTestServer server = HttpTestServer.create(version, sslContext, e);
HttpTestContext serverContext = server.addHandler(h,"/test/");
ServerAuth sa = new ServerAuth("foo realm");
serverContext.setAuthenticator(sa);
server.setExecutor(e);
server.start();
HttpClient client = HttpClient.newBuilder()
.authenticator(ca)
.build();
System.out.println("Server auth = " + server.serverAuthority());

ClientAuth ca = new ClientAuth();
var clientBuilder = HttpClient.newBuilder();
if (sslContext != null) clientBuilder.sslContext(sslContext);
HttpClient client = clientBuilder.authenticator(ca).build();

try {
URI uri = new URI("http://localhost:" + Integer.toString(port) + "/test/foo");
HttpRequest req = HttpRequest.newBuilder(uri).GET().build();
String scheme = sslContext == null ? "http" : "https";
URI uri = new URI(scheme + "://" + server.serverAuthority() + "/test/foo/"+version);
var builder = HttpRequest.newBuilder(uri);
HttpRequest req = builder.copy().GET().build();

System.out.println("\n\nSending request: " + req);
HttpResponse resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);

if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
var count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);

// repeat same request, should succeed but no additional authenticator calls

System.out.println("\n\nRepeat request: " + req);
resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200 && resp.body().equals(RESPONSE);

if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);

// try a POST

req = HttpRequest.newBuilder(uri)
.POST(BodyPublishers.ofString(POST_BODY))
req = builder.copy().POST(BodyPublishers.ofString(POST_BODY))
.build();
System.out.println("\n\nSending POST request: " + req);
resp = client.send(req, BodyHandlers.ofString());
ok = resp.statusCode() == 200;

if (!ok || ca.count != 1)
throw new RuntimeException("Test failed");
count = ca.count;
if (!ok || count != 1)
throw new RuntimeException("Test failed: ca.count=" + count);


} finally {
server.stop(0);
server.stop();
e.shutdownNow();
}
System.out.println("OK");
Expand Down Expand Up @@ -136,20 +151,20 @@ protected PasswordAuthentication getPasswordAuthentication() {
}
}

static class Handler implements HttpHandler {
static class Handler implements HttpTestHandler {
static volatile boolean ok;

@Override
public void handle(HttpExchange he) throws IOException {
public void handle(HttpTestExchange he) throws IOException {
String method = he.getRequestMethod();
InputStream is = he.getRequestBody();
if (method.equalsIgnoreCase("POST")) {
String requestBody = new String(is.readAllBytes(), US_ASCII);
if (!requestBody.equals(POST_BODY)) {
he.sendResponseHeaders(500, -1);
he.sendResponseHeaders(500, 0);
ok = false;
} else {
he.sendResponseHeaders(200, -1);
he.sendResponseHeaders(200, 0);
ok = true;
}
} else { // GET
Expand Down
Loading

3 comments on commit 50a995f

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 50a995f Apr 10, 2024

Choose a reason for hiding this comment

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

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 50a995f Apr 10, 2024

Choose a reason for hiding this comment

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

@GoeLin the backport was successfully created on the branch backport-GoeLin-50a995f0 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 50a995f0 from the openjdk/jdk repository.

The commit being backported was authored by Daniel Fuchs on 28 Mar 2023 and was reviewed by Jaikiran Pai.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-50a995f0:backport-GoeLin-50a995f0
$ git checkout backport-GoeLin-50a995f0
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-GoeLin-50a995f0

Please sign in to comment.