Search in sources :

Example 16 with Response

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

the class DefaultReactor method route.

@Override
public void route(Request serverRequest, Response serverResponse, final Handler<Response> handler) {
    LOGGER.debug("Receiving a request {} for path {}", serverRequest.id(), serverRequest.path());
    // Prepare handler chain
    Handler<Request> requestHandlerChain = transactionHandlerFactory.create(request -> {
        ReactorHandler reactorHandler = reactorHandlerResolver.resolve(request);
        if (reactorHandler != null) {
            // Prepare the handler chain
            Handler<Response> responseHandlerChain = new ResponseTimeHandler(request, new ReporterHandler(reporterService, request, handler));
            reactorHandler.handle(request, serverResponse, responseHandlerChain);
        } else {
            LOGGER.debug("No handler can be found for request {}, returning NOT_FOUND (404)", request.path());
            sendNotFound(serverResponse, handler);
        }
    }, serverResponse);
    // Set gateway tenant
    serverRequest.metrics().setTenant(gatewayConfiguration.tenant().orElse(null));
    // And handle the request
    requestHandlerChain.handle(serverRequest);
}
Also used : Response(io.gravitee.gateway.api.Response) ReactorHandler(io.gravitee.gateway.reactor.handler.ReactorHandler) ReporterHandler(io.gravitee.gateway.reactor.handler.reporter.ReporterHandler) ResponseTimeHandler(io.gravitee.gateway.reactor.handler.ResponseTimeHandler) Request(io.gravitee.gateway.api.Request)

Example 17 with Response

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

the class ReactorTest method processRequest_startedApi.

@Test
public void processRequest_startedApi() throws Exception {
    Request request = mock(Request.class);
    when(request.method()).thenReturn(HttpMethod.GET);
    when(request.headers()).thenReturn(new HttpHeaders());
    when(request.headers()).thenReturn(new HttpHeaders());
    when(request.path()).thenReturn("/team");
    when(request.metrics()).thenReturn(Metrics.on(System.currentTimeMillis()).build());
    when(handlerResolver.resolve(any(Request.class))).thenReturn(new AbstractReactorHandler() {

        @Override
        public Reactable reactable() {
            return new Reactable() {

                @Override
                public Object item() {
                    return null;
                }

                @Override
                public String contextPath() {
                    return "";
                }

                @Override
                public boolean enabled() {
                    return true;
                }

                @Override
                public Set dependencies(Class type) {
                    return null;
                }

                @Override
                public Map<String, Object> properties() {
                    return null;
                }
            };
        }

        @Override
        protected void doHandle(Request request, Response response, Handler<Response> handler) {
            Response proxyResponse = mock(Response.class);
            when(proxyResponse.headers()).thenReturn(new HttpHeaders());
            when(proxyResponse.status()).thenReturn(HttpStatusCode.OK_200);
            handler.handle(proxyResponse);
        }
    });
    final CountDownLatch lock = new CountDownLatch(1);
    Response proxyResponse = mock(Response.class);
    when(proxyResponse.headers()).thenReturn(new HttpHeaders());
    reactor.route(request, proxyResponse, response -> {
        Assert.assertEquals(HttpStatusCode.OK_200, response.status());
        lock.countDown();
    });
    Assert.assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Set(java.util.Set) AbstractReactorHandler(io.gravitee.gateway.reactor.handler.AbstractReactorHandler) Request(io.gravitee.gateway.api.Request) CountDownLatch(java.util.concurrent.CountDownLatch) Response(io.gravitee.gateway.api.Response) Map(java.util.Map) Test(org.junit.Test)

Example 18 with Response

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

the class TransactionHandlerTest method shouldHaveTransactionIdWithCustomHeader.

@Test
public void shouldHaveTransactionIdWithCustomHeader() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    when(request.id()).thenReturn(UUID.toString(UUID.random()));
    new TransactionHandler(CUSTOM_TRANSACTION_ID_HEADER, request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(request1.transactionId(), request1.headers().getFirst(CUSTOM_TRANSACTION_ID_HEADER));
        assertEquals(request1.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 19 with Response

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

the class TransactionHandlerTest method shouldPropagateSameTransactionId.

@Test
public void shouldPropagateSameTransactionId() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    String transactionId = UUID.toString(UUID.random());
    request.headers().set(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER, transactionId);
    new TransactionHandler(request1 -> {
        assertNotNull(request1.transactionId());
        assertEquals(transactionId, request1.transactionId());
        assertEquals(transactionId, request1.headers().getFirst(TransactionHandler.DEFAULT_TRANSACTIONAL_ID_HEADER));
        assertEquals(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 20 with Response

use of io.gravitee.gateway.api.Response 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)

Aggregations

Request (io.gravitee.gateway.api.Request)21 Response (io.gravitee.gateway.api.Response)21 Test (org.junit.Test)14 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)13 PolicyChain (io.gravitee.policy.api.PolicyChain)11 HttpHeaders (io.gravitee.common.http.HttpHeaders)7 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)7 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)7 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 Metrics (io.gravitee.reporter.api.http.Metrics)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 MockitoAnnotations (org.mockito.MockitoAnnotations)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2