Skip to content

Commit eda6d18

Browse files
authored
Switch low level rest tests to new style Requests (#31938)
In #29623 we added `Request` object flavored requests to the low level REST client and in #30315 we deprecated the old `performRequest`s. This changes all calls in the `client/rest` project to use the new versions.
1 parent aa6a1c5 commit eda6d18

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

client/rest/src/test/java/org/elasticsearch/client/RestClientBuilderIntegTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {
7676
try {
7777
try (RestClient client = buildRestClient()) {
7878
try {
79-
client.performRequest("GET", "/");
79+
client.performRequest(new Request("GET", "/"));
8080
fail("connection should have been rejected due to SSL handshake");
8181
} catch (Exception e) {
8282
assertThat(e.getMessage(), containsString("General SSLEngine problem"));
@@ -85,7 +85,7 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {
8585

8686
SSLContext.setDefault(getSslContext());
8787
try (RestClient client = buildRestClient()) {
88-
Response response = client.performRequest("GET", "/");
88+
Response response = client.performRequest(new Request("GET", "/"));
8989
assertEquals(200, response.getStatusLine().getStatusCode());
9090
}
9191
} finally {

client/rest/src/test/java/org/elasticsearch/client/RestClientSingleHostIntegTests.java

+27-11
Original file line numberDiff line numberDiff line change
@@ -256,35 +256,51 @@ public void testGetWithBody() throws IOException {
256256

257257
public void testEncodeParams() throws IOException {
258258
{
259-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "this/is/the/routing"));
259+
Request request = new Request("PUT", "/200");
260+
request.addParameter("routing", "this/is/the/routing");
261+
Response response = restClient.performRequest(request);
260262
assertEquals(pathPrefix + "/200?routing=this%2Fis%2Fthe%2Frouting", response.getRequestLine().getUri());
261263
}
262264
{
263-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "this|is|the|routing"));
265+
Request request = new Request("PUT", "/200");
266+
request.addParameter("routing", "this|is|the|routing");
267+
Response response = restClient.performRequest(request);
264268
assertEquals(pathPrefix + "/200?routing=this%7Cis%7Cthe%7Crouting", response.getRequestLine().getUri());
265269
}
266270
{
267-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "routing#1"));
271+
Request request = new Request("PUT", "/200");
272+
request.addParameter("routing", "routing#1");
273+
Response response = restClient.performRequest(request);
268274
assertEquals(pathPrefix + "/200?routing=routing%231", response.getRequestLine().getUri());
269275
}
270276
{
271-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "中文"));
277+
Request request = new Request("PUT", "/200");
278+
request.addParameter("routing", "中文");
279+
Response response = restClient.performRequest(request);
272280
assertEquals(pathPrefix + "/200?routing=%E4%B8%AD%E6%96%87", response.getRequestLine().getUri());
273281
}
274282
{
275-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo bar"));
283+
Request request = new Request("PUT", "/200");
284+
request.addParameter("routing", "foo bar");
285+
Response response = restClient.performRequest(request);
276286
assertEquals(pathPrefix + "/200?routing=foo+bar", response.getRequestLine().getUri());
277287
}
278288
{
279-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo+bar"));
289+
Request request = new Request("PUT", "/200");
290+
request.addParameter("routing", "foo+bar");
291+
Response response = restClient.performRequest(request);
280292
assertEquals(pathPrefix + "/200?routing=foo%2Bbar", response.getRequestLine().getUri());
281293
}
282294
{
283-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo/bar"));
295+
Request request = new Request("PUT", "/200");
296+
request.addParameter("routing", "foo/bar");
297+
Response response = restClient.performRequest(request);
284298
assertEquals(pathPrefix + "/200?routing=foo%2Fbar", response.getRequestLine().getUri());
285299
}
286300
{
287-
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo^bar"));
301+
Request request = new Request("PUT", "/200");
302+
request.addParameter("routing", "foo^bar");
303+
Response response = restClient.performRequest(request);
288304
assertEquals(pathPrefix + "/200?routing=foo%5Ebar", response.getRequestLine().getUri());
289305
}
290306
}
@@ -341,14 +357,14 @@ public void testAuthCredentialsAreNotClearedOnAuthChallenge() throws IOException
341357
public void testUrlWithoutLeadingSlash() throws Exception {
342358
if (pathPrefix.length() == 0) {
343359
try {
344-
restClient.performRequest("GET", "200");
360+
restClient.performRequest(new Request("GET", "200"));
345361
fail("request should have failed");
346362
} catch (ResponseException e) {
347363
assertEquals(404, e.getResponse().getStatusLine().getStatusCode());
348364
}
349365
} else {
350366
{
351-
Response response = restClient.performRequest("GET", "200");
367+
Response response = restClient.performRequest(new Request("GET", "200"));
352368
//a trailing slash gets automatically added if a pathPrefix is configured
353369
assertEquals(200, response.getStatusLine().getStatusCode());
354370
}
@@ -357,7 +373,7 @@ public void testUrlWithoutLeadingSlash() throws Exception {
357373
try (RestClient restClient = RestClient.builder(
358374
new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()))
359375
.setPathPrefix(pathPrefix.substring(1)).build()) {
360-
Response response = restClient.performRequest("GET", "200");
376+
Response response = restClient.performRequest(new Request("GET", "200"));
361377
//a trailing slash gets automatically added if a pathPrefix is configured
362378
assertEquals(200, response.getStatusLine().getStatusCode());
363379
}

client/rest/src/test/java/org/elasticsearch/client/documentation/RestClientDocumentation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void onFailure(Exception exception) {
267267
}
268268
{
269269
//tag::rest-client-response2
270-
Response response = restClient.performRequest("GET", "/");
270+
Response response = restClient.performRequest(new Request("GET", "/"));
271271
RequestLine requestLine = response.getRequestLine(); // <1>
272272
HttpHost host = response.getHost(); // <2>
273273
int statusCode = response.getStatusLine().getStatusCode(); // <3>

0 commit comments

Comments
 (0)