Search in sources :

Example 21 with Request

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

the class ReactorHandlerResolverTest method test_uniqContextPath.

@Test
public void test_uniqContextPath() {
    Collection<ReactorHandler> handlers = new ArrayList<>(new ArrayList<ReactorHandler>() {

        {
            add(createMockHandler("/teams"));
        }
    });
    when(handlerRegistry.getReactorHandlers()).thenReturn(handlers);
    Request request = Mockito.mock(Request.class);
    when(request.path()).thenReturn("/teams");
    ReactorHandler handler = handlerResolver.resolve(request);
    Assert.assertEquals("/teams/", handler.contextPath());
}
Also used : ArrayList(java.util.ArrayList) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 22 with Request

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

the class TransactionHandlerTest method shouldHaveTransactionId.

@Test
public void shouldHaveTransactionId() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    when(request.id()).thenReturn(UUID.toString(UUID.random()));
    new TransactionHandler(request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(request1.transactionId(), request1.headers().getFirst(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER));
        assertEquals(request1.transactionId(), request1.metrics().getTransactionId());
        assertEquals(request1.transactionId(), response.headers().getFirst(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER));
        lock.countDown();
    }, response).handle(request);
    assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Mock(org.mockito.Mock) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Metrics(io.gravitee.reporter.api.http.Metrics) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) CountDownLatch(java.util.concurrent.CountDownLatch) Response(io.gravitee.gateway.api.Response) UUID(io.gravitee.common.utils.UUID) Request(io.gravitee.gateway.api.Request) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 23 with Request

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

the class TransactionHandlerTest method shouldPropagateSameTransactionIdWithCustomHeader.

@Test
public void shouldPropagateSameTransactionIdWithCustomHeader() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    String transactionId = UUID.toString(UUID.random());
    request.headers().set(CUSTOM_TRANSACTION_ID_HEADER, transactionId);
    new TransactionHandler(CUSTOM_TRANSACTION_ID_HEADER, request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(transactionId, request1.transactionId());
        assertEquals(transactionId, request1.headers().getFirst(CUSTOM_TRANSACTION_ID_HEADER));
        assertEquals(transactionId, request1.metrics().getTransactionId());
        assertEquals(request1.transactionId(), response.headers().getFirst(CUSTOM_TRANSACTION_ID_HEADER));
        lock.countDown();
    }, response).handle(request);
    assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Mock(org.mockito.Mock) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Metrics(io.gravitee.reporter.api.http.Metrics) TimeUnit(java.util.concurrent.TimeUnit) MockitoAnnotations(org.mockito.MockitoAnnotations) CountDownLatch(java.util.concurrent.CountDownLatch) Response(io.gravitee.gateway.api.Response) UUID(io.gravitee.common.utils.UUID) Request(io.gravitee.gateway.api.Request) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 24 with Request

use of io.gravitee.gateway.api.Request 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);
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) RequestPolicyChain(io.gravitee.gateway.policy.impl.RequestPolicyChain) Metrics(io.gravitee.reporter.api.http.Metrics) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 25 with Request

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

the class ResponsePolicyChainTest 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 = ResponsePolicyChain.create(Arrays.asList(policy2, policy3), executionContext);
    chain.setResultHandler(result -> {
    });
    chain.doNext(request, null);
    verify(request, atLeastOnce()).metrics();
    verify(policy3, atLeastOnce()).onResponse(request, null, chain, executionContext);
    verify(policy2, never()).onResponse(request, null, chain, executionContext);
}
Also used : PolicyChain(io.gravitee.gateway.policy.impl.PolicyChain) ResponsePolicyChain(io.gravitee.gateway.policy.impl.ResponsePolicyChain) Metrics(io.gravitee.reporter.api.http.Metrics) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Aggregations

Request (io.gravitee.gateway.api.Request)51 Test (org.junit.Test)42 HttpHeaders (io.gravitee.common.http.HttpHeaders)24 Response (io.gravitee.gateway.api.Response)21 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)16 PolicyChain (io.gravitee.policy.api.PolicyChain)11 ArrayList (java.util.ArrayList)8 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 Metrics (io.gravitee.reporter.api.http.Metrics)6 Before (org.junit.Before)6 Subscription (io.gravitee.repository.management.model.Subscription)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 UUID (io.gravitee.common.utils.UUID)4 Handler (io.gravitee.gateway.api.handler.Handler)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 Assert.assertNotNull (org.junit.Assert.assertNotNull)4 Mock (org.mockito.Mock)4 Mockito.when (org.mockito.Mockito.when)4