All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.coreoz.http.mock.LocalHttpClient Maven / Gradle / Ivy

package com.coreoz.http.mock;

import com.google.common.net.HttpHeaders;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.function.Function;

public class LocalHttpClient {
    public static HttpResponse makeHttpGetRequest(int localPort, String path) throws IOException, InterruptedException {
        return makeHttpRequest(localPort, path, HttpRequest.Builder::GET);
    }

    public static HttpResponse makeHttpRequest(int localPort, String path, Function with) throws IOException, InterruptedException {
        return HttpClient.newHttpClient().send(
            with.apply(HttpRequest.newBuilder().uri(URI.create("http://localhost:" + localPort + path)))
                .header(HttpHeaders.ACCEPT, "custom_accept")
                .header(HttpHeaders.AUTHORIZATION, "custom_auth")
                .build(),
            HttpResponse.BodyHandlers.ofString()
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy