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 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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the capturedServiceRequestContext
s.Function<? super HttpService,? extends HttpService>
Creates a new decorator to capture theServiceRequestContext
s.Function<? super HttpService,? extends HttpService>
newDecorator(Predicate<? super ServiceRequestContext> filter)
Creates a new decorator to capture theServiceRequestContext
s satisfying the given predicatefilter
.poll()
Retrieves and removes the first capturedServiceRequestContext
, waiting up to 15 seconds if necessary until an element becomes available.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.take()
Retrieves and removes the first capturedServiceRequestContext
, waiting if necessary until an element becomes available.
-
Constructor Details
-
ServiceRequestContextCaptor
public ServiceRequestContextCaptor()
-
-
Method Details
-
newDecorator
Creates a new decorator to capture theServiceRequestContext
s. -
newDecorator
public Function<? super HttpService,? extends HttpService> newDecorator(Predicate<? super 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
Retrieves 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 ServiceRequestContext poll(long timeout, TimeUnit unit) throws InterruptedExceptionRetrieves 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
-