Skip to content

Commit

Permalink
fix: code issue
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyuT committed Oct 30, 2023
1 parent 945005a commit 24a7dbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
Expand Down Expand Up @@ -316,8 +315,7 @@ public RestResult post(String path, Object object, RestHeaders headers,
Request.Builder requestBuilder = getRequestBuilder(path, null, headers, params);
requestBuilder.post(buildRequestBody(object, headers));

try (Response response = this.request(
() -> client.newCall(requestBuilder.build()).execute())) {
try (Response response = request(requestBuilder)) {
checkStatus(response, 200, 201, 202);
return new RestResult(response);
}
Expand Down Expand Up @@ -346,8 +344,7 @@ public RestResult put(String path, String id, Object object,
Request.Builder requestBuilder = getRequestBuilder(path, id, headers, params);
requestBuilder.put(buildRequestBody(object, headers));

try (Response response = this.request(
() -> client.newCall(requestBuilder.build()).execute())) {
try (Response response = request(requestBuilder)) {
checkStatus(response, 200, 202);
return new RestResult(response);
}
Expand All @@ -372,8 +369,7 @@ public RestResult get(String path, String id) {
private RestResult get(String path, String id, Map<String, Object> params) {
Request.Builder requestBuilder = getRequestBuilder(path, id, null, params);

try (Response response = this.request(
() -> client.newCall(requestBuilder.build()).execute())) {
try (Response response = request(requestBuilder)) {
checkStatus(response, 200);
return new RestResult(response);
}
Expand All @@ -395,21 +391,17 @@ private RestResult delete(String path, String id,
Request.Builder requestBuilder = getRequestBuilder(path, id, null, params);
requestBuilder.delete();

try (Response response = this.request(
() -> client.newCall(requestBuilder.build()).execute())) {
try (Response response = request(requestBuilder)) {
checkStatus(response, 204, 202);
return new RestResult(response);
}
}

protected abstract void checkStatus(Response response, int... statuses);

protected Response request(Callable<Response> method) {
try {
return method.call();
} catch (Exception e) {
throw new ClientException("Failed to do request", e);
}
@SneakyThrows
protected Response request(Request.Builder requestBuilder) {
return client.newCall(requestBuilder.build()).execute();
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.function.BiFunction;

import javax.net.ssl.SSLContext;
Expand Down Expand Up @@ -342,86 +341,86 @@ public void testRequest() {
client.setAuthContext("token1");
result = client.delete("test", ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(RequestHeaders.AUTHORIZATION,"token1");
Mockito.verify(requestBuilder).addHeader(RequestHeaders.AUTHORIZATION, "token1");

client.resetAuthContext();

client.setAuthContext("token2");
result = client.delete("test", "id");
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token2");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token2");
client.resetAuthContext();

// Test get
client.setAuthContext("token3");
result = client.get("test");
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token3");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token3");
client.resetAuthContext();

client.setAuthContext("token4");
result = client.get("test", ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token4");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token4");
client.resetAuthContext();

client.setAuthContext("token5");
result = client.get("test", "id");
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token5");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token5");
client.resetAuthContext();

// Test put
client.setAuthContext("token6");
// result = client.post("test", new Object()); //why use new Object() as args here?
result = client.post("test", null);
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token6");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token6");
client.resetAuthContext();

client.setAuthContext("token7");
result = client.post("test", null, new RestHeaders());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token7");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token7");
client.resetAuthContext();

client.setAuthContext("token8");
result = client.post("test", null, ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token8");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token8");
client.resetAuthContext();

client.setAuthContext("token9");
result = client.post("test", null, new RestHeaders(),
ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token9");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token9");
client.resetAuthContext();

// Test post
client.setAuthContext("token10");
result = client.post("test", null);
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token10");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token10");
client.resetAuthContext();

client.setAuthContext("token11");
result = client.post("test", null, new RestHeaders());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token11");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token11");
client.resetAuthContext();

client.setAuthContext("token12");
result = client.post("test", null, ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token12");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token12");
client.resetAuthContext();

client.setAuthContext("token13");
result = client.post("test", null, new RestHeaders(),
ImmutableMap.of());
Assert.assertEquals(200, result.status());
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION,"token13");
Mockito.verify(requestBuilder).addHeader(HttpHeaders.AUTHORIZATION, "token13");
client.resetAuthContext();
}

Expand Down Expand Up @@ -517,7 +516,7 @@ public RestClientImpl(String url, int timeout, int status,

@SneakyThrows
@Override
protected Response request(Callable<Response> method) {
protected Response request(Request.Builder requestBuilder) {
Response response = Mockito.mock(Response.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(response.code()).thenReturn(this.status);
Mockito.when(response.headers()).thenReturn(this.headers.toOkHttpHeader());
Expand Down

0 comments on commit 24a7dbf

Please sign in to comment.