Fixes #4929 - HttpClient: HttpCookieStore.Empty prevents sending cookies.

Signed-off-by: Simone Bordet <simone.bordet@gmail.com>
This commit is contained in:
Simone Bordet 2020-06-01 17:02:28 +02:00
parent 84cb97e6bd
commit 46c62d48d2
2 changed files with 21 additions and 5 deletions

View File

@ -149,17 +149,16 @@ public abstract class HttpConnection implements Connection
} }
// Cookies // Cookies
StringBuilder cookies = convertCookies(request.getCookies(), null);
CookieStore cookieStore = getHttpClient().getCookieStore(); CookieStore cookieStore = getHttpClient().getCookieStore();
if (cookieStore != null && cookieStore.getClass() != HttpCookieStore.Empty.class) if (cookieStore != null && cookieStore.getClass() != HttpCookieStore.Empty.class)
{ {
StringBuilder cookies = null;
URI uri = request.getURI(); URI uri = request.getURI();
if (uri != null) if (uri != null)
cookies = convertCookies(HttpCookieStore.matchPath(uri, cookieStore.get(uri)), null); cookies = convertCookies(HttpCookieStore.matchPath(uri, cookieStore.get(uri)), cookies);
cookies = convertCookies(request.getCookies(), cookies); }
if (cookies != null) if (cookies != null)
request.header(HttpHeader.COOKIE.asString(), cookies.toString()); request.header(HttpHeader.COOKIE.asString(), cookies.toString());
}
// Authentication // Authentication
applyProxyAuthentication(request, proxy); applyProxyAuthentication(request, proxy);

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.client; package org.eclipse.jetty.client;
import java.net.CookieStore;
import java.net.HttpCookie; import java.net.HttpCookie;
import java.net.URI; import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
@ -34,6 +35,7 @@ import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.util.HttpCookieStore;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.provider.ArgumentsSource;
@ -132,6 +134,18 @@ public class HttpCookieTest extends AbstractHttpClientServerTest
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(ScenarioProvider.class) @ArgumentsSource(ScenarioProvider.class)
public void testPerRequestCookieIsSent(Scenario scenario) throws Exception public void testPerRequestCookieIsSent(Scenario scenario) throws Exception
{
testPerRequestCookieIsSent(scenario, null);
}
@ParameterizedTest
@ArgumentsSource(ScenarioProvider.class)
public void testPerRequestCookieIsSentWithEmptyCookieStore(Scenario scenario) throws Exception
{
testPerRequestCookieIsSent(scenario, new HttpCookieStore.Empty());
}
private void testPerRequestCookieIsSent(Scenario scenario, CookieStore cookieStore) throws Exception
{ {
final String name = "foo"; final String name = "foo";
final String value = "bar"; final String value = "bar";
@ -149,6 +163,9 @@ public class HttpCookieTest extends AbstractHttpClientServerTest
} }
}); });
if (cookieStore != null)
client.setCookieStore(cookieStore);
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()) ContentResponse response = client.newRequest("localhost", connector.getLocalPort())
.scheme(scenario.getScheme()) .scheme(scenario.getScheme())
.cookie(new HttpCookie(name, value)) .cookie(new HttpCookie(name, value))