Class MailpitClient

java.lang.Object
com.irurueta.authserver.testsupport.MailpitClient

public final class MailpitClient extends Object
Reads the messages a Mailpit server received through its HTTP API, so a test finds the code or the link the application mailed to a recipient. Every call is a plain HTTP request; there is no state besides the server's.
  • Field Details

    • CALL_TIMEOUT

      private static final Duration CALL_TIMEOUT
      Longest wait for one HTTP call.
    • DEFAULT_WAIT

      private static final Duration DEFAULT_WAIT
      Default longest wait for a message to arrive.
    • POLL_MILLIS

      private static final long POLL_MILLIS
      Pause between two polls of the server.
      See Also:
    • HTTP_OK

      private static final int HTTP_OK
      HTTP status of a successful call.
      See Also:
    • CODE

      private static final Pattern CODE
      Matches the numeric code of a message: a run of digits that stands alone.
    • JSON

      private static final tools.jackson.databind.json.JsonMapper JSON
      Reads the JSON answers.
    • http

      private final HttpClient http
      Sends the HTTP calls.
    • baseUrl

      private final String baseUrl
      Base URL of the HTTP API, without a trailing slash.
  • Constructor Details

    • MailpitClient

      public MailpitClient(String baseUrl)
      Creates the client.
      Parameters:
      baseUrl - base URL of the HTTP API, such as http://localhost:8025.
  • Method Details

    • awaitMessageTo

      public MailpitClient.Message awaitMessageTo(String recipient)
      Waits for the first message addressed to a recipient.
      Parameters:
      recipient - address the message was sent to.
      Returns:
      the message.
      Throws:
      IllegalStateException - if none arrives in time.
    • awaitMessagesTo

      public List<MailpitClient.Message> awaitMessagesTo(String recipient, int count)
      Waits until at least a number of messages addressed to a recipient arrived.
      Parameters:
      recipient - address the messages were sent to.
      count - number of messages to wait for.
      Returns:
      the messages, oldest first.
      Throws:
      IllegalStateException - if they do not arrive in time.
    • messagesTo

      public List<MailpitClient.Message> messagesTo(String recipient)
      Lists the messages addressed to a recipient that Mailpit holds now.
      Parameters:
      recipient - address the messages were sent to.
      Returns:
      the messages, oldest first; empty when there are none.
    • countMessagesTo

      public int countMessagesTo(String recipient)
      Counts the messages addressed to a recipient that Mailpit holds now.
      Parameters:
      recipient - address the messages were sent to.
      Returns:
      the number of messages.
    • read

      private MailpitClient.Message read(String id)
      Reads one message.
      Parameters:
      id - identifier Mailpit gave the message.
      Returns:
      the message.
    • get

      private tools.jackson.databind.JsonNode get(String path)
      Calls the API with GET and parses the JSON answer.
      Parameters:
      path - path and query of the call.
      Returns:
      the answer.
    • pause

      private static void pause()
      Sleeps a short while between two polls.