Fix client

This commit is contained in:
malkoas 2022-10-25 16:07:32 +03:00
parent 0d2345eed7
commit 849c387f5f
3 changed files with 21 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package dev.vality.wachter.client;
import dev.vality.woody.api.flow.error.WUnavailableResultException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
@ -9,6 +10,7 @@ import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
@ -28,9 +30,19 @@ public class WachterClient {
httppost.setEntity(new ByteArrayEntity(contentData));
log.info("Send request to url {}", url);
HttpResponse response = httpclient.execute(httppost);
log.info("Get response {}", response);
HttpEntity httpEntity = response.getEntity();
log.info("Get response with entity: {} and statusLine {}", httpEntity, response.getStatusLine());
return EntityUtils.toByteArray(httpEntity);
int statusCode = response.getStatusLine().getStatusCode();
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
if (httpStatus.is2xxSuccessful()) {
return EntityUtils.toByteArray(httpEntity);
} else if (httpStatus.is5xxServerError()) {
throw new WUnavailableResultException(String.format("Received 5xx error code: %s (response: %s)",
httpStatus, response));
} else {
throw new RuntimeException(String.format("Wrong status was received: %s (response: %s)",
httpStatus, response));
}
}
private void setHeader(HttpServletRequest request, HttpPost httppost) {

View File

@ -4,8 +4,10 @@ import dev.vality.wachter.config.AbstractKeycloakOpenIdAsWiremockConfig;
import dev.vality.wachter.testutil.TMessageUtil;
import lombok.SneakyThrows;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicStatusLine;
import org.apache.thrift.protocol.TProtocolFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -61,6 +63,7 @@ class WachterControllerDisabledAuthTest extends AbstractKeycloakOpenIdAsWiremock
@SneakyThrows
void requestSuccess() {
when(httpResponse.getEntity()).thenReturn(new StringEntity(""));
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("", 0, 0), 200, ""));
when(httpClient.execute(any())).thenReturn(httpResponse);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithoutRoles())

View File

@ -4,8 +4,10 @@ import dev.vality.wachter.config.AbstractKeycloakOpenIdAsWiremockConfig;
import dev.vality.wachter.testutil.TMessageUtil;
import lombok.SneakyThrows;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicStatusLine;
import org.apache.thrift.protocol.TProtocolFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@ -59,6 +61,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
@SneakyThrows
void requestSuccessWithServiceRole() {
when(httpResponse.getEntity()).thenReturn(new StringEntity(""));
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("", 0, 0), 200, ""));
when(httpClient.execute(any())).thenReturn(httpResponse);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithRoles())
@ -75,6 +78,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
@SneakyThrows
void requestSuccessWithMethodRole() {
when(httpResponse.getEntity()).thenReturn(new StringEntity(""));
when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("", 0, 0), 200, ""));
when(httpClient.execute(any())).thenReturn(httpResponse);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithRoles())