Add response handler (#25)

* Add response handler

* fix handler

* fix tests
This commit is contained in:
malkoas 2022-11-03 10:41:42 +03:00 committed by GitHub
parent 849c387f5f
commit 2951f75002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 23 deletions

View File

@ -1,16 +1,12 @@
package dev.vality.wachter.client;
import dev.vality.woody.api.flow.error.WUnavailableResultException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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;
@ -22,6 +18,7 @@ import java.util.Enumeration;
public class WachterClient {
private final HttpClient httpclient;
private final WachterResponseHandler responseHandler;
@SneakyThrows
public byte[] send(HttpServletRequest request, byte[] contentData, String url) {
@ -29,20 +26,9 @@ public class WachterClient {
setHeader(request, httppost);
httppost.setEntity(new ByteArrayEntity(contentData));
log.info("Send request to url {}", url);
HttpResponse response = httpclient.execute(httppost);
var response = httpclient.execute(httppost, responseHandler);
log.info("Get response {}", response);
HttpEntity httpEntity = response.getEntity();
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));
}
return response;
}
private void setHeader(HttpServletRequest request, HttpPost httppost) {

View File

@ -0,0 +1,33 @@
package dev.vality.wachter.client;
import dev.vality.woody.api.flow.error.WUnavailableResultException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Slf4j
@Component
public class WachterResponseHandler implements ResponseHandler<byte[]> {
@Override
public byte[] handleResponse(HttpResponse httpResponse) throws IOException {
int statusCode = httpResponse.getStatusLine().getStatusCode();
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
if (httpStatus.is2xxSuccessful()) {
HttpEntity httpEntity = httpResponse.getEntity();
return EntityUtils.toByteArray(httpEntity);
} else if (httpStatus.is5xxServerError()) {
throw new WUnavailableResultException(String.format("Received 5xx error code: %s (response: %s)",
httpStatus, httpResponse));
} else {
throw new RuntimeException(String.format("Wrong status was received: %s (response: %s)",
httpStatus, httpResponse));
}
}
}

View File

@ -1,5 +1,6 @@
package dev.vality.wachter.controller;
import dev.vality.wachter.client.WachterResponseHandler;
import dev.vality.wachter.config.AbstractKeycloakOpenIdAsWiremockConfig;
import dev.vality.wachter.testutil.TMessageUtil;
import lombok.SneakyThrows;
@ -36,6 +37,9 @@ class WachterControllerDisabledAuthTest extends AbstractKeycloakOpenIdAsWiremock
@MockBean
private HttpResponse httpResponse;
@Autowired
private WachterResponseHandler responseHandler;
@Autowired
private MockMvc mvc;
@ -64,7 +68,7 @@ class WachterControllerDisabledAuthTest extends AbstractKeycloakOpenIdAsWiremock
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);
when(httpClient.execute(any(), eq(responseHandler))).thenReturn(new byte[0]);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithoutRoles())
.header("Service", "messages")
@ -73,7 +77,7 @@ class WachterControllerDisabledAuthTest extends AbstractKeycloakOpenIdAsWiremock
.content(TMessageUtil.createTMessage(protocolFactory)))
.andDo(print())
.andExpect(status().is2xxSuccessful());
verify(httpClient, times(1)).execute(any());
verify(httpClient, times(1)).execute(any(), eq(responseHandler));
}
}

View File

@ -1,5 +1,6 @@
package dev.vality.wachter.controller;
import dev.vality.wachter.client.WachterResponseHandler;
import dev.vality.wachter.config.AbstractKeycloakOpenIdAsWiremockConfig;
import dev.vality.wachter.testutil.TMessageUtil;
import lombok.SneakyThrows;
@ -34,6 +35,9 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
@MockBean
private HttpResponse httpResponse;
@Autowired
private WachterResponseHandler responseHandler;
@Autowired
private MockMvc mvc;
@ -62,7 +66,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
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);
when(httpClient.execute(any(), eq(responseHandler))).thenReturn(new byte[0]);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithRoles())
.header("Service", "Domain")
@ -71,7 +75,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
.content(TMessageUtil.createTMessage(protocolFactory)))
.andDo(print())
.andExpect(status().is2xxSuccessful());
verify(httpClient, times(1)).execute(any());
verify(httpClient, times(1)).execute(any(), eq(responseHandler));
}
@Test
@ -79,7 +83,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
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);
when(httpClient.execute(any(), eq(responseHandler))).thenReturn(new byte[0]);
mvc.perform(post("/wachter")
.header("Authorization", "Bearer " + generateSimpleJwtWithRoles())
.header("Service", "messages")
@ -88,7 +92,7 @@ class WachterControllerTest extends AbstractKeycloakOpenIdAsWiremockConfig {
.content(TMessageUtil.createTMessage(protocolFactory)))
.andDo(print())
.andExpect(status().is2xxSuccessful());
verify(httpClient, times(1)).execute(any());
verify(httpClient, times(1)).execute(any(), eq(responseHandler));
}
}