Skip to content

Commit eec0fe5

Browse files
committed
added cookies test. fixed handling Bad Gateway status from server
1 parent 82cbb6a commit eec0fe5

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ public CompletableFuture<CommandResponse> execute(String sql) {
11531153
} catch (Exception e) {
11541154
throw new ClientException("Failed to get command response", e);
11551155
}
1156-
});
1156+
}, sharedOperationExecutor);
11571157
}
11581158

11591159
private String startOperation() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ public ClassicHttpResponse executeRequest(ClickHouseNode server, Map<String, Obj
145145
} finally {
146146
httpResponse.close();
147147
}
148-
} else if (httpResponse.getCode() >= 500) {
148+
} else if (httpResponse.getCode() == HttpStatus.SC_BAD_GATEWAY) {
149+
httpResponse.close();
150+
throw new ClientException("Server returned '502 Bad gateway'. Check network and proxy settings.");
151+
} else if (httpResponse.getCode() >= HttpStatus.SC_INTERNAL_SERVER_ERROR) {
149152
httpResponse.close();
150153
return httpResponse;
151154
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import com.github.tomakehurst.wiremock.client.WireMock;
1313
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
1414
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
15+
import com.github.tomakehurst.wiremock.http.Fault;
16+
import com.github.tomakehurst.wiremock.stubbing.Scenario;
1517
import org.apache.hc.core5.http.HttpHeaders;
1618
import org.apache.hc.core5.http.HttpStatus;
1719
import org.testng.Assert;
@@ -105,6 +107,31 @@ public void testPrivateProxyWithCredentials() {
105107
}
106108
}
107109

110+
@Test(groups = { "integration" })
111+
public void testProxyWithCookies() {
112+
client.set(clientBuilder(initProxy(), true).build());
113+
final int targetPort = getServer(ClickHouseProtocol.HTTP).getPort();
114+
115+
proxy.get().addStubMapping(post(urlMatching("/.*"))
116+
.inScenario("routeCookies")
117+
.whenScenarioStateIs(Scenario.STARTED)
118+
.willReturn(aResponse().withHeader(HttpHeaders.SET_COOKIE, "routeName=routeA")
119+
.proxiedFrom("http://localhost:" + targetPort)).willSetStateTo("cookies").build());
120+
121+
proxy.get().addStubMapping(post(urlMatching("/.*"))
122+
.inScenario("routeCookies")
123+
.whenScenarioStateIs("cookies")
124+
.withHeader(HttpHeaders.COOKIE, equalTo("routeName=routeA"))
125+
.willReturn(aResponse().proxiedFrom("http://localhost:" + targetPort)).build());
126+
127+
try {
128+
client.get().execute("select 1").get();
129+
client.get().execute("select 1").get();
130+
} catch (Exception e) {
131+
fail("Should not have thrown exception.", e);
132+
}
133+
}
134+
108135
private Client.Builder clientBuilder(int proxyPort, boolean onlyNewImplementation) {
109136
return new Client.Builder()
110137
.addEndpoint(Protocol.HTTP, "clickhouse", 8123, false)

0 commit comments

Comments
 (0)