Class HarImporter

java.lang.Object
org.mockserver.imports.HarImporter

public class HarImporter extends Object
Imports expectations from HAR (HTTP Archive) format JSON files.

For each entry in log.entries[], builds a request matcher (method + path + query string parameters) and a response (status, filtered headers, body). The generated matchers are intentionally permissive: volatile headers (Date, Set-Cookie, etc.) are excluded from request matching, and request body matching is only applied when postData.text is present and reasonably sized.

  • Constructor Details

    • HarImporter

      public HarImporter()
  • Method Details

    • volatileRequestHeaders

      public static Set<String> volatileRequestHeaders()
      The lower-cased set of request header names that are volatile, hop-by-hop or over-constraining and so should not appear in a generated request matcher. Exposed (read-only) so the recorded-expectation consolidation path can reuse exactly the same set when stripping volatile headers from recorded requests.
      Returns:
      an immutable set of lower-cased volatile request header names
    • importExpectations

      public List<Expectation> importExpectations(String harJson)
      Parses a HAR JSON string and returns one expectation per entry, with sensitive headers and body fields redacted (see ImportRedaction). Captured HAR files routinely contain real Authorization/API-key headers and secret body fields, so redaction is on by default.
      Parameters:
      harJson - the HAR document as a JSON string
      Returns:
      the generated expectations (may be empty if there are no entries)
      Throws:
      IllegalArgumentException - if the JSON is null, blank, or not a valid HAR
    • importExpectations

      public List<Expectation> importExpectations(String harJson, ImportRedaction.Options redactionOptions)
      Parses a HAR JSON string and returns one expectation per entry, applying the supplied redaction options before the expectations are returned.
      Parameters:
      harJson - the HAR document as a JSON string
      redactionOptions - controls whether/how sensitive data is masked; pass ImportRedaction.Options.disabled() to keep imported values verbatim
      Returns:
      the generated expectations (may be empty if there are no entries)
      Throws:
      IllegalArgumentException - if the JSON is null, blank, or not a valid HAR