use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class RequestPolicyChainTest method doNext_streamablePolicies_streaming.
@Test
public void doNext_streamablePolicies_streaming() throws Exception {
StreamablePolicy policy4 = spy(new StreamablePolicy());
StreamablePolicy policy5 = spy(new StreamablePolicy());
ExecutionContext executionContext = mock(ExecutionContext.class);
ReadWriteStream streamPolicy4 = spy(new BufferedReadWriteStream());
when(policy4.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy4);
ReadWriteStream streamPolicy5 = spy(new BufferedReadWriteStream());
when(policy5.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy5);
InOrder inOrder = inOrder(streamPolicy4, streamPolicy5);
PolicyChain chain = RequestPolicyChain.create(Arrays.asList(policy4, policy5), executionContext);
chain.setResultHandler(result -> {
});
chain.bodyHandler(mock(Handler.class));
chain.endHandler(mock(Handler.class));
chain.doNext(null, null);
chain.write(Buffer.buffer("TEST"));
chain.write(Buffer.buffer("TEST"));
chain.end();
inOrder.verify(streamPolicy4, atLeastOnce()).bodyHandler(any(Handler.class));
inOrder.verify(streamPolicy4, atLeastOnce()).endHandler(any(Handler.class));
inOrder.verify(streamPolicy5, atLeastOnce()).bodyHandler(any(Handler.class));
inOrder.verify(streamPolicy5, atLeastOnce()).endHandler(any(Handler.class));
verify(policy4, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class RequestPolicyChainTest method doNext_streamablePolicies.
@Test
public void doNext_streamablePolicies() throws Exception {
StreamablePolicy policy4 = spy(new StreamablePolicy());
StreamablePolicy policy5 = spy(new StreamablePolicy());
ExecutionContext executionContext = mock(ExecutionContext.class);
ReadWriteStream streamPolicy4 = spy(new BufferedReadWriteStream());
when(policy4.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy4);
ReadWriteStream streamPolicy5 = spy(new BufferedReadWriteStream());
when(policy5.onRequestContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(streamPolicy5);
InOrder inOrder = inOrder(streamPolicy4, streamPolicy5);
PolicyChain chain = RequestPolicyChain.create(Arrays.asList(policy4, policy5), executionContext);
chain.setResultHandler(result -> {
});
chain.doNext(null, null);
inOrder.verify(streamPolicy4, atLeastOnce()).bodyHandler(any(Handler.class));
inOrder.verify(streamPolicy4, atLeastOnce()).endHandler(any(Handler.class));
inOrder.verify(streamPolicy5, atLeastOnce()).bodyHandler(any(Handler.class));
inOrder.verify(streamPolicy5, atLeastOnce()).endHandler(any(Handler.class));
verify(policy4, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class ResponsePolicyChainTest method doNext_multiplePolicy_throwError.
@Test
public void doNext_multiplePolicy_throwError() throws Exception {
ExecutionContext executionContext = mock(ExecutionContext.class);
Request request = mock(Request.class);
Metrics metrics = Metrics.on(System.currentTimeMillis()).build();
when(request.metrics()).thenReturn(metrics);
PolicyChain chain = ResponsePolicyChain.create(Arrays.asList(policy2, policy3), executionContext);
chain.setResultHandler(result -> {
});
chain.doNext(request, null);
verify(request, atLeastOnce()).metrics();
verify(policy3, atLeastOnce()).onResponse(request, null, chain, executionContext);
verify(policy2, never()).onResponse(request, null, chain, executionContext);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class ApiKeyAuthenticationHandlerTest method shouldReturnPolicies.
@Test
public void shouldReturnPolicies() {
ExecutionContext executionContext = mock(ExecutionContext.class);
List<AuthenticationPolicy> apikeyProviderPolicies = authenticationHandler.handle(executionContext);
Assert.assertEquals(1, apikeyProviderPolicies.size());
PluginAuthenticationPolicy policy = (PluginAuthenticationPolicy) apikeyProviderPolicies.iterator().next();
Assert.assertEquals(policy.name(), ApiKeyAuthenticationHandler.API_KEY_POLICY);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class DefaultInvoker method invoke.
@Override
public Request invoke(ExecutionContext executionContext, Request serverRequest, ReadStream<Buffer> stream, Handler<ProxyConnection> connectionHandler) {
TargetEndpoint targetEndpoint = selectEndpoint(serverRequest, executionContext);
if (!targetEndpoint.isReachable()) {
DirectProxyConnection statusOnlyConnection = new DirectProxyConnection(HttpStatusCode.SERVICE_UNAVAILABLE_503);
connectionHandler.handle(statusOnlyConnection);
statusOnlyConnection.sendResponse();
} else {
// Remove duplicate slash
String uri = DUPLICATE_SLASH_REMOVER.matcher(targetEndpoint.uri).replaceAll(URI_PATH_SEPARATOR);
URI requestUri = null;
try {
requestUri = encodeQueryParameters(serverRequest, uri);
} catch (Exception ex) {
serverRequest.metrics().setMessage(getStackTraceAsString(ex));
// Request URI is not correct nor correctly encoded, returning a bad request
DirectProxyConnection statusOnlyConnection = new DirectProxyConnection(HttpStatusCode.BAD_REQUEST_400);
connectionHandler.handle(statusOnlyConnection);
statusOnlyConnection.sendResponse();
}
if (requestUri != null) {
uri = requestUri.toString();
// Add the endpoint reference in metrics to know which endpoint has been invoked while serving the request
serverRequest.metrics().setEndpoint(uri);
final HttpMethod httpMethod = extractHttpMethod(executionContext, serverRequest);
ProxyRequest proxyRequest = ProxyRequestBuilder.from(serverRequest).uri(requestUri).method(httpMethod).headers(setProxyHeaders(serverRequest.headers(), requestUri, targetEndpoint.endpoint)).build();
ProxyConnection proxyConnection = targetEndpoint.endpoint.connector().request(proxyRequest);
// Enable logging at proxy level
if (api.getProxy().getLoggingMode().isProxyMode()) {
proxyConnection = new LoggableProxyConnection(proxyConnection, proxyRequest);
}
connectionHandler.handle(proxyConnection);
// Plug underlying stream to connection stream
ProxyConnection finalProxyConnection = proxyConnection;
stream.bodyHandler(buffer -> {
finalProxyConnection.write(buffer);
if (finalProxyConnection.writeQueueFull()) {
serverRequest.pause();
finalProxyConnection.drainHandler(aVoid -> serverRequest.resume());
}
}).endHandler(aVoid -> finalProxyConnection.end());
}
}
// Resume the incoming request to handle content and end
serverRequest.resume();
return serverRequest;
}
Aggregations