Search in sources :

Example 26 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class ResponsePolicyChainTest method doNext_streamablePolicy.

@Test
public void doNext_streamablePolicy() throws Exception {
    StreamablePolicy policy4 = spy(new StreamablePolicy());
    ExecutionContext executionContext = mock(ExecutionContext.class);
    ReadWriteStream stream = spy(new BufferedReadWriteStream());
    when(policy4.onResponseContent(any(Request.class), any(Response.class), any(io.gravitee.policy.api.PolicyChain.class), eq(executionContext))).thenReturn(stream);
    PolicyChain chain = ResponsePolicyChain.create(Collections.singletonList(policy4), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(null, null);
    verify(stream, atLeastOnce()).bodyHandler(any(Handler.class));
    verify(stream, atLeastOnce()).endHandler(any(Handler.class));
    verify(policy4, atLeastOnce()).onResponse(null, null, chain, executionContext);
}
Also used : ReadWriteStream(io.gravitee.gateway.api.stream.ReadWriteStream) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) BufferedReadWriteStream(io.gravitee.gateway.api.stream.BufferedReadWriteStream) Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) Test(org.junit.Test)

Example 27 with ExecutionContext

use of io.gravitee.gateway.api.ExecutionContext in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldReturnUnauthorized_badClient.

@Test
public void shouldReturnUnauthorized_badClient() 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-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)));
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 28 with ExecutionContext

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);
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Aggregations

ExecutionContext (io.gravitee.gateway.api.ExecutionContext)28 Request (io.gravitee.gateway.api.Request)22 Test (org.junit.Test)21 Response (io.gravitee.gateway.api.Response)19 PolicyChain (io.gravitee.gateway.policy.impl.PolicyChain)12 Handler (io.gravitee.gateway.api.handler.Handler)9 PolicyChain (io.gravitee.policy.api.PolicyChain)9 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 BufferedReadWriteStream (io.gravitee.gateway.api.stream.BufferedReadWriteStream)6 ReadWriteStream (io.gravitee.gateway.api.stream.ReadWriteStream)6 ResponsePolicyChain (io.gravitee.gateway.policy.impl.ResponsePolicyChain)6 RequestPolicyChain (io.gravitee.gateway.policy.impl.RequestPolicyChain)5 Subscription (io.gravitee.repository.management.model.Subscription)5 AuthenticationPolicy (io.gravitee.gateway.security.core.AuthenticationPolicy)4 PluginAuthenticationPolicy (io.gravitee.gateway.security.core.PluginAuthenticationPolicy)4 InOrder (org.mockito.InOrder)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 HttpHeaders (io.gravitee.common.http.HttpHeaders)3 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)3