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
ModifierConstructorDescriptionprotected
SocketHttpClient
(String host, int port, Duration timeout) Create a new client connecting to the specified coordinates. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Assert that the socket is closed.void
Assert that the socket is working and open.awaitResponse
(String expectedStartsWith, String expectedEndsWith) Wait for text coming from socket.void
close()
void
connect()
Connect with default timeout.void
Connect with custom connect timeout.boolean
Whether this client is connected.continuePayload
(String payload) Continue sending more to text to socket.static SocketHttpClient
create
(int port) Socket client that allows sending any content.static SocketHttpClient
Socket client that allows sending any content.void
Disconnect from server socket.static String
entityFromResponse
(String response, boolean validateHeaderFormat) Get entity from response.static ClientResponseHeaders
headersFromResponse
(String response) Find headers in response and parse them.static StringBuilder
longData
(int bytes) Generates at leastbytes
number 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.void
Sends a request to the server.void
Sends a request to the server.void
Sends a request to the server.void
Sends a request to the server.void
request
(String method, String path, String protocol, String host, Iterable<String> headers, String payload) Send raw data to the server.void
requestRaw
(String content) 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 void
sendPayload
(PrintWriter pw, String payload) Override this to send a specific payload.Provides access to underlying socket reader.static Status
statusFromResponse
(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.void
writeProxyHeader
(byte[] header) Write raw proxy protocol header before a request.
-
Constructor Details
-
SocketHttpClient
Create a new client connecting to the specified coordinates.- Parameters:
host
- host to connect toport
- port to connect totimeout
- socket timeout
-
-
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 leastbytes
number 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:
close
in 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 OK
line 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 OK
line 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 OK
line 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)
-
requestRaw
Send raw data to the server.- Parameters:
content
- content to send over the socket
-
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
- in case the underlying socket throws an exception
-
sendChunk
Send single chunk as defined by RFC 9112 §7.1.- Parameters:
payload
- of the chunk- Returns:
- this http client
- Throws:
IOException
- in case the underlying socket throws an exception
-
socketReader
Provides access to underlying socket reader.- Returns:
- the reader
-
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.
-