use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_onException.
@Test
public void shouldReturnUnauthorized_onException() throws PolicyException, TechnicalException {
CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
Request request = mock(Request.class);
Response response = mock(Response.class);
PolicyChain policyChain = mock(PolicyChain.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenThrow(TechnicalException.class);
policy.onRequest(request, response, policyChain, executionContext);
verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class JWTAuthenticationHandlerTest method shouldReturnPolicies.
@Test
public void shouldReturnPolicies() {
ExecutionContext executionContext = mock(ExecutionContext.class);
List<AuthenticationPolicy> jwtProviderPolicies = authenticationHandler.handle(executionContext);
Assert.assertEquals(2, jwtProviderPolicies.size());
Iterator<AuthenticationPolicy> policyIte = jwtProviderPolicies.iterator();
PluginAuthenticationPolicy policy = (PluginAuthenticationPolicy) policyIte.next();
Assert.assertEquals(JWTAuthenticationHandler.AUTHENTICATION_HANDLER_NAME, policy.name());
HookAuthenticationPolicy policy2 = (HookAuthenticationPolicy) policyIte.next();
Assert.assertEquals(CheckSubscriptionPolicy.class, policy2.clazz());
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class CheckSubscriptionPolicyTest method shouldContinue.
@Test
public void shouldContinue() throws PolicyException, TechnicalException {
CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
Request request = mock(Request.class);
Response response = mock(Response.class);
PolicyChain policyChain = mock(PolicyChain.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
Subscription subscription = mock(Subscription.class);
when(subscription.getClientId()).thenReturn("my-client-id");
when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
policy.onRequest(request, response, policyChain, executionContext);
verify(policyChain, times(1)).doNext(request, response);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class RequestPolicyChainTest 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 = RequestPolicyChain.create(policies3(), executionContext);
chain.setResultHandler(result -> {
});
chain.doNext(request, null);
verify(request, atLeastOnce()).metrics();
verify(policy3, atLeastOnce()).onRequest(request, null, chain, executionContext);
verify(policy2, never()).onRequest(request, null, chain);
}
use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.
the class RequestPolicyChainTest method doNext_multiplePolicy.
@Test
public void doNext_multiplePolicy() throws Exception {
ExecutionContext executionContext = mock(ExecutionContext.class);
PolicyChain chain = RequestPolicyChain.create(policies2(), executionContext);
chain.setResultHandler(result -> {
});
chain.doNext(null, null);
verify(policy, atLeastOnce()).onRequest(null, null, chain, executionContext);
verify(policy2, atLeastOnce()).onRequest(null, null, chain, executionContext);
}
Aggregations