Search in sources :

Example 36 with Request

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

the class OAuth2AuthenticationHandlerTest method shouldHandleRequest_ignoreCaseAuthorizationHeader.

@Test
public void shouldHandleRequest_ignoreCaseAuthorizationHeader() {
    HttpHeaders headers = new HttpHeaders();
    Request request = mock(Request.class);
    when(request.headers()).thenReturn(headers);
    headers.add(HttpHeaders.AUTHORIZATION, "BeaRer xxx-xx-xxx-xx-xx");
    boolean handle = authenticationHandler.canHandle(request);
    Assert.assertTrue(handle);
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 37 with Request

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

the class OAuth2AuthenticationHandlerTest method shouldNotHandleRequest_noBearerValue.

@Test
public void shouldNotHandleRequest_noBearerValue() {
    HttpHeaders headers = new HttpHeaders();
    Request request = mock(Request.class);
    when(request.headers()).thenReturn(headers);
    headers.add(HttpHeaders.AUTHORIZATION, OAuth2AuthenticationHandler.BEARER_AUTHORIZATION_TYPE + " ");
    boolean handle = authenticationHandler.canHandle(request);
    Assert.assertFalse(handle);
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 38 with Request

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

the class OAuth2AuthenticationHandlerTest method shouldNotHandleRequest_noAuthorizationHeader.

@Test
public void shouldNotHandleRequest_noAuthorizationHeader() {
    Request request = mock(Request.class);
    when(request.headers()).thenReturn(new HttpHeaders());
    boolean handle = authenticationHandler.canHandle(request);
    Assert.assertFalse(handle);
}
Also used : HttpHeaders(io.gravitee.common.http.HttpHeaders) Request(io.gravitee.gateway.api.Request) Test(org.junit.Test)

Example 39 with Request

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

the class PolicyTest method onResponse_mockParameters.

@Test
public void onResponse_mockParameters() throws Exception {
    PolicyMetadata policyDefinition = mock(PolicyMetadata.class);
    when(policyDefinition.policy()).then((Answer<Class>) invocationOnMock -> DummyPolicy.class);
    DummyPolicy policyInst = Mockito.spy((DummyPolicy) policyFactory.create(policyDefinition, null));
    Method onResponseMethod = resolvePolicyMethod(DummyPolicy.class, OnResponse.class);
    when(policyDefinition.method(OnResponse.class)).thenReturn(onResponseMethod);
    Policy policy = PolicyImpl.target(policyInst).definition(policyDefinition).build();
    Request mockRequest = mock(Request.class);
    Response mockResponse = mock(Response.class);
    policy.onResponse(mockRequest, mockResponse);
    verify(policyInst, atLeastOnce()).onResponse(eq(mockRequest), eq(mockResponse), any(PolicyChain.class));
}
Also used : PolicyImpl(io.gravitee.gateway.policy.impl.PolicyImpl) OnResponse(io.gravitee.policy.api.annotations.OnResponse) OnRequest(io.gravitee.policy.api.annotations.OnRequest) ReflectionUtils.withAnnotation(org.reflections.ReflectionUtils.withAnnotation) PolicyFactoryImpl(io.gravitee.gateway.policy.impl.PolicyFactoryImpl) Set(java.util.Set) Test(org.junit.Test) ReflectionUtils.withModifier(org.reflections.ReflectionUtils.withModifier) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Answer(org.mockito.stubbing.Answer) Response(io.gravitee.gateway.api.Response) Request(io.gravitee.gateway.api.Request) Modifier(java.lang.reflect.Modifier) PolicyChain(io.gravitee.policy.api.PolicyChain) Annotation(java.lang.annotation.Annotation) ReflectionUtils(org.reflections.ReflectionUtils) Method(java.lang.reflect.Method) Before(org.junit.Before) OnResponse(io.gravitee.policy.api.annotations.OnResponse) Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) OnRequest(io.gravitee.policy.api.annotations.OnRequest) Request(io.gravitee.gateway.api.Request) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 40 with Request

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

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