use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class TransferQuotaPolicyTest method testDownloadLimit.
@Test
@Configuration("{" + " \"limit\" : 1000," + " \"direction\" : \"download\"," + " \"granularity\" : \"Api\"," + " \"period\" : \"Day\"," + " \"headerRemaining\" : \"X-DBytes-Remaining\"," + " \"headerLimit\" : \"X-DBytes-Limit\"," + " \"headerReset\" : \"X-DBytes-Reset\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimit() throws Throwable {
PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
request.header("X-Payload-Size", "389");
PolicyTestResponse response = send(request);
Assert.assertNotNull(response.body());
Assert.assertEquals("1000", response.header("X-DBytes-Remaining"));
Assert.assertEquals("1000", response.header("X-DBytes-Limit"));
send(request);
send(request);
// Now if we try it one more time, we'll get a failure!
try {
send(request);
Assert.fail("Expected a policy failure!");
} catch (PolicyFailureError e) {
PolicyFailure failure = e.getFailure();
Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
Assert.assertEquals(429, failure.getResponseCode());
}
}
use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class AuthorizationPolicyTest method doTest.
private void doTest(String json, HashSet<String> userRoles, String verb, String path, boolean shouldSucceed) {
AuthorizationPolicy policy = new AuthorizationPolicy();
Object config = policy.parseConfiguration(json);
ApiRequest request = new ApiRequest();
request.setType(verb);
request.setDestination(path);
IPolicyContext context = Mockito.mock(IPolicyContext.class);
IPolicyChain<ApiRequest> chain = Mockito.mock(IPolicyChain.class);
Mockito.when(context.getAttribute(AuthorizationPolicy.AUTHENTICATED_USER_ROLES, (HashSet<String>) null)).thenReturn(userRoles);
final PolicyFailure failure = new PolicyFailure();
Mockito.when(context.getComponent(IPolicyFailureFactoryComponent.class)).thenReturn(new IPolicyFailureFactoryComponent() {
@Override
public PolicyFailure createFailure(PolicyFailureType type, int failureCode, String message) {
failure.setFailureCode(failureCode);
failure.setType(type);
failure.setMessage(message);
return failure;
}
});
// Success
policy.apply(request, context, config, chain);
if (shouldSucceed) {
Mockito.verify(chain).doApply(request);
} else {
Mockito.verify(chain).doFailure(failure);
}
}
use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class BeanSharedStatePolicy method apply.
/**
* @see io.apiman.gateway.engine.policy.IPolicy#apply(io.apiman.gateway.engine.beans.ApiResponse, io.apiman.gateway.engine.policy.IPolicyContext, java.lang.Object, io.apiman.gateway.engine.policy.IPolicyChain)
*/
@Override
public void apply(final ApiResponse response, IPolicyContext context, Object config, final IPolicyChain<ApiResponse> chain) {
final ISharedStateComponent sharedState = context.getComponent(ISharedStateComponent.class);
final String namespace = "urn:" + getClass().getName();
final String propName = "bean-property";
final DataBean notfound = new DataBean();
sharedState.getProperty(namespace, propName, notfound, new IAsyncResultHandler<DataBean>() {
@Override
public void handle(IAsyncResult<DataBean> result) {
DataBean value = result.getResult();
if (value == notfound) {
PolicyFailure failure = new PolicyFailure(PolicyFailureType.NotFound, 0, "DataBean not found.");
chain.doFailure(failure);
} else {
response.getHeaders().put("X-Property-1", value.getProperty1());
response.getHeaders().put("X-Property-2", String.valueOf(value.isProperty2()));
response.getHeaders().put("X-Property-3", String.valueOf(value.getProperty3()));
chain.doApply(response);
}
}
});
}
use of io.apiman.gateway.engine.beans.PolicyFailure 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);
}
use of io.apiman.gateway.engine.beans.PolicyFailure in project apiman by apiman.
the class TransferQuotaPolicyTest method testDownloadLimitEmptyHeaderConfig.
@Test
@Configuration("{" + " \"limit\" : 1000," + " \"direction\" : \"download\"," + " \"granularity\" : \"Api\"," + " \"period\" : \"Day\"," + " \"headerRemaining\" : \"\"," + " \"headerLimit\" : \"\"," + " \"headerReset\" : \"\"" + "}")
@BackEndApi(DownloadTestBackEndApi.class)
public void testDownloadLimitEmptyHeaderConfig() throws Throwable {
PolicyTestRequest request = PolicyTestRequest.build(PolicyTestRequestType.GET, "/some/resource");
request.header("X-Payload-Size", "389");
PolicyTestResponse response = send(request);
Assert.assertNotNull(response.body());
Assert.assertEquals("1000", response.header("X-TransferQuota-Remaining"));
Assert.assertEquals("1000", response.header("X-TransferQuota-Limit"));
send(request);
send(request);
// Now if we try it one more time, we'll get a failure!
try {
send(request);
Assert.fail("Expected a policy failure!");
} catch (PolicyFailureError e) {
PolicyFailure failure = e.getFailure();
Assert.assertEquals(PolicyFailureCodes.BYTE_QUOTA_EXCEEDED, failure.getFailureCode());
Assert.assertEquals("Transfer quota exceeded.", failure.getMessage());
Assert.assertEquals(429, failure.getResponseCode());
}
}
Aggregations