use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.
the class ApiRequestExecutorImpl method createRequestChain.
/**
* Creates the chain used to apply policies in order to the api request.
*/
private Chain<ApiRequest> createRequestChain(IAsyncHandler<ApiRequest> requestHandler) {
RequestChain chain = new RequestChain(policyImpls, context);
chain.headHandler(requestHandler);
chain.policyFailureHandler(failure -> {
// It will likely not have been initialised, so create one.
if (responseChain == null) {
// Its response will not be used as we take the failure path only, so we just use an empty lambda.
responseChain = createResponseChain((ignored) -> {
});
}
responseChain.doFailure(failure);
});
chain.policyErrorHandler(policyErrorHandler);
return chain;
}
use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.
the class DataPolicyChainTest method shouldEnsureNonNullRequestReceivedByHandlers.
@Test
public void shouldEnsureNonNullRequestReceivedByHandlers() {
policies.add(pwcOne);
requestChain = new RequestChain(policies, mockContext);
requestChain.bodyHandler(mockBodyHandler);
requestChain.endHandler(mockEndHandler);
requestChain.doApply(mockRequest);
requestChain.write(mockBuffer);
requestChain.end();
verify(mockBodyHandler, times(1)).handle(mockBuffer);
verify(mockEndHandler, times(1)).handle((Void) null);
// At this point we must ensure that the request and responses are NOT null.
verify(policyOne).getRequestDataHandler(mockRequest, mockContext, configuration);
}
use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.
the class PolicyChainTest method shouldPreserveBufferOrder.
@Test
public void shouldPreserveBufferOrder() {
policies.add(pwcOne);
requestChain = new RequestChain(policies, mockContext);
requestChain.bodyHandler(mockBodyHandler);
requestChain.endHandler(mockEndHandler);
requestChain.doApply(mockRequest);
IApimanBuffer buffer1 = mock(IApimanBuffer.class);
IApimanBuffer buffer2 = mock(IApimanBuffer.class);
IApimanBuffer buffer3 = mock(IApimanBuffer.class);
requestChain.write(buffer1);
requestChain.write(buffer2);
requestChain.write(buffer3);
requestChain.end();
InOrder order = inOrder(mockBodyHandler, mockEndHandler);
order.verify(mockBodyHandler).handle(buffer1);
order.verify(mockBodyHandler).handle(buffer2);
order.verify(mockBodyHandler).handle(buffer3);
order.verify(mockEndHandler).handle((Void) null);
}
use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.
the class PolicyChainTest method shouldEndChainImmediatelyWhenSkipCalled.
@Test
public void shouldEndChainImmediatelyWhenSkipCalled() {
IPolicy skipPolicy = spy(new IPolicy() {
@Override
public Object parseConfiguration(String jsonConfiguration) throws ConfigurationParseException {
return null;
}
@Override
public void apply(ApiRequest request, IPolicyContext context, Object config, IPolicyChain<ApiRequest> chain) {
chain.doSkip(request);
}
@Override
public void apply(ApiResponse response, IPolicyContext context, Object config, IPolicyChain<ApiResponse> chain) {
chain.doSkip(response);
}
});
PolicyWithConfiguration pwcSkip = new PolicyWithConfiguration(skipPolicy, null);
policies.add(pwcSkip);
policies.add(pwcTwo);
requestChain = new RequestChain(policies, mockContext);
requestChain.bodyHandler(mockBodyHandler);
requestChain.endHandler(mockEndHandler);
requestChain.doApply(mockRequest);
requestChain.end();
verify(mockEndHandler, times(1)).handle((Void) null);
// Should only be called once, as the second is skipped
verify(skipPolicy, times(1)).apply(mockRequest, mockContext, null, requestChain);
verify(policyOne, never()).apply(mockRequest, mockContext, null, requestChain);
}
use of io.apiman.gateway.engine.policy.RequestChain in project apiman by apiman.
the class PolicyChainTest method shouldCallFailureHandlerOnDoFail.
@Test
public void shouldCallFailureHandlerOnDoFail() {
policies.add(pwcOne);
policies.add(pwcTwo);
requestChain = new RequestChain(policies, mockContext);
IAsyncHandler<PolicyFailure> mPolicyFailureHandler = mock(IAsyncHandler.class);
PolicyFailure mPolicyFailure = mock(PolicyFailure.class);
requestChain.policyFailureHandler(mPolicyFailureHandler);
requestChain.bodyHandler(mockBodyHandler);
requestChain.endHandler(mockEndHandler);
requestChain.doApply(mockRequest);
requestChain.doFailure(mPolicyFailure);
verify(mPolicyFailureHandler).handle(mPolicyFailure);
}
Aggregations