Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

Custom Apache Clients ignores cookie management #412

Closed timomeinen closed 2 years ago

timomeinen commented 2 years ago

Describe the bug Having a custom configured Apache Client, it is not possible to disable cookie management.

To Reproduce

static {
    HttpClient baseClient =
            HttpClients.custom()
                    .setConnectionManager(new BasicHttpClientConnectionManager())
                    .build();

    Unirest.config()
            .enableCookieManagement(false)
            .httpClient(ApacheClient.builder(baseClient));
}

public static void main(String[] args) {
    System.out.println("Set-Cookie = " + getSetCookieHeader());

    // Second invocation throws exception as a Cookie is sent in the request
    // and therefore no Set-Cookie is returned.
    System.out.println("Set-Cookie = " + getSetCookieHeader());
}

private static String getSetCookieHeader() {
    return Unirest.get("https://www.google.com")
            .asString()
            .getHeaders().get("Set-Cookie")
            .stream()
            .findAny()
            .orElseThrow(() -> new RuntimeException("Set-Cookie not found"));
}

Expected behavior As enableCookieManagement is disabled, I would expect that Unirest does not store the cookie and does not sent it in the second request.

Environmental Data:

ryber commented 2 years ago

Unirest doest store cookies, Apache does. and by setting your own client it ignores the Unirest settings. There is no opportunity to build a client with cookie management off because you just did it.

If you want it off AND to use a custom client you need to call .disableCookieManagement()

The only thing Unirest COULD do in this situation would be to throw an exception when it's in this state.

timomeinen commented 2 years ago

Ok - understand and I can confirm, that setting .disableCookieManagement() on the client works.

I was confused, because I thought, that Unirest.config().enableCookieManagement(false) would be configured on the Apache client in any case - no matter if it is a Unirest configured or a manual configured client.