private static void testRemoteInvocation(Tracing tracing, String remoteServiceName) throws Exception { // prepare parameters final HttpRequest req = HttpRequest.of(HttpMethod.POST, "/hello/armeria"); final RpcRequest rpcReq = RpcRequest.of(HelloService.Iface.class, "hello", "Armeria"); final HttpResponse res = HttpResponse.of(HttpStatus.OK); final RpcResponse rpcRes = RpcResponse.of("Hello, Armeria!"); final ClientRequestContext ctx = new DefaultClientRequestContext( new DefaultEventLoop(), NoopMeterRegistry.get(), H2C, Endpoint.of("localhost", 8080), HttpMethod.POST, "/hello/armeria", null, null, ClientOptions.DEFAULT, req); ctx.logBuilder().startRequest(mock(Channel.class), H2C, "localhost"); ctx.logBuilder().requestContent(rpcReq, req); ctx.logBuilder().endRequest(); @SuppressWarnings("unchecked") Client<HttpRequest, HttpResponse> delegate = mock(Client.class); when(delegate.execute(any(), any())).thenReturn(res); HttpTracingClient stub = new HttpTracingClient(delegate, tracing, remoteServiceName); // do invoke HttpResponse actualRes = stub.execute(ctx, req); assertThat(actualRes).isEqualTo(res); verify(delegate, times(1)).execute(ctx, req); ctx.logBuilder().responseHeaders(HttpHeaders.of(HttpStatus.OK)); ctx.logBuilder().responseContent(rpcRes, res); ctx.logBuilder().endResponse(); }
private static ClientRequestContext newClientContext(RpcRequest req) { return spy(new DefaultClientRequestContext( new DefaultEventLoop(), NoopMeterRegistry.get(), SessionProtocol.HTTP, Endpoint.of("localhost", 8080), HttpMethod.POST, "/cd/thrift/v1", null, null, ClientOptions.DEFAULT, req)); }
@Test public void testProjectAccessController() throws Exception { final EventLoop ev = new DefaultEventLoop(); try { final ServiceRequestContext ctx = spy(mock(ServiceRequestContext.class)); when(ctx.eventLoop()).thenReturn(ev); final DefaultAttributeMap attrs = new DefaultAttributeMap(); attrs.attr(ROLE_MAP).set(roleMap::get); when(ctx.attr(CURRENT_USER_KEY)).thenReturn(attrs.attr(CURRENT_USER_KEY)); when(ctx.attr(ROLE_MAP)).thenReturn(attrs.attr(ROLE_MAP)); try (SafeCloseable ignore = RequestContext.push(ctx)) { attrs.attr(CURRENT_USER_KEY).set(User.DEFAULT); when(ctx.pathParam("projectName")).thenReturn("A"); assertThat(new ProjectMembersOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); assertThat(new ProjectOwnersOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); when(ctx.pathParam("projectName")).thenReturn("B"); assertThat(new ProjectMembersOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); assertThatThrownBy(() -> new ProjectOwnersOnly().serve(delegate, ctx, null)) .isInstanceOf(HttpStatusException.class) .satisfies(cause -> { assertThat(((HttpStatusException) cause).httpStatus()) .isEqualTo(HttpStatus.UNAUTHORIZED); }); attrs.attr(CURRENT_USER_KEY).set(User.ADMIN); assertThat(new AdministratorsOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); assertThat(new ProjectMembersOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); assertThat(new ProjectOwnersOnly().serve(delegate, ctx, null).aggregate().join().status()) .isEqualTo(HttpStatus.OK); } } finally { ev.shutdownGracefully(); } }
public ServerBootstrap(ThriftServerDef serverDef) { this(serverDef, new DefaultChannelGroup(new DefaultEventLoop())); }
@Override protected boolean isCompatible(EventLoop eventloop) { return eventloop instanceof DefaultEventLoop; }
@Override protected EventLoop newChild(Executor executor, Object... args) throws Exception { EventLoop eventLoop = new DefaultEventLoop(this, executor); eventLoop.submit(() -> CURRENT_EVENT_LOOP.set(eventLoop)).syncUninterruptibly(); return eventLoop; }
@BeforeClass public static void setUpEventLoop() { EVENT_LOOP = new DefaultEventLoop(Executors.newSingleThreadExecutor()); }
@BeforeClass public static void startEventLoop() { eventLoop = new DefaultEventLoop(); }