java.lang.Object
io.helidon.common.testing.http.junit5.SocketHttpClient
All Implemented Interfaces:
AutoCloseable

public class SocketHttpClient extends Object implements 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 Details

    • SocketHttpClient

      protected SocketHttpClient(String host, int port, Duration timeout)
  • Method Details

    • create

      public static SocketHttpClient create(String host, int port, Duration timeout)
      Socket client that allows sending any content.
      Parameters:
      host - host to connect to
      port - port to connect to
      timeout - socket timeout
      Returns:
      a new (disconnected) client
    • create

      public static SocketHttpClient create(int port)
      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

      public static StringBuilder longData(int bytes)
      Generates at least bytes 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

      public static ClientResponseHeaders headersFromResponse(String response)
      Find headers in response and parse them.
      Parameters:
      response - full HTTP response
      Returns:
      headers map
    • statusFromResponse

      public static Status statusFromResponse(String response)
      Find the status line and return response HTTP status.
      Parameters:
      response - full HTTP response
      Returns:
      status
    • entityFromResponse

      public static String entityFromResponse(String response, boolean validateHeaderFormat)
      Get entity from response.
      Parameters:
      response - response with initial line, headers, and entity
      validateHeaderFormat - whether to validate headers are correctly formatted
      Returns:
      entity string
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      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

      public String sendAndReceive(Method method, String payload)
      A helper method that sends the given payload with the provided method to the server.
      Parameters:
      method - the http method
      payload - 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

      public String 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.
      Parameters:
      method - the http method
      path - the path to access
      payload - 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

      public String sendAndReceive(Method method, String path, String payload, Iterable<String> headers)
      A helper method that sends the given payload at the given path with the provided method to the server.
      Parameters:
      method - the http method
      path - the path to access
      payload - 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

      public String 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

      public SocketHttpClient awaitResponse(String expectedStartsWith, String expectedEndsWith)
      Wait for text coming from socket.
      Parameters:
      expectedStartsWith - expected beginning
      expectedEndsWith - expected end
      Returns:
      this http client
    • request

      public void request(Method method)
      Sends a request to the server.
      Parameters:
      method - the http method
    • request

      public void request(Method method, String payload)
      Sends a request to the server.
      Parameters:
      method - the http method
      payload - the payload to send (must be without the newlines; otherwise it's not a valid payload)
    • request

      public void request(Method method, String path, String payload)
      Sends a request to the server.
      Parameters:
      method - the http method
      path - the path to access
      payload - the payload to send (must be without the newlines; otherwise it's not a valid payload)
    • request

      public void request(Method method, String path, String payload, Iterable<String> headers)
      Sends a request to the server.
      Parameters:
      method - the http method
      path - the path to access
      payload - 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 Method
      path - path
      protocol - protocol
      host - 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

      public void connect(Duration timeout)
      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

      public SocketHttpClient manualRequest(String formatString, Object... args) throws IOException
      Send supplied text manually to socket.
      Parameters:
      formatString - text to send
      args - format arguments
      Returns:
      this http client
      Throws:
      IOException - when we fail to write or read
    • continuePayload

      public SocketHttpClient continuePayload(String payload) throws IOException
      Continue sending more to text to socket.
      Parameters:
      payload - text to be sent
      Returns:
      this http client
      Throws:
      IOException
    • sendChunk

      public SocketHttpClient sendChunk(String payload) throws IOException
      Send single chunk as defined by RFC 9112 §7.1.
      Parameters:
      payload - of the chunk
      Returns:
      this http client
      Throws:
      IOException
    • sendPayload

      protected void sendPayload(PrintWriter pw, String payload)
      Override this to send a specific payload.
      Parameters:
      pw - the print writer where to write the payload
      payload - the payload as provided in the receive() methods.