Skip to content

Commit

Permalink
Switch low level rest tests to new style Requests (elastic#31938)
Browse files Browse the repository at this point in the history
In elastic#29623 we added `Request` object flavored requests to the low level
REST client and in elastic#30315 we deprecated the old `performRequest`s. This
changes all calls in the `client/rest` project to use the new versions.
  • Loading branch information
nik9000 authored Jul 11, 2018
1 parent aa6a1c5 commit eda6d18
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {
try {
try (RestClient client = buildRestClient()) {
try {
client.performRequest("GET", "/");
client.performRequest(new Request("GET", "/"));
fail("connection should have been rejected due to SSL handshake");
} catch (Exception e) {
assertThat(e.getMessage(), containsString("General SSLEngine problem"));
Expand All @@ -85,7 +85,7 @@ public void testBuilderUsesDefaultSSLContext() throws Exception {

SSLContext.setDefault(getSslContext());
try (RestClient client = buildRestClient()) {
Response response = client.performRequest("GET", "/");
Response response = client.performRequest(new Request("GET", "/"));
assertEquals(200, response.getStatusLine().getStatusCode());
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,35 +256,51 @@ public void testGetWithBody() throws IOException {

public void testEncodeParams() throws IOException {
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "this/is/the/routing"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "this/is/the/routing");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=this%2Fis%2Fthe%2Frouting", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "this|is|the|routing"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "this|is|the|routing");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=this%7Cis%7Cthe%7Crouting", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "routing#1"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "routing#1");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=routing%231", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "中文"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "中文");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=%E4%B8%AD%E6%96%87", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo bar"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "foo bar");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=foo+bar", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo+bar"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "foo+bar");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=foo%2Bbar", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo/bar"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "foo/bar");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=foo%2Fbar", response.getRequestLine().getUri());
}
{
Response response = restClient.performRequest("PUT", "/200", Collections.singletonMap("routing", "foo^bar"));
Request request = new Request("PUT", "/200");
request.addParameter("routing", "foo^bar");
Response response = restClient.performRequest(request);
assertEquals(pathPrefix + "/200?routing=foo%5Ebar", response.getRequestLine().getUri());
}
}
Expand Down Expand Up @@ -341,14 +357,14 @@ public void testAuthCredentialsAreNotClearedOnAuthChallenge() throws IOException
public void testUrlWithoutLeadingSlash() throws Exception {
if (pathPrefix.length() == 0) {
try {
restClient.performRequest("GET", "200");
restClient.performRequest(new Request("GET", "200"));
fail("request should have failed");
} catch (ResponseException e) {
assertEquals(404, e.getResponse().getStatusLine().getStatusCode());
}
} else {
{
Response response = restClient.performRequest("GET", "200");
Response response = restClient.performRequest(new Request("GET", "200"));
//a trailing slash gets automatically added if a pathPrefix is configured
assertEquals(200, response.getStatusLine().getStatusCode());
}
Expand All @@ -357,7 +373,7 @@ public void testUrlWithoutLeadingSlash() throws Exception {
try (RestClient restClient = RestClient.builder(
new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort()))
.setPathPrefix(pathPrefix.substring(1)).build()) {
Response response = restClient.performRequest("GET", "200");
Response response = restClient.performRequest(new Request("GET", "200"));
//a trailing slash gets automatically added if a pathPrefix is configured
assertEquals(200, response.getStatusLine().getStatusCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void onFailure(Exception exception) {
}
{
//tag::rest-client-response2
Response response = restClient.performRequest("GET", "/");
Response response = restClient.performRequest(new Request("GET", "/"));
RequestLine requestLine = response.getRequestLine(); // <1>
HttpHost host = response.getHost(); // <2>
int statusCode = response.getStatusLine().getStatusCode(); // <3>
Expand Down

0 comments on commit eda6d18

Please sign in to comment.