package rde.tests.net.http;

import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * Test proxy authentication failure with http and https targets.
 *
 * Arguments are proxy host and port.
 *
 * @author Richard Evans
 */

public class proxyauthhttps {

  public static void main(String ... args) throws Exception {

    InetSocketAddress proxy  = InetSocketAddress.createUnresolved(args[0], Integer.parseInt(args[1]));
    HttpClient        client = HttpClient.newBuilder().proxy(ProxySelector.of(proxy)).build();

    run(client, "http://nowhere/");
    run(client, "https://nowhere/");
  }

  private static void run(HttpClient client, String target) throws Exception {
    HttpRequest          req  = HttpRequest.newBuilder(new URI(target)).method("GET", HttpRequest.BodyPublishers.noBody()).build();
    HttpResponse<String> res  = client.send(req, HttpResponse.BodyHandlers.ofString());
    String               body = res.body();

    System.out.printf("%s: status code = %d; body %s%n", target, res.statusCode(), body == null ? "is null" : "length: " + body.length());
  }
}
