HtmlUnit 5.9.0 moves several core parts of the library closer to real browsers. It adds a first native fetch() implementation, a much more complete Intl implementation, and a long list of security rules for cookies, CORS, referrers, and local files. Some of these changes are incompatible with earlier versions, so they come first.

Breaking changes

  • Local files: A page that was not loaded from a file can no longer load local files. This applies to scripts, images, stylesheets, workers, importScripts(), navigation, and window.open(). Like browsers, the load fails. If your tests depend on the old behavior, WebClientOptions.setLocalFileAccessFromRemotePagesAllowed(true) restores it, but real browsers block this, so use it sparingly.
  • File URLs: URL.href and anchor href values serialize file URLs without a host, for example file:///C:/a.
  • Protocol setters: location.protocol throws a SyntaxError for an invalid scheme and navigates only to http(s) URLs.
  • Redirects: A redirect to a non-http(s) URL is now a network error.
  • Markup: asXml() writes empty HTML elements with an end tag. If you read back markup written by asXml() before 5.8.0, enable WebClientOptions.setHtmlFragmentSelfClosingTagsAllowed(). Thanks to Ronny Shapiro for this option.
  • Chrome 158: The XSLTProcessor JavaScript object is no longer supported.
  • CSP sandbox: In htmlunit-csp, the sandbox directive no longer makes allowsExternalScript(), allowsInlineScript(), allowsScriptAsAttribute(), or allowsFormAction() return false. The user agent applies the sandbox flags itself.

Native fetch()

fetch() now runs natively instead of only through the polyfill. This release covers the basics:

  • Requests to the page’s origin, data: and blob: URLs, redirects, abort signals, and the redirect modes error and manual
  • Cross-origin requests in cors mode, including preflight requests, credentials modes, and filtering of response headers through Access-Control-Expose-Headers
  • no-cors mode with opaque responses and restricted request headers
  • Cache modes, subresource integrity, and the body size limit for keepalive requests
  • Request, Response, and Headers, including clone(), the body methods, Response.json(), and Response.redirect()
  • AbortController and AbortSignal, including AbortSignal.timeout() and AbortSignal.any()
  • fetch() in dedicated workers

The polyfill is still available as an option and replaces the native implementation when enabled.

Intl

  • Intl.NumberFormat gains formatToParts(), formatRange(), and formatRangeToParts(), plus support for percent, currency, unit, scientific, engineering, and compact notation, along with rounding, grouping, and sign options.
  • Intl.Collator supports options such as usage, sensitivity, numeric, and caseFirst. The German phonebook and traditional Spanish collations are included.
  • resolvedOptions() reports the resolved locale, including relevant Unicode extensions.
  • Locale arguments are canonicalized the way Intl.getCanonicalLocales() does it, and unsupported locales are skipped.

Cookies and origins

  • SameSite is enforced. Strict cookies are not sent cross-site, and Lax cookies are sent only on top-level navigations.
  • Secure cookies set from an insecure origin are ignored, and insecure pages cannot overwrite secure cookies.
  • Prefixes: __Secure- and __Host- cookies must follow their rules.
  • Hidden values: Set-Cookie and Set-Cookie2 headers are no longer visible through getResponseHeader(), so HttpOnly cookie values are no longer readable from JavaScript. Thanks to Vidya m.
  • Domains: Public suffixes such as github.io are treated as public suffixes, and document.cookie can no longer set cookies for another domain. Thanks to Vidya m.
  • Credentials in URLs: location.href, the Referer and Origin headers, and document.referrer no longer contain the user name and password. Thanks to Vidya m.
  • Referrer policy is supported, with strict-origin-when-cross-origin as the default.

CORS

  • Cross-origin requests send a preflight when Accept, Accept-Language, Content-Language, or Content-Type is not CORS-safelisted. Simple Range headers do not trigger one. Thanks to Vidya m.
  • Forbidden request headers such as Origin, DNT, and Set-Cookie are ignored in setRequestHeader(). Method override headers are ignored when they name a forbidden method. Thanks to Vidya m.
  • navigator.sendBeacon() with a non-safelisted body type is a CORS request. Thanks to Vidya m.

Content Security Policy

  • A sandbox directive in an enforced policy sandboxes the document, the same way the sandbox attribute of an iframe does.
  • form-action also applies to redirects from form submissions, and the path of its sources is ignored for redirect targets. Redirect targets for scripts, styles, images, and connections ignore the path too.
  • Data URLs are matched by scheme sources such as data:.
  • Violation reports can be turned off with WebClientOptions.setContentSecurityPolicyReportsEnabled(false).
  • When simulating Chrome/Edge, a WebSocket blocked by connect-src fires only the error event.

Iframes, documents, and windows

  • The sandbox attribute of iframes is supported. Without allow-same-origin, the document gets an opaque origin, which means no cookies and no storage.
  • The srcdoc attribute is supported, and the document uses the about:srcdoc URL.
  • window.origin is available.
  • window.isSecureContext is true for loopback addresses such as 127.0.0.1 and [::1].

JavaScript engine

Together with the Rhino team:

  • Holes are skipped in the array returned by a flatMap callback.
  • for...in skips properties shadowed by non-enumerable ones.
  • Number.prototype.toPrecision is fixed for small values that need padding zeros.

Thank you

Big thanks to Vidya m for the many security and cookie fixes in this release, to Ronny Shapiro for the markup compatibility option, and to the Rhino team for their steady JavaScript work.


Version 5.9.0 is available on Maven Central. Check out the full changes report for the complete list of changes.

Happy testing!

— RBRi


Resources: