Class TestClient
- All Implemented Interfaces:
AutoCloseable
Each call is admitted by a private dispatcher configured with the application's aggregate and
per-route admission and stream policies and request timeout, and runs on a virtual thread, as in
a network listener. A stream leaves the request lane for the separate stream lane before its head
is returned, so idle streams cannot exhaust request capacity. Admission outcomes map to the same
statuses as the HTTP listener: 503 when capacity, stream capacity or the queue is exhausted or
the queue wait expires, and 504 when the request deadline expires. Framework errors use the same
application/problem+json bodies as the listener: request bodies over the application's
maxRequestBody get 413, unknown routes 404 (501 for an unrecognized method), CONNECT 501,
method mismatches 405 with Allow, OPTIONS without an OPTIONS route 204 with
Allow, and AxiomExceptions thrown by handlers (including body
decoding failures) their own status. Other exceptions thrown by a handler propagate to the test
by default, which helps debugging; startMappingFailures(com.jsgalactic.axiom.application.Application) answers them like the listener
does, with the generic 500 problem response. After codec encoding, response bodies must be
null, String or byte[] and within the transport size limits; anything else fails
the call with IllegalStateException, where the listener would answer 500 (also answered
500 by startMappingFailures(com.jsgalactic.axiom.application.Application)); streamed responses (see Response.stream) are
collected into bytes by execute(com.jsgalactic.axiom.http.Request) and submit(com.jsgalactic.axiom.http.Request), with the listener's byte cap and
deadline, and read incrementally with stream(com.jsgalactic.axiom.http.Request); HEAD follows the same rules and returns
the Content-Length a listener sends. Bodies are sent as raw bytes; this module installs
no codec, so decoding uses whatever codec the test's runtime classpath provides. Request targets
may carry a query and are split and validated by Request.fromTarget(String, String), as
the listener does; a target the listener would answer with 400 throws IllegalArgumentException instead. Transport rules such as 414, 431, Expect handling, pipelining
and keep-alive are not modeled. Admission draws from the application's budget, which is shared
with every listener and other test client of that application. webSocket(java.lang.String, com.jsgalactic.axiom.websocket.WebSocketHandler) models a
WebSocket handshake in memory: an in-memory session pair follows the same message, error and
close rules as a live session, without opening a socket.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Cancels this client's requests, closes its WebSocket sessions and closes the application.Executes a request through admission and waits for its outcome.execute(Request request, BodyReader reader) Executes a request whose body is handed to the handler incrementally through the supplied reader, with the same aggregate cap as a listener: a read pastmaxRequestBodyfails the request with 413.Executes a GET request for the supplied target.Executes a PATCH request with a binary body.Executes a PATCH request with a UTF-8 text body.Executes a POST request with a binary body.Executes a POST request with a UTF-8 text body, for example raw JSON.Executes a PUT request with a binary body.Executes a PUT request with a UTF-8 text body.static TestClientstart(Application application) Freezes registration and takes responsibility for closing the application.static TestClientstart(Application application, StreamBuffering buffering) Freezes registration and takes responsibility for closing the application, with streamed responses (seestream(com.jsgalactic.axiom.http.Request)) buffered like a connection instead of handed over one write at a time.static TestClientstartMappingFailures(Application application) Likestart(com.jsgalactic.axiom.application.Application), but answers a handler failure the way the HTTP listener does, so a test sees what a client would.Executes a request and returns as soon as the response head is known, so that a streamed body (seeResponse.streamandResponse.sse) can be read piece by piece withStreamedResponse.next()while the handler is still writing it.stream(Request request, BodyReader reader) Likestream(Request), with the request body handed to the handler incrementally throughreader.Starts a request without waiting, so tests can hold capacity and observe queueing.submit(Request request, BodyReader reader) Starts a request with an incremental body reader for the handler, mirroring the listener's hand-off: buffering is replaced by reads from the reader, and its bytes are bounded by the application's body limit.LikewebSocket(String, WebSocketHandler), with no callbacks on the test end.webSocket(String target, WebSocketHandler handler) Opens an in-memory WebSocket session on a registered route, without binding a socket.
-
Method Details
-
start
Freezes registration and takes responsibility for closing the application.- Parameters:
application- configured application- Returns:
- a client to use with try-with-resources
-
startMappingFailures
Likestart(com.jsgalactic.axiom.application.Application), but answers a handler failure the way the HTTP listener does, so a test sees what a client would. An exception that is not anAxiomExceptionand that no error handler maps becomes the generic 500application/problem+jsonresponse (status, code and request ID only, withConnection: close), and a response the transport could not send is a 500 too, instead of failing the call. Each such failure is logged at ERROR with its request ID, as the listener does. Interruption, cancellation and a stream aborted after its head was sent still fail the call, because the listener sends no response for them.- Parameters:
application- configured application- Returns:
- a client to use with try-with-resources
-
start
Freezes registration and takes responsibility for closing the application, with streamed responses (seestream(com.jsgalactic.axiom.http.Request)) buffered like a connection instead of handed over one write at a time.- Parameters:
application- configured applicationbuffering- water marks, piece size and stall bound of streams;StreamBuffering.listener()uses the HTTP listener's- Returns:
- a client to use with try-with-resources
-
webSocket
Opens an in-memory WebSocket session on a registered route, without binding a socket. The application's handler is connected to the supplied test handler: a send on either end calls the other end'sonMessage, and a close on either end delivers one close callback to each. The session has the same 64 KiB message bound and close rules as a live session, but no remote address. Closing this client closes every session it opened with code 1001. An unregistered path throws instead of answering 501, since there is no HTTP handshake to answer.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)handler- callbacks of the test end; shared with other sessions, so thread-safe- Returns:
- the test end of the pair
- Throws:
IllegalArgumentException- if no WebSocket route matches the target
-
webSocket
LikewebSocket(String, WebSocketHandler), with no callbacks on the test end. Use the other overload to observe messages and close.- Parameters:
target- absolute raw path, optionally with a query- Returns:
- the test end of the pair
-
get
Executes a GET request for the supplied target.- Parameters:
target- absolute raw path, optionally followed by?and a raw query- Returns:
- response
- Throws:
IllegalArgumentException- for a path or query the listener would answer with 400, validated as byRequest.fromTarget(String, String)Exception- if the handler fails
-
post
Executes a POST request with a UTF-8 text body, for example raw JSON.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- text encoded as UTF-8- Returns:
- response
- Throws:
Exception- if the handler fails
-
post
Executes a POST request with a binary body.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- content; copied- Returns:
- response
- Throws:
Exception- if the handler fails
-
put
Executes a PUT request with a UTF-8 text body.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- text encoded as UTF-8- Returns:
- response
- Throws:
Exception- if the handler fails
-
put
Executes a PUT request with a binary body.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- content; copied- Returns:
- response
- Throws:
Exception- if the handler fails
-
patch
Executes a PATCH request with a UTF-8 text body.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- text encoded as UTF-8- Returns:
- response
- Throws:
Exception- if the handler fails
-
patch
Executes a PATCH request with a binary body.- Parameters:
target- absolute raw path, optionally with a query, as forget(String)contentType- Content-Type header value, or null to send nonebody- content; copied- Returns:
- response
- Throws:
Exception- if the handler fails
-
execute
Executes a request through admission and waits for its outcome.- Parameters:
request- request to execute- Returns:
- the response, or a 503 or 504 problem response for admission and deadline failures
- Throws:
Exception- if the handler fails, unless this client maps failuresIllegalStateException- if the response body or headers cannot be sent by the transport (a 500 response when this client maps failures)
-
execute
Executes a request whose body is handed to the handler incrementally through the supplied reader, with the same aggregate cap as a listener: a read pastmaxRequestBodyfails the request with 413. The client closes the reader when the exchange ends.- Parameters:
request- request to execute; its buffered body is not used when a reader is suppliedreader- source of body bytes, or null for the buffered hand-off ofexecute(Request)- Returns:
- response
- Throws:
Exception- if the handler fails
-
submit
Starts a request without waiting, so tests can hold capacity and observe queueing. The future completes with a 503 or 504 response for admission and deadline failures, and exceptionally for handler failures (with a 500 response when started withstartMappingFailures(com.jsgalactic.axiom.application.Application)). A streamed response completes the future once its body has ended, with the whole body as bytes; usestream(com.jsgalactic.axiom.http.Request)to read it while it is written.- Parameters:
request- request to execute- Returns:
- the request outcome
-
submit
Starts a request with an incremental body reader for the handler, mirroring the listener's hand-off: buffering is replaced by reads from the reader, and its bytes are bounded by the application's body limit. The reader is closed when the exchange completes.- Parameters:
request- request to executereader- source of body bytes, or null for the buffered hand-off- Returns:
- the request outcome
-
stream
Executes a request and returns as soon as the response head is known, so that a streamed body (seeResponse.streamandResponse.sse) can be read piece by piece withStreamedResponse.next()while the handler is still writing it. A response that is not streamed is returned as a single chunk. A stream transfers its request slot into the stream lane before the head is returned and holds that slot until the body ends or the response is closed; ordinary requests keep their own capacity throughout.Failures before the head is known behave as for
execute(com.jsgalactic.axiom.http.Request): admission and deadline failures give a 503 or 504 problem response, and handler exceptions are thrown. Failures after it surface throughStreamedResponse.completion().- Parameters:
request- request to execute- Returns:
- the response head and a reader for its body
- Throws:
Exception- if the handler fails before the head is knownIllegalStateException- if the response headers cannot be sent by the transport
-
stream
Likestream(Request), with the request body handed to the handler incrementally throughreader. The client closes the reader when the exchange ends.- Parameters:
request- request to executereader- source of body bytes, or null for the buffered hand-off- Returns:
- the response head and a reader for its body
- Throws:
Exception- if the handler fails before the head is known
-
close
public void close()Cancels this client's requests, closes its WebSocket sessions and closes the application.- Specified by:
closein interfaceAutoCloseable
-