use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable.
@Test
public void testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
messageContext.setProperty(RESPONSE, "true");
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenBlockingConditionsAreSatisfied.
@Test
public void testMsgThrottleOutWhenBlockingConditionsAreSatisfied() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
throttleDataHolder.addIpBlockingCondition("carbon.super", 1, "{\"fixedIp\":\"127.0.0.1\",\"invert\":false}", APIConstants.BLOCKING_CONDITIONS_IP);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
// Mockito.when(throttleDataHolder.isRequestBlocked(apiContext, authenticationContext
// .getSubscriber() + ":" + authenticationContext.getApplicationName(), authenticationContext
// .getUsername(), "carbon.super" + ":" + "127.0.0.1")).thenReturn(true);
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeIpBlockingCondition("carbon.super", 1);
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenHittingSubscriptionLevelSpike.
@Test
public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
throttleHandler.setSandboxMaxCount("100");
SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
throttleHandler.init(synapseEnvironment);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApiTier(throttlingTier);
authenticationContext.setKeyType("SANDBOX");
authenticationContext.setSpikeArrestLimit(100);
authenticationContext.setStopOnQuotaReach(true);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("$user", "$user");
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testMsgThrottleOutWhenCustomThrottlingLimitExceeded.
@Test
public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
throttleHandler.setProductionMaxCount("100");
SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
throttleHandler.init(synapseEnvironment);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApiTier(throttlingTier);
authenticationContext.setSpikeArrestLimit(0);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("$user", "$user");
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() + 10000);
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeKeyTemplate("testKeyTemplate");
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.gateway.throttling.ThrottleDataHolder in project carbon-apimgt by wso2.
the class ThrottleHandlerTest method testCheckForStaledThrottleData.
@Test
public void testCheckForStaledThrottleData() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator, accessInformation);
throttleHandler.setProductionMaxCount("100");
SynapseEnvironment synapseEnvironment = Mockito.mock(SynapseEnvironment.class);
throttleHandler.init(synapseEnvironment);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
messageContext.setProperty(VERB_INFO_DTO, verbInfoDTO);
((Axis2MessageContext) messageContext).getAxis2MessageContext().getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApiTier(throttlingTier);
authenticationContext.setSpikeArrestLimit(0);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
verbInfo.setConditionGroups(conditionGroupDTOs);
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>();
Mockito.when(accessInformation.isAccessAllowed()).thenReturn(false);
matchingConditions.add(conditionGroupDTO);
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() - 10000);
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
}
Aggregations