class TestHttpResponse extends java.lang.Object implements java.net.http.HttpResponse
Fluent assertion wrapper around a JDK java.net.http.HttpResponse used by the HTTP client testing helpers.
This type keeps the underlying response accessible through the standard java.net.http.HttpResponse interface while adding convenience methods for the kinds of assertions common in integration and functional tests: status codes, headers, body text, structural JSON checks, XML parsing, and regex matching.
Assertion helpers are chainable and return this when successful, making it practical to express tests as:
def response = http('/health')
response.assertStatus(200)
.assertContains('UP')
JSON helpers use structural comparison instead of raw string comparison, so object key ordering does not matter. Text and regex helpers work on the response body as a java.lang.String. Parsing helpers such as json, jsonList, and xml convert the body into richer representations when tests need to inspect structured content directly. XML parsing uses a secure default groovy.xml.XmlSlurper configuration and can be overridden per wrapper with withXmlSlurper.
When assertion methods accept a headers map, only the provided header entries are validated.
Additional headers present on the response are ignored unless explicitly included in the expected map.
Assertion failures are reported as AssertionFailedError with expected/actual values when possible. Methods that parse JSON may also surface parsing exceptions if the response body is not valid for the configured parser settings.
| Constructor and description |
|---|
TestHttpResponse(java.net.http.HttpResponse<?> response) |
TestHttpResponse(java.net.http.HttpResponse<?> response, SlurperConfig jsonSlurperConfig, SlurperConfig xmlSlurperConfig) |
| Type Params | Return Type | Name and description |
|---|---|---|
|
TestHttpResponse |
assertContains(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence body)Asserts that the response body contains body as a substring. |
|
TestHttpResponse |
assertContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence body)Asserts status and that the response body contains body as a substring. |
|
TestHttpResponse |
assertContainsMatches(java.util.Map<java.lang.String, java.lang.String> headers, java.util.regex.Pattern pattern)Asserts that the response body contains a regex match using java.util.regex.Matcher#find(). |
|
TestHttpResponse |
assertContainsMatches(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.regex.Pattern pattern)Asserts status and that the response body contains a regex match using find(). |
|
TestHttpResponse |
assertEquals(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence body)Asserts exact response body equality. |
|
TestHttpResponse |
assertEquals(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence body)Asserts status and exact response body equality. |
|
TestHttpResponse |
assertHeaders(java.util.Map<java.lang.String, java.lang.String> expected)Asserts exact values for the provided header names. |
|
TestHttpResponse |
assertHeaders(java.util.Map<java.lang.String, java.lang.String> expected, int status)Asserts status and exact values for the provided header names. |
|
TestHttpResponse |
assertHeadersIgnoreCase(java.util.Map<java.lang.String, java.lang.String> expected)Asserts case-insensitive header-name/value equality for all expected entries. |
|
TestHttpResponse |
assertHeadersIgnoreCase(java.util.Map<java.lang.String, java.lang.String> expected, int status)Asserts status and case-insensitive header-name/value equality. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, java.util.Map<java.lang.String, ?> json)Asserts response JSON tree equality against a JSON object. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, java.util.List<?> json)Asserts response JSON tree equality against a JSON array. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.Object json)Asserts response JSON tree equality against a JSON-compatible object. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence json)Asserts response JSON tree equality against a JSON document string. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.Map<java.lang.String, ?> json)Asserts response status and JSON tree equality against a JSON object. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.List<?> json)Asserts response status and JSON tree equality against a JSON array. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.Object json)Asserts response status and JSON tree equality against a JSON-compatible object. |
|
TestHttpResponse |
assertJson(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence json)Asserts response status and JSON tree equality against JSON text. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, java.util.Map<java.lang.String, ?> json)Asserts response JSON contains an expected object subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, java.util.List<?> json)Asserts response JSON contains an expected array subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.Object json)Asserts response JSON contains the expected subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence json)Asserts response JSON contains the expected subset expressed as JSON text. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.Map<java.lang.String, ?> json)Asserts response status and JSON tree contains an expected object subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.List<?> json)Asserts response status and JSON tree contains an expected array subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.Object json)Asserts response status and JSON tree contains an expected JSON-compatible subset. |
|
TestHttpResponse |
assertJsonContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence json)Asserts response status and JSON tree contains the expected subset expressed as JSON text. |
|
TestHttpResponse |
assertMatches(java.util.Map<java.lang.String, java.lang.String> headers, java.util.regex.Pattern pattern)Asserts that the entire response body matches the regex using java.util.regex.Matcher#matches(). |
|
TestHttpResponse |
assertMatches(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.regex.Pattern pattern)Asserts status and that the entire response body matches the regex using matches(). |
|
TestHttpResponse |
assertNotContains(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence text)Asserts that the response body does not contain text as a substring. |
|
TestHttpResponse |
assertNotContains(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence text)Asserts status and that the response body does not contain text as a substring. |
|
TestHttpResponse |
assertNotContainsMatches(java.util.Map<java.lang.String, java.lang.String> headers, java.util.regex.Pattern pattern)Asserts that the response body does not contain a regex match using find(). |
|
TestHttpResponse |
assertNotContainsMatches(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.regex.Pattern pattern)Asserts status and that the response body does not contain a regex match using find(). |
|
TestHttpResponse |
assertNotEquals(java.util.Map<java.lang.String, java.lang.String> headers, java.lang.CharSequence body)Asserts that the full response body does not equal body. |
|
TestHttpResponse |
assertNotEquals(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.lang.CharSequence body)Asserts status and that the full response body does not equal body. |
|
TestHttpResponse |
assertNotHeaders(java.util.Map<java.lang.String, java.lang.String> headers)Asserts that the provided header values are not present as exact first-value matches. |
|
TestHttpResponse |
assertNotMatches(java.util.Map<java.lang.String, java.lang.String> headers, java.util.regex.Pattern pattern)Asserts that the response body does not fully match the regex using matches(). |
|
TestHttpResponse |
assertNotMatches(java.util.Map<java.lang.String, java.lang.String> headers, int status, java.util.regex.Pattern pattern)Asserts status and that the response body does not fully match the regex using matches(). |
|
TestHttpResponse |
assertNotStatus(int status)Asserts that the response status is not equal to status. |
|
TestHttpResponse |
assertStatus(java.util.Map<java.lang.String, java.lang.String> headers, int status)Asserts response status and optional exact header values. |
|
java.lang.String |
body()
|
|
java.lang.String |
getContentType()Shortcut for the Content-Type header value. |
|
boolean |
hasHeader(java.lang.String name)Indicates whether the response contains the named header. |
|
boolean |
hasHeaderValue(java.lang.String name, java.lang.String expected)Indicates whether the named header exists and its first value equals expected exactly. |
|
boolean |
hasHeaderValueIgnoreCase(java.lang.String name, java.lang.String expected)Indicates whether a matching header exists and any of its values equals expected ignoring case. |
|
java.lang.String |
headerValue(java.lang.String name)Returns the first value for the given header, or null when missing. |
|
long |
headerValueAsLong(java.lang.String name)Returns the first value for the given header as a long. |
|
java.lang.String |
headerValueIgnoreCase(java.lang.String name)Returns the first header value for the given name using case-insensitive header-name matching. |
|
HttpHeaders |
headers()
|
|
java.util.Map |
json()Parses the response body as a JSON object. |
|
java.util.List |
jsonList()Parses the response body as a JSON array. |
|
java.util.Optional<java.net.http.HttpResponse<?>> |
previousResponse()Returns the previous response, wrapped as TestHttpResponse when present. |
|
java.net.http.HttpRequest |
request()
|
|
java.util.Optional<javax.net.ssl.SSLSession> |
sslSession()
|
|
int |
statusCode()
|
|
java.net.URI |
uri()
|
|
java.net.http.HttpClient$Version |
version() |
|
TestHttpResponse |
withJsonSlurper(SlurperConfig jsonSlurperConfig)Returns a new response wrapper that uses the provided org.apache.grails.testing.http.client.utils.JsonUtils.SlurperConfig for JSON parsing and JSON assertion helpers. |
|
TestHttpResponse |
withXmlSlurper(SlurperConfig xmlSlurperConfig)Returns a new response wrapper that uses the provided org.apache.grails.testing.http.client.utils.XmlUtils.SlurperConfig for XML parsing helpers. |
|
static TestHttpResponse |
wrap(java.net.http.HttpResponse<?> response)Wraps a raw java.net.http.HttpResponse in TestHttpResponse unless it is already wrapped. |
|
groovy.xml.slurpersupport.GPathResult |
xml()Parses the response body as XML. |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#equals(java.lang.Object), java.lang.Object#getClass(), java.lang.Object#hashCode(), java.lang.Object#notify(), java.lang.Object#notifyAll(), java.lang.Object#toString(), java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int) |
Asserts that the response body contains body as a substring.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredbody - substring expected to be present in the response body Asserts status and that the response body contains body as a substring.
headers - expected headers; ignored when emptystatus - expected HTTP statusbody - substring expected to be present in the response bodyAsserts that the response body contains a regex match using java.util.regex.Matcher#find().
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredpattern - pattern that must be found somewhere in the response body Asserts status and that the response body contains a regex match using find().
headers - expected headers; ignored when emptystatus - expected HTTP statuspattern - pattern that must be found somewhere in the response bodyAsserts exact response body equality.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredbody - expected response bodyAsserts status and exact response body equality.
headers - expected headers; ignored when emptystatus - expected HTTP statusbody - expected response bodyAsserts exact values for the provided header names.
expected - expected header values keyed by header name;
only these entries are checked and extra response headers are ignoredAsserts status and exact values for the provided header names.
expected - expected header values keyed by header name;
only these entries are checked and extra response headers are ignoredstatus - expected HTTP statusAsserts case-insensitive header-name/value equality for all expected entries.
expected - expected headers to validate;
only these entries are checked and extra response headers are ignoredAsserts status and case-insensitive header-name/value equality.
expected - expected headers to validate;
only these entries are checked and extra response headers are ignoredstatus - expected HTTP statusAsserts response JSON tree equality against a JSON object.
headers - expected headers; ignored when emptyjson - expected JSON objectAsserts response JSON tree equality against a JSON array.
headers - expected headers; ignored when emptyjson - expected JSON arrayAsserts response JSON tree equality against a JSON-compatible object.
Uses structural comparison (for example, object key order does not matter).
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredjson - expected JSON-compatible object treeAsserts response JSON tree equality against a JSON document string.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredjson - expected JSON document textAsserts response status and JSON tree equality against a JSON object.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected JSON objectAsserts response status and JSON tree equality against a JSON array.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected JSON arrayAsserts response status and JSON tree equality against a JSON-compatible object.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected JSON-compatible object treeAsserts response status and JSON tree equality against JSON text.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected JSON document textAsserts response JSON contains an expected object subset.
headers - expected headers; ignored when emptyjson - expected subset objectAsserts response JSON contains an expected array subset.
headers - expected headers; ignored when emptyjson - expected subset arrayAsserts response JSON contains the expected subset.
For objects, expected keys must exist; for arrays, expected elements are matched by index.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredjson - expected JSON-compatible subsetAsserts response JSON contains the expected subset expressed as JSON text.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredjson - expected subset encoded as JSON textAsserts response status and JSON tree contains an expected object subset.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected subset objectAsserts response status and JSON tree contains an expected array subset.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected subset arrayAsserts response status and JSON tree contains an expected JSON-compatible subset.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected JSON-compatible subsetAsserts response status and JSON tree contains the expected subset expressed as JSON text.
headers - expected headers; ignored when emptystatus - expected HTTP statusjson - expected subset encoded as JSON textAsserts that the entire response body matches the regex using java.util.regex.Matcher#matches().
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredpattern - pattern that must match the full response body Asserts status and that the entire response body matches the regex using matches().
headers - expected headers; ignored when emptystatus - expected HTTP statuspattern - pattern that must match the full response body Asserts that the response body does not contain text as a substring.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredtext - unexpected substring Asserts status and that the response body does not contain text as a substring.
headers - expected headers; ignored when emptystatus - expected HTTP statustext - unexpected substring Asserts that the response body does not contain a regex match using find().
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredpattern - pattern that must not be found anywhere in the response body Asserts status and that the response body does not contain a regex match using find().
headers - expected headers; ignored when emptystatus - expected HTTP statuspattern - pattern that must not be found anywhere in the response body Asserts that the full response body does not equal body.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredbody - unexpected full response body Asserts status and that the full response body does not equal body.
headers - expected headers; ignored when emptystatus - expected HTTP statusbody - unexpected full response bodyAsserts that the provided header values are not present as exact first-value matches.
headers - header/value pairs that must not match the response Asserts that the response body does not fully match the regex using matches().
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredpattern - pattern that must not match the full response body Asserts status and that the response body does not fully match the regex using matches().
headers - expected headers; ignored when emptystatus - expected HTTP statuspattern - pattern that must not match the full response body Asserts that the response status is not equal to status.
status - unexpected HTTP statusAsserts response status and optional exact header values.
headers - expected headers to validate;
ignored when empty and treated as a subset match when provided,
meaning additional response headers are ignoredstatus - expected HTTP status
Shortcut for the Content-Type header value.
Content-Type header (ignoring case),
or null when the header does not existIndicates whether the response contains the named header.
name - header name to look uptrue if at least one matching header exists Indicates whether the named header exists and its first value equals expected exactly.
name - header name to look upexpected - expected first header valuetrue if the first value matches exactly Indicates whether a matching header exists and any of its values equals expected ignoring case.
name - header name to look up case-insensitivelyexpected - expected header value, compared ignoring casetrue if a matching value exists Returns the first value for the given header, or null when missing.
name - header name to look upnull when no matching header existsReturns the first value for the given header as a long.
Returns the first header value for the given name using case-insensitive header-name matching.
name - header name to look upnull when no matching header exists
Parses the response body as a JSON object.
Parses the response body as a JSON array.
Returns the previous response, wrapped as TestHttpResponse when present.
Returns a new response wrapper that uses the provided org.apache.grails.testing.http.client.utils.JsonUtils.SlurperConfig for JSON parsing and JSON assertion helpers.
This method does not mutate the current wrapper. Instead, it returns a new wrapper sharing the same underlying response while applying the supplied JsonUtils.SlurperConfig to methods such as json(), jsonList(), assertJson(CharSequence), and assertJsonContains(CharSequence).
jsonSlurperConfig - JSON parser configuration to use for the returned wrapperReturns a new response wrapper that uses the provided org.apache.grails.testing.http.client.utils.XmlUtils.SlurperConfig for XML parsing helpers.
This method does not mutate the current wrapper. Instead, it returns a new wrapper sharing the same underlying response while applying the supplied XML parser configuration to xml().
xmlSlurperConfig - XML parser configuration to use for the returned wrapperWraps a raw java.net.http.HttpResponse in TestHttpResponse unless it is already wrapped.
response - raw or already wrapped responseresponse unchanged when already a TestHttpResponse; otherwise a new wrapperParses the response body as XML. A secure groovy.xml.XmlSlurper configuration is used by default, disabling external entity expansion and external DTD loading while remaining namespace aware and non-validating. Override the parser with withXmlSlurper(XmlUtils.SlurperConfig) when a test needs custom XML parsing behavior.