Class ServiceRequestContextCaptor

java.lang.Object
com.linecorp.armeria.testing.server.ServiceRequestContextCaptor

@UnstableApi public final class ServiceRequestContextCaptor extends Object
Captures the ServiceRequestContexts.

Example:


 class ServiceRequestContextCaptorTest {
     @RegisterExtension
     static final ServerExtension server = new ServerExtension() {
         @Override
         protected void configure(ServerBuilder sb) throws Exception {
             sb.service("/hello", (ctx, req) -> HttpResponse.of(200));
         }
     };

     @Test
     void test() {
         final WebClient client = WebClient.of(server.httpUri());
         final ServiceRequestContextCaptor captor = server.requestContextCaptor();
         client.get("/hello").aggregate().join();
         assertThat(captor.size()).isEqualTo(1);
     }
 }
 

Example: use ServiceRequestContextCaptor manually


 class ServiceRequestContextCaptorTest {
     static final ServiceRequestContextCaptor captor = new ServiceRequestContextCaptor();
     static final Server server = Server.builder()
                                        .decorator(captor.newDecorator())
                                        .service("/hello", (ctx, req) -> HttpResponse.of(200))
                                        .build();

     @BeforeAll
     static void beforeClass() {
         server.start().join();
     }

     @AfterAll
     static void afterClass() {
         server.stop().join();
     }

     @Test
     void test() {
         final WebClient client = WebClient.builder("http://127.0.0.1:" + server.activeLocalPort()).build();
         client.get("/hello").aggregate().join();
         assertThat(captor.size()).isEqualTo(1);
     }

     @AfterEach
     void tearDown() {
         captor.clear();
     }
 }
 
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the captured ServiceRequestContexts.
    boolean
    Returns whether there is any captured ServiceRequestContext.
    Function<? super com.linecorp.armeria.server.HttpService,? extends com.linecorp.armeria.server.HttpService>
    Creates a new decorator to capture the ServiceRequestContexts.
    Function<? super com.linecorp.armeria.server.HttpService,? extends com.linecorp.armeria.server.HttpService>
    newDecorator(Predicate<? super com.linecorp.armeria.server.ServiceRequestContext> filter)
    Creates a new decorator to capture the ServiceRequestContexts satisfying the given predicate filter.
    @Nullable com.linecorp.armeria.server.ServiceRequestContext
    Retrieves and removes the first captured ServiceRequestContext, waiting up to 15 seconds if necessary until an element becomes available.
    @Nullable com.linecorp.armeria.server.ServiceRequestContext
    poll(long timeout, TimeUnit unit)
    Retrieves and removes the first captured ServiceRequestContext, waiting up to the specified wait time if necessary until an element becomes available.
    int
    Returns the number of captured ServiceRequestContexts.
    com.linecorp.armeria.server.ServiceRequestContext
    Retrieves and removes the first captured ServiceRequestContext, waiting if necessary until an element becomes available.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ServiceRequestContextCaptor

      public ServiceRequestContextCaptor()
  • Method Details

    • newDecorator

      public Function<? super com.linecorp.armeria.server.HttpService,? extends com.linecorp.armeria.server.HttpService> newDecorator()
      Creates a new decorator to capture the ServiceRequestContexts.
    • newDecorator

      public Function<? super com.linecorp.armeria.server.HttpService,? extends com.linecorp.armeria.server.HttpService> newDecorator(Predicate<? super com.linecorp.armeria.server.ServiceRequestContext> filter)
      Creates a new decorator to capture the ServiceRequestContexts satisfying the given predicate filter.
    • clear

      public void clear()
      Clears the captured ServiceRequestContexts.
    • size

      public int size()
      Returns the number of captured ServiceRequestContexts.
    • take

      public com.linecorp.armeria.server.ServiceRequestContext take() throws InterruptedException
      Retrieves and removes the first captured ServiceRequestContext, waiting if necessary until an element becomes available.
      Throws:
      InterruptedException - if interrupted while waiting
    • poll

      @Nullable public @Nullable com.linecorp.armeria.server.ServiceRequestContext poll() throws InterruptedException
      Retrieves and removes the first captured ServiceRequestContext, waiting up to 15 seconds if necessary until an element becomes available.
      Throws:
      InterruptedException - if interrupted while waiting
    • poll

      @Nullable public @Nullable com.linecorp.armeria.server.ServiceRequestContext poll(long timeout, TimeUnit unit) throws InterruptedException
      Retrieves and removes the first captured ServiceRequestContext, waiting up to the specified wait time if necessary until an element becomes available.
      Throws:
      InterruptedException - if interrupted while waiting
    • isEmpty

      public boolean isEmpty()
      Returns whether there is any captured ServiceRequestContext.