Class ServiceRequestContextCaptor
java.lang.Object
com.linecorp.armeria.testing.server.ServiceRequestContextCaptor
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 -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the capturedServiceRequestContexts.booleanisEmpty()Returns whether there is any capturedServiceRequestContext.Function<? super com.linecorp.armeria.server.HttpService,? extends com.linecorp.armeria.server.HttpService> Creates a new decorator to capture theServiceRequestContexts.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 theServiceRequestContexts satisfying the given predicatefilter.@Nullable com.linecorp.armeria.server.ServiceRequestContextpoll()Retrieves and removes the first capturedServiceRequestContext, waiting up to 15 seconds if necessary until an element becomes available.@Nullable com.linecorp.armeria.server.ServiceRequestContextRetrieves and removes the first capturedServiceRequestContext, waiting up to the specified wait time if necessary until an element becomes available.intsize()Returns the number of capturedServiceRequestContexts.com.linecorp.armeria.server.ServiceRequestContexttake()Retrieves and removes the first capturedServiceRequestContext, waiting if necessary until an element becomes available.
-
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 theServiceRequestContexts. -
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 theServiceRequestContexts satisfying the given predicatefilter. -
clear
public void clear()Clears the capturedServiceRequestContexts. -
size
public int size()Returns the number of capturedServiceRequestContexts. -
take
Retrieves and removes the first capturedServiceRequestContext, 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 InterruptedExceptionRetrieves and removes the first capturedServiceRequestContext, 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 capturedServiceRequestContext, 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 capturedServiceRequestContext.
-