Search in sources :

Example 1 with Request

use of oap.http.Request in project oap by oaplatform.

the class PatternCorsPolicyTest method testAnotherDomainOrigin.

@Test
public void testAnotherDomainOrigin() throws UnknownHostException {
    final Request request = getRequest("http://example.com/", "http://example.com/path/to/api");
    assertEquals(cors.getCors(request).allowOrigin, RequestCors.NO_ORIGIN);
}
Also used : Request(oap.http.Request) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Test(org.testng.annotations.Test)

Example 2 with Request

use of oap.http.Request in project oap by oaplatform.

the class PatternCorsPolicyTest method testSubDomainOrigin.

@Test
public void testSubDomainOrigin() throws UnknownHostException {
    final String origin = "https://oap.oaplatform.org/";
    final Request request = getRequest(origin, "https://oap.oaplatform.org/cors");
    assertEquals(cors.getCors(request).allowOrigin, origin);
}
Also used : Request(oap.http.Request) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Test(org.testng.annotations.Test)

Example 3 with Request

use of oap.http.Request in project oap by oaplatform.

the class SecurityInterceptor2Test method testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent.

@Test
public void testShouldVerifyAndSetUserInSessionIfAuthorizationHeaderIsPresent() throws UnknownHostException {
    val methodWithAnnotation = REFLECTION.method(method -> method.name().equals("methodWithAnnotation")).get();
    val context = new Context("/", InetAddress.getLocalHost(), Protocol.HTTP.name());
    val tokenId = UUID.randomUUID().toString();
    val httpRequest = new HttpGet();
    httpRequest.setHeader("Authorization", tokenId);
    httpRequest.setHeader("Host", "localhost");
    val request = new Request(httpRequest, context);
    val userId = "testUser";
    val token = new Token2(tokenId, userId, DateTimeUtils.currentTimeMillis());
    when(mockTokenService.getToken(tokenId)).thenReturn(Optional.of(token));
    val session = new Session();
    when(mockAclService.checkOne("obj", userId, "parent.read")).thenReturn(true);
    val httpResponse = securityInterceptor.intercept(request, session, methodWithAnnotation, p -> "obj");
    assertThat(httpResponse).isEmpty();
    assertThat(session.get(USER_ID)).contains(userId);
}
Also used : lombok.val(lombok.val) DateTimeUtils(org.joda.time.DateTimeUtils) Reflection(oap.reflect.Reflection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) Reflect(oap.reflect.Reflect) InetAddress(java.net.InetAddress) Arrays.asList(java.util.Arrays.asList) USER_ID(oap.ws.Interceptor.USER_ID) Session(oap.http.Session) MockRequest(oap.http.testng.MockRequest) AclService(oap.security.acl.AclService) Id(oap.util.Id) Context(oap.http.Context) WsParam(oap.ws.WsParam) lombok.val(lombok.val) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Request(oap.http.Request) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) Optional(java.util.Optional) Protocol(oap.http.Protocol) Mockito.mock(org.mockito.Mockito.mock) Context(oap.http.Context) HttpGet(org.apache.http.client.methods.HttpGet) MockRequest(oap.http.testng.MockRequest) Request(oap.http.Request) Session(oap.http.Session) Test(org.testng.annotations.Test)

Example 4 with Request

use of oap.http.Request in project oap by oaplatform.

the class SecurityInterceptor2Test method testShouldVerifyUserIfPresentInSession.

@Test
public void testShouldVerifyUserIfPresentInSession() {
    val methodWithAnnotation = REFLECTION.method(method -> method.name().equals("methodWithAnnotation")).get();
    val userId = "testUser";
    final Session session = new Session();
    session.set(USER_ID, userId);
    when(mockAclService.checkOne("obj", userId, "parent.read")).thenReturn(true);
    final MockRequest request = new MockRequest();
    request.headers.put("authorization", "token1");
    val httpResponse = securityInterceptor.intercept(request, session, methodWithAnnotation, p -> "obj");
    assertThat(httpResponse).isEmpty();
}
Also used : lombok.val(lombok.val) DateTimeUtils(org.joda.time.DateTimeUtils) Reflection(oap.reflect.Reflection) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.testng.annotations.Test) Collections.singletonList(java.util.Collections.singletonList) Reflect(oap.reflect.Reflect) InetAddress(java.net.InetAddress) Arrays.asList(java.util.Arrays.asList) USER_ID(oap.ws.Interceptor.USER_ID) Session(oap.http.Session) MockRequest(oap.http.testng.MockRequest) AclService(oap.security.acl.AclService) Id(oap.util.Id) Context(oap.http.Context) WsParam(oap.ws.WsParam) lombok.val(lombok.val) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Request(oap.http.Request) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) Optional(java.util.Optional) Protocol(oap.http.Protocol) Mockito.mock(org.mockito.Mockito.mock) MockRequest(oap.http.testng.MockRequest) Session(oap.http.Session) Test(org.testng.annotations.Test)

Example 5 with Request

use of oap.http.Request in project oap by oaplatform.

the class NioHandlerAdapter method handle.

@Override
public void handle(final HttpRequest httpRequest, final HttpAsyncExchange httpAsyncExchange, final HttpContext httpContext) throws HttpException, IOException {
    LOGGER.trace("handling [{}]", httpRequest);
    final HttpInetConnection connection = (HttpInetConnection) httpContext.getAttribute(HttpCoreContext.HTTP_CONNECTION);
    final InetAddress remoteAddress = connection.getRemoteAddress();
    final HttpResponse response = httpAsyncExchange.getResponse();
    final String httpContextProtocol = String.valueOf(httpContext.getAttribute("protocol"));
    if (Protocol.LOCAL.equals(this.protocol) && !Inet.isLocalAddress(remoteAddress)) {
        response.setStatusCode(HTTP_FORBIDDEN);
    } else {
        Request request = new Request(httpRequest, new Context(location, remoteAddress, httpContextProtocol));
        handler.handle(request, new Response(response, corsPolicy.getCors(request)));
    }
    httpAsyncExchange.submitResponse();
}
Also used : Context(oap.http.Context) HttpCoreContext(org.apache.http.protocol.HttpCoreContext) HttpContext(org.apache.http.protocol.HttpContext) Response(oap.http.Response) HttpResponse(org.apache.http.HttpResponse) HttpRequest(org.apache.http.HttpRequest) Request(oap.http.Request) HttpResponse(org.apache.http.HttpResponse) HttpInetConnection(org.apache.http.HttpInetConnection) InetAddress(java.net.InetAddress)

Aggregations

Request (oap.http.Request)11 Test (org.testng.annotations.Test)7 Context (oap.http.Context)6 Optional (java.util.Optional)5 UUID (java.util.UUID)5 Session (oap.http.Session)5 Reflect (oap.reflect.Reflect)5 Reflection (oap.reflect.Reflection)5 InetAddress (java.net.InetAddress)4 List (java.util.List)4 lombok.val (lombok.val)4 UnknownHostException (java.net.UnknownHostException)3 HttpResponse (oap.http.HttpResponse)3 Protocol (oap.http.Protocol)3 Response (oap.http.Response)3 HttpGet (org.apache.http.client.methods.HttpGet)3 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)3 DateTime (org.joda.time.DateTime)3 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3