Class SocketHttpClient
java.lang.Object
io.helidon.common.testing.http.junit5.SocketHttpClient
- All Implemented Interfaces:
AutoCloseable
The SocketHttpClient provides means to simply pass any bytes over the network
and to see how a server deals with such a case.
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSocketHttpClient(String host, int port, Duration timeout) -
Method Summary
Modifier and TypeMethodDescriptionvoidAssert that the socket is closed.voidAssert that the socket is working and open.awaitResponse(String expectedStartsWith, String expectedEndsWith) Wait for text coming from socket.voidclose()voidconnect()Connect with default timeout.voidConnect with custom connect timeout.booleanWhether this client is connected.continuePayload(String payload) Continue sending more to text to socket.static SocketHttpClientcreate(int port) Socket client that allows sending any content.static SocketHttpClientSocket client that allows sending any content.voidDisconnect from server socket.static StringentityFromResponse(String response, boolean validateHeaderFormat) Get entity from response.static ClientResponseHeadersheadersFromResponse(String response) Find headers in response and parse them.static StringBuilderlongData(int bytes) Generates at leastbytesnumber of bytes as a sequence of decimal numbers delimited by newlines.manualRequest(String formatString, Object... args) Send supplied text manually to socket.receive()Read the data from the socket.voidSends a request to the server.voidSends a request to the server.voidSends a request to the server.voidSends a request to the server.voidrequest(String method, String path, String protocol, String host, Iterable<String> headers, String payload) Send raw data to the server.sendAndReceive(Method method, String payload) A helper method that sends the given payload with the provided method to the server.sendAndReceive(Method method, String path, String payload) A helper method that sends the given payload at the given path with the provided method and headers to the server.A helper method that sends the given payload at the given path with the provided method to the server.Send single chunk as defined by RFC 9112 §7.1.protected voidsendPayload(PrintWriter pw, String payload) Override this to send a specific payload.static StatusstatusFromResponse(String response) Find the status line and return response HTTP status.then(Consumer<SocketHttpClient> exec) Execute immediately given consumer with this socket client as an argument.voidwriteProxyHeader(byte[] header) Write raw proxy protocol header before a request.
-
Constructor Details
-
SocketHttpClient
-
-
Method Details
-
create
Socket client that allows sending any content.- Parameters:
host- host to connect toport- port to connect totimeout- socket timeout- Returns:
- a new (disconnected) client
-
create
Socket client that allows sending any content. Uses localhost and timeout of 5 seconds.- Parameters:
port- port to connect to- Returns:
- a new (disconnected) client
-
longData
Generates at leastbytesnumber of bytes as a sequence of decimal numbers delimited by newlines.- Parameters:
bytes- the amount of bytes to generate (might get little bit more than that)- Returns:
- the generated bytes as a sequence of decimal numbers delimited by newlines
-
headersFromResponse
Find headers in response and parse them.- Parameters:
response- full HTTP response- Returns:
- headers map
-
statusFromResponse
Find the status line and return response HTTP status.- Parameters:
response- full HTTP response- Returns:
- status
-
entityFromResponse
Get entity from response.- Parameters:
response- response with initial line, headers, and entityvalidateHeaderFormat- whether to validate headers are correctly formatted- Returns:
- entity string
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
assertConnectionIsOpen
public void assertConnectionIsOpen()Assert that the socket is working and open. -
assertConnectionIsClosed
public void assertConnectionIsClosed()Assert that the socket is closed. -
sendAndReceive
A helper method that sends the given payload with the provided method to the server.- Parameters:
method- the http methodpayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)- Returns:
- the exact string returned by webserver (including
HTTP/1.1 200 OKline for instance)
-
sendAndReceive
A helper method that sends the given payload at the given path with the provided method and headers to the server.- Parameters:
method- the http methodpath- the path to accesspayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)- Returns:
- the exact string returned by webserver (including
HTTP/1.1 200 OKline for instance)
-
sendAndReceive
A helper method that sends the given payload at the given path with the provided method to the server.- Parameters:
method- the http methodpath- the path to accesspayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)headers- HTTP request headers- Returns:
- the exact string returned by webserver (including
HTTP/1.1 200 OKline for instance)
-
receive
Read the data from the socket. If socket is closed, an empty string is returned.- Returns:
- the read data
-
then
Execute immediately given consumer with this socket client as an argument.- Parameters:
exec- consumer to execute- Returns:
- this http client
-
awaitResponse
Wait for text coming from socket.- Parameters:
expectedStartsWith- expected beginningexpectedEndsWith- expected end- Returns:
- this http client
-
request
Sends a request to the server.- Parameters:
method- the http method
-
request
Sends a request to the server.- Parameters:
method- the http methodpayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)
-
request
Sends a request to the server.- Parameters:
method- the http methodpath- the path to accesspayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)
-
request
Sends a request to the server.- Parameters:
method- the http methodpath- the path to accesspayload- the payload to send (must be without the newlines; otherwise it's not a valid payload)headers- the headers (e.g.,Content-Type: application/json)
-
request
public void request(String method, String path, String protocol, String host, Iterable<String> headers, String payload) Send raw data to the server.- Parameters:
method- HTTP Methodpath- pathprotocol- protocolhost- host header value (if null, host header is not sent)headers- headers (if null, additional headers are not sent)payload- entity (if null, entity is not sent)
-
writeProxyHeader
public void writeProxyHeader(byte[] header) Write raw proxy protocol header before a request.- Parameters:
header- header to write
-
disconnect
public void disconnect()Disconnect from server socket. -
connect
public void connect()Connect with default timeout. -
connect
Connect with custom connect timeout.- Parameters:
timeout- timeout to use
-
connected
public boolean connected()Whether this client is connected.- Returns:
- whether connected to server socket
-
manualRequest
Send supplied text manually to socket.- Parameters:
formatString- text to sendargs- format arguments- Returns:
- this http client
- Throws:
IOException- when we fail to write or read
-
continuePayload
Continue sending more to text to socket.- Parameters:
payload- text to be sent- Returns:
- this http client
- Throws:
IOException
-
sendChunk
Send single chunk as defined by RFC 9112 §7.1.- Parameters:
payload- of the chunk- Returns:
- this http client
- Throws:
IOException
-
sendPayload
Override this to send a specific payload.- Parameters:
pw- the print writer where to write the payloadpayload- the payload as provided in thereceive()methods.
-