use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.
the class HttpClientFacadeTest method whenSupportedStaticPathServiceRequested_expectRequestExecutedAndResponseOKWithBody.
@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenSupportedStaticPathServiceRequested_expectRequestExecutedAndResponseOKWithBody(TestContext context) throws Exception {
Async async = context.async();
// given
final WebClient mockedWebClient = PowerMockito.spy(webClient());
HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
final JsonObject expectedResponse = new JsonObject(FileReader.readText("first-response.json"));
// when
Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", REQUEST_PATH), clientRequest), HttpMethod.POST);
// then
result.doOnSuccess(response -> {
context.assertEquals(HttpResponseStatus.OK.code(), response.getStatusCode());
context.assertEquals(expectedResponse, response.getBody().toJsonObject());
Mockito.verify(mockedWebClient, Mockito.times(1)).request(HttpMethod.POST, PORT, DOMAIN, REQUEST_PATH);
}).subscribe(response -> async.complete(), error -> context.fail(error.getMessage()));
}
use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.
the class HttpClientFacadeTest method whenUnsupportedPathServiceRequested_expectNoServiceRequestAndBadRequest.
@Test
@KnotxConfiguration("knotx-action-adapter-http-test.json")
public void whenUnsupportedPathServiceRequested_expectNoServiceRequestAndBadRequest(TestContext context) throws Exception {
Async async = context.async();
// given
final WebClient mockedWebClient = PowerMockito.spy(webClient());
HttpClientFacade clientFacade = new HttpClientFacade(mockedWebClient, getConfiguration());
// when
Single<ClientResponse> result = clientFacade.process(payloadMessage(new JsonObject().put("path", "/not/supported/path"), clientRequest), HttpMethod.POST);
// then
result.doOnError(error -> {
context.assertEquals(UnsupportedServiceException.class, error.getClass());
Mockito.verify(mockedWebClient, Mockito.times(0)).request(Matchers.any(), Matchers.anyInt(), Matchers.anyString(), Matchers.anyString());
}).subscribe(response -> context.fail("Error should occur!"), error -> async.complete());
}
use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.
the class HttpClientFacade method prepareRequestData.
private Pair<ClientRequest, ServiceMetadata> prepareRequestData(AdapterRequest adapterRequest) {
final Pair<ClientRequest, ServiceMetadata> serviceData;
final JsonObject params = adapterRequest.getParams();
final ClientRequest serviceRequest = buildServiceRequest(adapterRequest.getRequest(), params);
final Optional<ServiceMetadata> serviceMetadata = findServiceMetadata(serviceRequest.getPath());
if (serviceMetadata.isPresent()) {
final ServiceMetadata metadata = serviceMetadata.get();
if (params.containsKey(HEADERS_PROPERTY_KEY)) {
metadata.setAdditionalHeaders(params.getJsonObject(HEADERS_PROPERTY_KEY));
}
if (params.containsKey(QUERY_PARAMS_PROPERTY_KEY)) {
metadata.setQueryParams(params.getJsonObject(QUERY_PARAMS_PROPERTY_KEY));
}
serviceData = Pair.of(serviceRequest, metadata);
} else {
final String error = String.format("No matching service definition for the requested path '%s'", serviceRequest.getPath());
throw new UnsupportedServiceException(error);
}
return serviceData;
}
use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.
the class HttpClientFacade method callService.
private Single<HttpResponse<Buffer>> callService(Pair<ClientRequest, ServiceMetadata> serviceData, HttpMethod method) {
final Single<HttpResponse<Buffer>> httpResponse;
final ClientRequest serviceRequest = serviceData.getLeft();
final ServiceMetadata serviceMetadata = serviceData.getRight();
final HttpRequest<Buffer> request = webClient.request(method, serviceMetadata.getPort(), serviceMetadata.getDomain(), serviceRequest.getPath());
updateRequestQueryParams(request, serviceMetadata);
updateRequestHeaders(request, serviceRequest, serviceMetadata);
overrideRequestHeaders(request, serviceMetadata);
if (!serviceRequest.getFormAttributes().isEmpty()) {
httpResponse = request.rxSendForm(serviceRequest.getFormAttributes());
} else {
httpResponse = request.rxSend();
}
return httpResponse;
}
use of io.knotx.dataobjects.ClientRequest in project knotx by Cognifide.
the class HttpClientFacade method updateRequestHeaders.
private void updateRequestHeaders(HttpRequest<Buffer> request, ClientRequest serviceRequest, ServiceMetadata serviceMetadata) {
MultiMap filteredHeaders = getFilteredHeaders(serviceRequest.getHeaders(), serviceMetadata.getAllowedRequestHeaderPatterns());
filteredHeaders.names().forEach(headerName -> filteredHeaders.getAll(headerName).forEach(value -> request.headers().add(headerName, value)));
if (customRequestHeader.containsKey("name") && customRequestHeader.containsKey("value")) {
request.headers().set(customRequestHeader.getString("name"), customRequestHeader.getString("value"));
}
}
Aggregations