Class SseBodyParser

java.lang.Object
org.mockserver.fixture.SseBodyParser

public class SseBodyParser extends Object
Parses raw text/event-stream bytes into a list of SseEvent objects suitable for constructing an HttpSseResponse.

The parser follows the SSE specification (W3C Server-Sent Events):

  • Events are separated by blank lines (\n\n)
  • Lines starting with data: set the data field
  • Lines starting with event: set the event type
  • Lines starting with id: set the last event ID
  • Lines starting with retry: set the reconnection time
  • Lines starting with : are comments and are ignored
  • Multiple data: lines within one event are joined with \n

When per-chunk timestamps are available (captured by the streaming relay during recording), the parser applies the actual inter-chunk delays to reproduce the original timing. When per-chunk timestamps are absent (legacy recordings), a fixed inter-event delay is applied to each event (except the first). The default fallback is 50 milliseconds.

  • Field Details

    • DEFAULT_INTER_EVENT_DELAY_MS

      public static final long DEFAULT_INTER_EVENT_DELAY_MS
      Default inter-event delay in milliseconds when per-chunk timestamps are not available.
      See Also:
  • Constructor Details

    • SseBodyParser

      public SseBodyParser()
      Create a parser with the default inter-event delay.
    • SseBodyParser

      public SseBodyParser(long interEventDelayMs)
      Create a parser with a custom inter-event delay.
      Parameters:
      interEventDelayMs - delay in milliseconds between events on replay
  • Method Details

    • parse

      public List<SseEvent> parse(String sseText)
      Parse raw SSE body text into a list of SseEvent objects.
      Parameters:
      sseText - the raw SSE body text (e.g., captured from a forwarded streaming response)
      Returns:
      the parsed events; empty list if the input is null or blank
    • parse

      public List<SseEvent> parse(String sseText, List<Long> perChunkDelaysMs)
      Parse raw SSE body text into a list of SseEvent objects, optionally applying per-chunk replay timing captured during recording.

      When perChunkDelaysMs is non-null and has enough entries, each event (after the first) receives its corresponding captured inter-chunk delay instead of the fixed default. When perChunkDelaysMs is null or too short, the fixed interEventDelayMs fallback is used, preserving backward compatibility with recordings that predate per-chunk timing capture.

      Parameters:
      sseText - the raw SSE body text
      perChunkDelaysMs - optional list of inter-chunk delays in milliseconds (index 0 = first chunk delay, typically 0). May be null for fallback behaviour.
      Returns:
      the parsed events; empty list if the input is null or blank