CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-rest-assured--rest-assured

Java DSL for easy testing of REST services

Pending
Overview
Eval results
Files

response-validation.mddocs/

Response Validation

Comprehensive response validation capabilities including status codes, headers, cookies, body content validation, and data extraction using Hamcrest matchers and JSONPath/XPath expressions.

Capabilities

Response Specification Entry Points

Entry points for defining expected response characteristics.

/**
 * Start building response expectations
 * @return Response specification for defining expected response characteristics
 */
static ResponseSpecification expect();

Usage Examples:

// Using expect() for response validation
expect()
    .statusCode(200)
    .contentType(ContentType.JSON)
    .body("name", equalTo("John"))
.when()
    .get("/users/1");

// More common fluent pattern
given()
    .queryParam("id", 1)
.when()
    .get("/users")
.then()
    .statusCode(200)
    .body("users.size()", greaterThan(0));

Status Code Validation

Methods for validating HTTP response status codes and status lines.

interface ResponseSpecification {
    /**
     * Validate response status code
     * @param expectedStatusCode The expected HTTP status code
     * @return Updated response specification
     */
    ResponseSpecification statusCode(int expectedStatusCode);
    
    /**
     * Validate response status code with Hamcrest matcher
     * @param expectedStatusCode Hamcrest matcher for status code
     * @return Updated response specification
     */
    ResponseSpecification statusCode(Matcher<? super Integer> expectedStatusCode);
    
    /**
     * Validate response status line
     * @param expectedStatusLine The expected status line string
     * @return Updated response specification
     */
    ResponseSpecification statusLine(String expectedStatusLine);
    
    /**
     * Validate response status line with Hamcrest matcher
     * @param expectedStatusLine Hamcrest matcher for status line
     * @return Updated response specification
     */
    ResponseSpecification statusLine(Matcher<String> expectedStatusLine);
}

interface ValidatableResponse {
    /**
     * Assert response status code
     * @param expectedStatusCode The expected HTTP status code
     * @return Updated validatable response
     */
    ValidatableResponse statusCode(int expectedStatusCode);
    
    /**
     * Assert response status code with Hamcrest matcher
     * @param expectedStatusCode Hamcrest matcher for status code
     * @return Updated validatable response
     */
    ValidatableResponse statusCode(Matcher<? super Integer> expectedStatusCode);
    
    /**
     * Assert response status line
     * @param expectedStatusLine The expected status line
     * @return Updated validatable response
     */
    ValidatableResponse statusLine(String expectedStatusLine);
    
    /**
     * Assert response status line with Hamcrest matcher
     * @param expectedStatusLine Hamcrest matcher for status line
     * @return Updated validatable response
     */
    ValidatableResponse statusLine(Matcher<String> expectedStatusLine);
}

Usage Examples:

// Exact status code
get("/users").then().statusCode(200);

// Status code with matcher
get("/users").then().statusCode(anyOf(is(200), is(201)));

// Status line validation
get("/users").then().statusLine("HTTP/1.1 200 OK");
get("/users").then().statusLine(containsString("200"));

Body Content Validation

Methods for validating response body content using JSONPath, XPath, and Hamcrest matchers.

interface ResponseSpecification {
    /**
     * Validate response body using path expression
     * @param path JSONPath or XPath expression
     * @param matcher Hamcrest matcher for the extracted value
     * @return Updated response specification
     */
    ResponseSpecification body(String path, Matcher<?> matcher);
    
    /**
     * Validate multiple body paths
     * @param path First path expression
     * @param matcher First matcher
     * @param additionalKeyMatcherPairs Additional path-matcher pairs
     * @return Updated response specification
     */
    ResponseSpecification body(String path, Matcher<?> matcher, Object... additionalKeyMatcherPairs);
    
    /**
     * Validate response body using path with arguments
     * @param path Path expression with placeholder arguments
     * @param arguments Arguments for path placeholders
     * @param matcher Hamcrest matcher for the extracted value
     * @param additionalKeyMatcherPairs Additional path-matcher pairs
     * @return Updated response specification
     */
    ResponseSpecification body(String path, List<Argument> arguments, Matcher<?> matcher, Object... additionalKeyMatcherPairs);
    
    /**
     * Validate entire response body content
     * @param matcher Hamcrest matcher for the complete body
     * @return Updated response specification
     */
    ResponseSpecification body(Matcher<?> matcher);
    
    /**
     * Validate response content (same as body)
     * @param path JSONPath or XPath expression
     * @param matcher Hamcrest matcher for the extracted value
     * @return Updated response specification
     */
    ResponseSpecification content(String path, Matcher<?> matcher);
    
    /**
     * Validate entire response content (same as body)
     * @param matcher Hamcrest matcher for the complete content
     * @return Updated response specification
     */
    ResponseSpecification content(Matcher<?> matcher);
}

interface ValidatableResponse {
    /**
     * Assert response body using path expression
     * @param path JSONPath or XPath expression
     * @param matcher Hamcrest matcher for the extracted value
     * @return Updated validatable response
     */
    ValidatableResponse body(String path, Matcher<?> matcher);
    
    /**
     * Assert multiple body paths
     * @param path First path expression
     * @param matcher First matcher
     * @param additionalKeyMatcherPairs Additional path-matcher pairs
     * @return Updated validatable response
     */
    ValidatableResponse body(String path, Matcher<?> matcher, Object... additionalKeyMatcherPairs);
    
    /**
     * Assert response body using path with arguments
     * @param path Path expression with placeholder arguments  
     * @param arguments Arguments for path placeholders
     * @param matcher Hamcrest matcher for the extracted value
     * @param additionalKeyMatcherPairs Additional path-matcher pairs
     * @return Updated validatable response
     */
    ValidatableResponse body(String path, List<Argument> arguments, Matcher<?> matcher, Object... additionalKeyMatcherPairs);
    
    /**
     * Assert entire response body content
     * @param matcher Hamcrest matcher for the complete body
     * @return Updated validatable response
     */
    ValidatableResponse body(Matcher<?> matcher);
}

Usage Examples:

// JSONPath validation
get("/users/1").then()
    .body("name", equalTo("John"))
    .body("age", greaterThan(18))
    .body("emails.size()", is(2))
    .body("emails[0]", endsWith("@example.com"));

// Multiple path validation in one call
get("/users/1").then()
    .body("name", equalTo("John"),
          "age", greaterThan(18),
          "active", is(true));

// XPath validation for XML responses
get("/users/1.xml").then()
    .body("user.name", equalTo("John"))
    .body("user.@id", equalTo("123"))
    .body(hasXPath("/user/emails/email[1]", containsString("john")));

// Full body validation
get("/users/1").then()
    .body(containsString("John"))
    .body(not(containsString("password")));

// Path with arguments
get("/users").then()
    .body("users.find { it.name == '%s' }.age", withArgs("John"), greaterThan(18));

Header Validation

Methods for validating HTTP response headers.

interface ResponseSpecification {
    /**
     * Validate response header
     * @param headerName The header name to validate
     * @param expectedValueMatcher Hamcrest matcher for header value
     * @return Updated response specification
     */
    ResponseSpecification header(String headerName, Matcher<?> expectedValueMatcher);
    
    /**
     * Validate response header with string value
     * @param headerName The header name to validate
     * @param expectedValue Expected header value
     * @return Updated response specification
     */
    ResponseSpecification header(String headerName, String expectedValue);
    
    /**
     * Validate multiple response headers
     * @param firstHeaderName First header name
     * @param firstHeaderValueMatcher First header value matcher
     * @param headerNameValueMatchers Additional header name-matcher pairs
     * @return Updated response specification
     */
    ResponseSpecification headers(String firstHeaderName, Matcher<?> firstHeaderValueMatcher, Object... headerNameValueMatchers);
    
    /**
     * Validate response headers from map
     * @param expectedHeaders Map of header names to expected values or matchers
     * @return Updated response specification
     */
    ResponseSpecification headers(Map<String, ?> expectedHeaders);
}

interface ValidatableResponse {
    /**
     * Assert response header
     * @param headerName The header name to validate
     * @param expectedValueMatcher Hamcrest matcher for header value
     * @return Updated validatable response
     */
    ValidatableResponse header(String headerName, Matcher<?> expectedValueMatcher);
    
    /**
     * Assert response header with string value
     * @param headerName The header name to validate
     * @param expectedValue Expected header value
     * @return Updated validatable response
     */
    ValidatableResponse header(String headerName, String expectedValue);
    
    /**
     * Assert multiple response headers
     * @param firstHeaderName First header name
     * @param firstHeaderValueMatcher First header value matcher
     * @param headerNameValueMatchers Additional header name-matcher pairs
     * @return Updated validatable response
     */
    ValidatableResponse headers(String firstHeaderName, Matcher<?> firstHeaderValueMatcher, Object... headerNameValueMatchers);
    
    /**
     * Assert response headers from map
     * @param expectedHeaders Map of header names to expected values or matchers
     * @return Updated validatable response
     */
    ValidatableResponse headers(Map<String, ?> expectedHeaders);
}

Usage Examples:

// Single header validation
get("/users").then()
    .header("Content-Type", "application/json")
    .header("Cache-Control", containsString("no-cache"))
    .header("X-Total-Count", notNullValue());

// Multiple headers
get("/users").then()
    .headers("Content-Type", "application/json",
             "Server", containsString("nginx"));

// Headers with map
Map<String, String> expectedHeaders = new HashMap<>();
expectedHeaders.put("Content-Type", "application/json");
expectedHeaders.put("Content-Encoding", "gzip");
get("/users").then().headers(expectedHeaders);

Cookie Validation

Methods for validating HTTP response cookies.

interface ResponseSpecification {
    /**
     * Validate response cookie
     * @param cookieName The cookie name to validate
     * @param expectedValueMatcher Hamcrest matcher for cookie value
     * @return Updated response specification
     */
    ResponseSpecification cookie(String cookieName, Matcher<?> expectedValueMatcher);
    
    /**
     * Validate response cookie with string value
     * @param cookieName The cookie name to validate
     * @param expectedValue Expected cookie value
     * @return Updated response specification
     */
    ResponseSpecification cookie(String cookieName, String expectedValue);
    
    /**
     * Validate multiple response cookies
     * @param firstCookieName First cookie name
     * @param firstCookieValueMatcher First cookie value matcher
     * @param cookieNameValueMatchers Additional cookie name-matcher pairs
     * @return Updated response specification
     */
    ResponseSpecification cookies(String firstCookieName, Matcher<?> firstCookieValueMatcher, Object... cookieNameValueMatchers);
    
    /**
     * Validate response cookies from map
     * @param expectedCookies Map of cookie names to expected values or matchers
     * @return Updated response specification
     */
    ResponseSpecification cookies(Map<String, ?> expectedCookies);
}

interface ValidatableResponse {
    /**
     * Assert response cookie
     * @param cookieName The cookie name to validate
     * @param expectedValueMatcher Hamcrest matcher for cookie value
     * @return Updated validatable response
     */
    ValidatableResponse cookie(String cookieName, Matcher<?> expectedValueMatcher);
    
    /**
     * Assert response cookie with string value
     * @param cookieName The cookie name to validate
     * @param expectedValue Expected cookie value
     * @return Updated validatable response
     */
    ValidatableResponse cookie(String cookieName, String expectedValue);
    
    /**
     * Assert multiple response cookies
     * @param firstCookieName First cookie name
     * @param firstCookieValueMatcher First cookie value matcher
     * @param cookieNameValueMatchers Additional cookie name-matcher pairs
     * @return Updated validatable response
     */
    ValidatableResponse cookies(String firstCookieName, Matcher<?> firstCookieValueMatcher, Object... cookieNameValueMatchers);
    
    /**
     * Assert response cookies from map
     * @param expectedCookies Map of cookie names to expected values or matchers
     * @return Updated validatable response
     */
    ValidatableResponse cookies(Map<String, ?> expectedCookies);
}

Content Type Validation

Methods for validating response content type.

interface ResponseSpecification {
    /**
     * Validate response content type
     * @param contentType Expected content type
     * @return Updated response specification
     */
    ResponseSpecification contentType(ContentType contentType);
    
    /**
     * Validate response content type with string
     * @param contentType Expected content type as string
     * @return Updated response specification
     */
    ResponseSpecification contentType(String contentType);
    
    /**
     * Validate response content type with matcher
     * @param contentType Hamcrest matcher for content type
     * @return Updated response specification
     */
    ResponseSpecification contentType(Matcher<String> contentType);
}

interface ValidatableResponse {
    /**
     * Assert response content type
     * @param contentType Expected content type
     * @return Updated validatable response
     */
    ValidatableResponse contentType(ContentType contentType);
    
    /**
     * Assert response content type with string
     * @param contentType Expected content type as string
     * @return Updated validatable response
     */
    ValidatableResponse contentType(String contentType);
    
    /**
     * Assert response content type with matcher
     * @param contentType Hamcrest matcher for content type
     * @return Updated validatable response
     */
    ValidatableResponse contentType(Matcher<String> contentType);
}

Response Time Validation

Methods for validating response time.

interface ValidatableResponse {
    /**
     * Assert response time
     * @param matcher Hamcrest matcher for response time in milliseconds
     * @return Updated validatable response
     */
    ValidatableResponse time(Matcher<Long> matcher);
    
    /**
     * Assert response time with time unit
     * @param matcher Hamcrest matcher for response time
     * @param timeUnit Time unit for the matcher
     * @return Updated validatable response
     */
    ValidatableResponse time(Matcher<Long> matcher, TimeUnit timeUnit);
}

Usage Examples:

// Response time validation
get("/users").then()
    .time(lessThan(2000L)) // Less than 2 seconds
    .time(lessThan(5L), TimeUnit.SECONDS); // Less than 5 seconds

Data Extraction

Methods for extracting data from responses for further processing.

interface ValidatableResponse {
    /**
     * Extract data from the response for further processing
     * @return Extractable response interface
     */
    ExtractableResponse<Response> extract();
}

interface ExtractableResponse<T> {
    /**
     * Extract the complete response
     * @return The response object
     */
    T response();
    
    /**
     * Extract response body as string
     * @return Response body as string
     */
    String asString();
    
    /**
     * Extract response body as byte array
     * @return Response body as byte array
     */
    byte[] asByteArray();
    
    /**
     * Extract response body as input stream
     * @return Response body as input stream
     */
    InputStream asInputStream();
    
    /**
     * Extract and deserialize response body to object
     * @param cls Class to deserialize to
     * @return Deserialized object
     */
    <T> T as(Class<T> cls);
    
    /**
     * Extract value using path expression
     * @param path JSONPath or XPath expression
     * @return Extracted value
     */
    <T> T path(String path);
    
    /**
     * Extract value using path expression with arguments
     * @param path Path expression with placeholders
     * @param arguments Arguments for placeholders
     * @return Extracted value
     */
    <T> T path(String path, Object... arguments);
    
    /**
     * Extract JSON path value
     * @param path JSONPath expression
     * @return Extracted value
     */
    <T> T jsonPath(String path);
    
    /**
     * Extract XML path value
     * @param path XPath expression
     * @return Extracted value
     */
    <T> T xmlPath(String path);
    
    /**
     * Extract response header value
     * @param name Header name
     * @return Header value
     */
    String header(String name);
    
    /**
     * Extract response cookie value
     * @param name Cookie name
     * @return Cookie value
     */
    String cookie(String name);
    
    /**
     * Extract response status code
     * @return HTTP status code
     */
    int statusCode();
}

Usage Examples:

// Extract complete response
Response response = get("/users/1").then().extract().response();

// Extract specific values
String name = get("/users/1").then().extract().path("name");
int age = get("/users/1").then().extract().path("age");
List<String> emails = get("/users/1").then().extract().path("emails");

// Extract and deserialize to object
User user = get("/users/1").then().extract().as(User.class);

// Extract headers and cookies
String contentType = get("/users").then().extract().header("Content-Type");
String sessionId = post("/login").then().extract().cookie("JSESSIONID");

// Extract status code
int statusCode = get("/users/1").then().extract().statusCode();

Types

// Main response validation interfaces
interface ResponseSpecification {
    // All validation methods listed above
}

interface ValidatableResponse extends Validatable<ValidatableResponse, Response> {
    // All assertion methods listed above
}

interface ExtractableResponse<T> {
    // All extraction methods listed above
}

// Argument for path expressions
class Argument {
    static Argument arg(Object argument);
    Object getArgument();
}

// Content type enumeration (same as in request building)
enum ContentType {
    JSON("application/json"),
    XML("application/xml"),
    HTML("text/html"),
    TEXT("text/plain"),
    URLENC("application/x-www-form-urlencoded"),
    MULTIPART("multipart/form-data"),
    BINARY("application/octet-stream"),
    ANY("*/*");
}

Install with Tessl CLI

npx tessl i tessl/maven-io-rest-assured--rest-assured

docs

authentication.md

configuration.md

filters-extensions.md

http-operations.md

index.md

object-mapping.md

request-building.md

response-validation.md

tile.json