use of io.gravitee.policy.api.PolicyChain in project gravitee-gateway by gravitee-io.
the class AbstractPolicy method onResponse.
public void onResponse(Object... args) throws PolicyException {
ExecutionContext executionContext = getParameterAssignableTo(ExecutionContext.class, args);
PolicyChain policyChain = getParameterAssignableTo(PolicyChain.class, args);
Request request = getParameterAssignableTo(Request.class, args);
Response response = getParameterAssignableTo(Response.class, args);
this.onResponse(request, response, policyChain, executionContext);
}
use of io.gravitee.policy.api.PolicyChain in project gravitee-gateway by gravitee-io.
the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_noClient.
@Test
public void shouldReturnUnauthorized_noClient() throws PolicyException, TechnicalException {
CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
Request request = mock(Request.class);
Response response = mock(Response.class);
when(response.headers()).thenReturn(mock(HttpHeaders.class));
PolicyChain policyChain = mock(PolicyChain.class);
ExecutionContext executionContext = mock(ExecutionContext.class);
SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
Subscription subscription = mock(Subscription.class);
when(subscription.getClientId()).thenReturn("my-bad-client-id");
when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
policy.onRequest(request, response, policyChain, executionContext);
verify(policyChain, times(1)).failWith(argThat(statusCode(HttpStatusCode.UNAUTHORIZED_401)));
}
use of io.gravitee.policy.api.PolicyChain 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.policy.api.PolicyChain 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.policy.api.PolicyChain in project gravitee-gateway by gravitee-io.
the class AbstractPolicy method onRequest.
public void onRequest(Object... args) throws PolicyException {
ExecutionContext executionContext = getParameterAssignableTo(ExecutionContext.class, args);
PolicyChain policyChain = getParameterAssignableTo(PolicyChain.class, args);
Request request = getParameterAssignableTo(Request.class, args);
Response response = getParameterAssignableTo(Response.class, args);
this.onRequest(request, response, policyChain, executionContext);
}
Aggregations