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);
}
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);
}
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);
}
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();
}
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();
}
Aggregations