Skip to content

Commit 64569f6

Browse files
committed
added option to disable cookies
1 parent eec0fe5 commit 64569f6

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ public Builder useNewImplementation(boolean useNewImplementation) {
450450
return this;
451451
}
452452

453+
public Builder setHttpCookiesEnabled(boolean enabled) {
454+
//TODO: extract to settings string constants
455+
this.configuration.put("client.http.cookies_enabled", String.valueOf(enabled));
456+
return this;
457+
}
458+
453459
public Client build() {
454460
// check if endpoint are empty. so can not initiate client
455461
if (this.endpoints.isEmpty()) {

client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public CloseableHttpClient createHttpClient() {
9090
} else if (proxyType == ProxyType.SOCKS) {
9191
soCfgBuilder.setSocksProxyAddress(new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort)));
9292
}
93+
94+
if (chConfiguration.getOrDefault("client.http.cookies_enabled", "true")
95+
.equalsIgnoreCase("false")) {
96+
clientBuilder.disableCookieManagement();
97+
}
9398
clientBuilder.setDefaultCredentialsProvider(credProviderBuilder.build());
9499
return clientBuilder.build();
95100
}

client-v2/src/test/java/com/clickhouse/client/ProxyTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.IOException;
2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.concurrent.ExecutionException;
2728
import java.util.concurrent.TimeUnit;
2829

2930
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -132,6 +133,37 @@ public void testProxyWithCookies() {
132133
}
133134
}
134135

136+
@Test(groups = { "integration" })
137+
public void testProxyWithDisabledCookies() {
138+
client.set(clientBuilder(initProxy(), true).setHttpCookiesEnabled(false).build());
139+
final int targetPort = getServer(ClickHouseProtocol.HTTP).getPort();
140+
141+
proxy.get().addStubMapping(post(urlMatching("/.*"))
142+
.inScenario("routeCookies")
143+
.whenScenarioStateIs(Scenario.STARTED)
144+
.willReturn(aResponse().withHeader(HttpHeaders.SET_COOKIE, "routeName=routeA")
145+
.proxiedFrom("http://localhost:" + targetPort)).willSetStateTo("cookies").build());
146+
147+
proxy.get().addStubMapping(post(urlMatching("/.*"))
148+
.inScenario("routeCookies")
149+
.whenScenarioStateIs("cookies")
150+
.withHeader(HttpHeaders.COOKIE, equalTo("routeName=routeA"))
151+
.willReturn(aResponse().proxiedFrom("http://localhost:" + targetPort)).build());
152+
153+
try {
154+
client.get().execute("select 1").get();
155+
} catch (Exception e) {
156+
fail("Should not have thrown exception.", e);
157+
}
158+
try {
159+
client.get().execute("select 1").get();
160+
} catch (ExecutionException e) {
161+
Assert.assertTrue(e.getCause() instanceof ClientException);
162+
} catch (Exception e) {
163+
fail("Should have thrown exception.", e);
164+
}
165+
}
166+
135167
private Client.Builder clientBuilder(int proxyPort, boolean onlyNewImplementation) {
136168
return new Client.Builder()
137169
.addEndpoint(Protocol.HTTP, "clickhouse", 8123, false)

0 commit comments

Comments
 (0)