private static HttpResponse executeRequest()

in LocalHTTPServer.java [89:120]


    private static HttpResponse<byte[]> executeRequest(
            String uri,
            Executor httpClientExecutor,
            HttpExchange exchange
    ) throws IOException, InterruptedException {

        var cookie = System.getenv("X_COOKIE");
        var method = exchange.getRequestMethod();
        HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(URI.create(uri))
                .timeout(Duration.ofMillis(30000))
                .method(method.toUpperCase(), !method.equalsIgnoreCase("GET") ? HttpRequest.BodyPublishers.ofInputStream(exchange::getRequestBody): HttpRequest.BodyPublishers.noBody())
                .version(HttpClient.Version.HTTP_1_1);

        exchange.getRequestHeaders().forEach((name, value) -> {
            if (RESTRICTED_HEADERS.stream().noneMatch(name::equalsIgnoreCase)) {
                reqBuilder.header(name, String.join("", value));
                System.out.printf("header %s value %s%n", name, value);
            }
        });

        HttpRequest httpRequest = reqBuilder
                .header("Cookie", cookie)
                .build();

        HttpClient httpClient = HttpClient.newBuilder()
                .connectTimeout(Duration.ofMillis(15000))
                .executor(httpClientExecutor)
                .build();

        return httpClient
                .send(httpRequest, HttpResponse.BodyHandlers.ofByteArray());
    }