Class ServiceRequestContextCaptor
java.lang.Object
com.linecorp.armeria.testing.server.ServiceRequestContextCaptor
Captures the
ServiceRequestContext
s.
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 TypeMethodDescriptionvoid
clear()
Clears the capturedServiceRequestContext
s.boolean
isEmpty()
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 theServiceRequestContext
s.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 theServiceRequestContext
s satisfying the given predicatefilter
.@Nullable com.linecorp.armeria.server.ServiceRequestContext
poll()
Retrieves and removes the first capturedServiceRequestContext
, waiting up to 15 seconds if necessary until an element becomes available.@Nullable com.linecorp.armeria.server.ServiceRequestContext
Retrieves and removes the first capturedServiceRequestContext
, waiting up to the specified wait time if necessary until an element becomes available.int
size()
Returns the number of capturedServiceRequestContext
s.com.linecorp.armeria.server.ServiceRequestContext
take()
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 theServiceRequestContext
s. -
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 theServiceRequestContext
s satisfying the given predicatefilter
. -
clear
public void clear()Clears the capturedServiceRequestContext
s. -
size
public int size()Returns the number of capturedServiceRequestContext
s. -
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
.
-