CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-seleniumhq-selenium--selenium-http

HTTP client and server abstractions for Selenium WebDriver communication

Pending
Overview
Eval results
Files

request-response.mddocs/

Request and Response

HTTP message abstractions with header management, content handling, and query parameter support for both client requests and server responses.

Capabilities

HttpMessage Base Class

Abstract base class providing common functionality for HTTP messages including header management, content handling, and message attributes.

/**
 * Abstract base class for HTTP messages (requests and responses)
 * Provides header management, content handling, and attribute storage
 */
public abstract class HttpMessage<M extends HttpMessage<M>> {
    /**
     * Retrieves user-defined attribute by key
     * @param key Attribute name
     * @return Attribute value or null if not found
     */
    public Object getAttribute(String key);
    
    /**
     * Sets user-defined attribute
     * @param key Attribute name
     * @param value Attribute value
     * @return This message instance for chaining
     */
    public M setAttribute(String key, Object value);
    
    /**
     * Removes user-defined attribute
     * @param key Attribute name to remove
     * @return This message instance for chaining
     */
    public M removeAttribute(String key);
    
    /**
     * Gets all attribute names
     * @return Iterable of attribute names
     */
    public Iterable<String> getAttributeNames();
    
    /**
     * Executes action for each header name-value pair
     * @param action BiConsumer to process each header
     */
    public void forEachHeader(BiConsumer<String, String> action);
    
    /**
     * Gets all header names
     * @return Iterable of header names (case-insensitive)
     */
    public Iterable<String> getHeaderNames();
    
    /**
     * Gets all values for specific header name
     * @param name Header name (case-insensitive)
     * @return Iterable of header values
     */
    public Iterable<String> getHeaders(String name);
    
    /**
     * Gets first value for specific header name
     * @param name Header name (case-insensitive)
     * @return First header value or null if not found
     */
    public String getHeader(String name);
    
    /**
     * Sets header value, replacing any existing values
     * @param name Header name (case-insensitive)
     * @param value Header value
     * @return This message instance for chaining
     */
    public M setHeader(String name, String value);
    
    /**
     * Adds header value, preserving existing values
     * @param name Header name (case-insensitive)
     * @param value Header value to add
     * @return This message instance for chaining
     */
    public M addHeader(String name, String value);
    
    /**
     * Removes all headers with specified name
     * @param name Header name to remove (case-insensitive)
     * @return This message instance for chaining
     */
    public M removeHeader(String name);
    
    /**
     * Gets content encoding charset from Content-Type header
     * @return Charset for content encoding (defaults to UTF-8)
     */
    public Charset getContentEncoding();
    
    /**
     * Sets message content using supplier
     * @param supplier Content supplier providing InputStream
     * @return This message instance for chaining
     */
    public M setContent(Contents.Supplier supplier);
    
    /**
     * Gets message content supplier
     * @return Contents.Supplier for accessing message body
     */
    public Contents.Supplier getContent();
}

HttpRequest

Represents an HTTP request with method, URI, query parameters, headers, and content.

/**
 * HTTP request implementation extending HttpMessage base class
 * Supports query parameters, headers, and content
 */
public class HttpRequest extends HttpMessage<HttpRequest> {
    /**
     * Creates HTTP request with method and URI
     * @param method HTTP method (GET, POST, etc.)
     * @param uri Request URI path
     */
    public HttpRequest(HttpMethod method, String uri);
    
    /**
     * Gets the request URI
     * @return Request URI string
     */
    public String getUri();
    
    /**
     * Gets the HTTP method
     * @return HTTP method enum value
     */
    public HttpMethod getMethod();
    
    /**
     * Gets first value of query parameter by name
     * Implementation handles URL decoding automatically
     * @param name Parameter name (case-sensitive)
     * @return Parameter value or null if not found
     */
    public String getQueryParameter(String name);
    
    /**
     * Adds query parameter value, preserving existing values
     * Implementation handles URL encoding automatically
     * @param name Parameter name
     * @param value Parameter value
     * @return This request instance for chaining
     */
    public HttpRequest addQueryParameter(String name, String value);
    
    /**
     * Gets all query parameter names
     * @return Iterable of parameter names
     */
    public Iterable<String> getQueryParameterNames();
    
    /**
     * Gets all values for specific query parameter
     * @param name Parameter name
     * @return Iterable of parameter values or null if not found
     */
    public Iterable<String> getQueryParameters(String name);
    
    /**
     * String representation showing method and URI
     * @return String in format "(METHOD) URI"
     */
    public String toString();
}

Usage Examples:

import org.openqa.selenium.remote.http.*;

// Create GET request
HttpRequest getRequest = new HttpRequest(HttpMethod.GET, "/api/users");
getRequest.addHeader("Accept", "application/json");
getRequest.addHeader("User-Agent", "MyApp/1.0");

// Add query parameters
getRequest.addQueryParameter("page", "1");
getRequest.addQueryParameter("size", "20");
getRequest.addQueryParameter("filter", "active");
getRequest.addQueryParameter("filter", "verified"); // Multiple values

// Access query parameters
String page = getRequest.getQueryParameter("page"); // "1"
Iterable<String> filters = getRequest.getQueryParameters("filter"); // ["active", "verified"]

// Create POST request with JSON content
HttpRequest postRequest = new HttpRequest(HttpMethod.POST, "/api/users");
postRequest.setContent(Contents.asJson(Map.of(
    "name", "John Doe",
    "email", "john@example.com"
)));
postRequest.addHeader("Content-Type", "application/json");

System.out.println(postRequest); // "(POST) /api/users"

HttpResponse

Represents an HTTP response with status code, headers, content, and target host information.

/**
 * HTTP response implementation extending HttpMessage base class
 * Includes status code and target host tracking
 */
public class HttpResponse extends HttpMessage<HttpResponse> {
    /**
     * Attribute key for storing target host information
     */
    public static final String HTTP_TARGET_HOST = "http.target.host";
    
    /**
     * Checks if response status indicates success (2xx range)
     * @return true if status is between 200-299, false otherwise
     */
    public boolean isSuccessful();
    
    /**
     * Gets HTTP status code
     * @return Status code (default is 200)
     */
    public int getStatus();
    
    /**
     * Sets HTTP status code
     * @param status Status code to set
     * @return This response instance for chaining
     */
    public HttpResponse setStatus(int status);
    
    /**
     * Sets the host this response was received from
     * Stores as message attribute using HTTP_TARGET_HOST key
     * @param host Originating host name
     * @return This response instance for chaining
     */
    public HttpResponse setTargetHost(String host);
    
    /**
     * Gets the host this response was received from
     * @return Originating host name or null if not set
     */
    public String getTargetHost();
    
    /**
     * String representation showing status and content
     * @return String in format "STATUS: CONTENT"
     */
    public String toString();
}

Usage Examples:

import org.openqa.selenium.remote.http.*;

// Create successful response
HttpResponse response = new HttpResponse();
response.setStatus(200);
response.setContent(Contents.asJson(Map.of(
    "id", 123,
    "name", "John Doe",
    "status", "active"
)));
response.addHeader("Content-Type", "application/json");
response.setTargetHost("api.example.com");

// Check response status
if (response.isSuccessful()) {
    System.out.println("Request succeeded");
    String responseBody = Contents.string(response);
    System.out.println("Response: " + responseBody);
}

// Create error response
HttpResponse errorResponse = new HttpResponse();
errorResponse.setStatus(404);
errorResponse.setContent(Contents.utf8String("User not found"));
errorResponse.addHeader("Content-Type", "text/plain");

System.out.println("Error: " + errorResponse.getStatus());
System.out.println("Host: " + errorResponse.getTargetHost());

HttpMessage Base Class

Abstract base class providing common functionality for HTTP requests and responses including headers, attributes, and content management.

/**
 * Abstract base class for HTTP messages (requests and responses)
 * Provides header management, attributes, and content handling
 * @param <M> The concrete message type for method chaining
 */
abstract class HttpMessage<M extends HttpMessage<M>> {
    /**
     * Gets message attribute by key
     * Attributes are not serialized with the message
     * @param key Attribute name
     * @return Attribute value or null if not found
     */
    public Object getAttribute(String key);
    
    /**
     * Sets message attribute
     * @param key Attribute name
     * @param value Attribute value
     * @return This message instance for chaining
     */
    public M setAttribute(String key, Object value);
    
    /**
     * Removes message attribute
     * @param key Attribute name to remove
     * @return This message instance for chaining
     */
    public M removeAttribute(String key);
    
    /**
     * Gets all attribute names
     * @return Iterable of attribute names
     */
    public Iterable<String> getAttributeNames();
    
    /**
     * Calls action for each header name-value pair
     * @param action BiConsumer to call for each header
     */
    public void forEachHeader(BiConsumer<String, String> action);
    
    /**
     * Gets all header names
     * @return Iterable of header names (case-insensitive)
     */
    public Iterable<String> getHeaderNames();
    
    /**
     * Gets all values for header by name (case-insensitive)
     * @param name Header name
     * @return Iterable of header values
     */
    public Iterable<String> getHeaders(String name);
    
    /**
     * Gets first header value by name (case-insensitive)
     * @param name Header name
     * @return First header value or null if not found
     */
    public String getHeader(String name);
    
    /**
     * Sets header value, replacing any existing values (case-insensitive)
     * @param name Header name
     * @param value Header value
     * @return This message instance for chaining
     */
    public M setHeader(String name, String value);
    
    /**
     * Adds header value, preserving existing values (case-insensitive)
     * @param name Header name
     * @param value Header value to add
     * @return This message instance for chaining
     */
    public M addHeader(String name, String value);
    
    /**
     * Removes all headers with specified name (case-insensitive)
     * @param name Header name to remove
     * @return This message instance for chaining
     */
    public M removeHeader(String name);
    
    /**
     * Gets content character encoding from Content-Type header
     * Defaults to UTF-8 if not specified or invalid
     * @return Charset for content encoding
     */
    public Charset getContentEncoding();
    
    /**
     * Sets message content using Contents.Supplier
     * @param supplier Content supplier providing InputStream
     * @return This message instance for chaining
     */
    public M setContent(Contents.Supplier supplier);
    
    /**
     * Gets message content supplier
     * @return Contents.Supplier for accessing content
     */
    public Contents.Supplier getContent();
}

Usage Examples:

import org.openqa.selenium.remote.http.*;
import java.nio.charset.StandardCharsets;

// Working with headers
HttpRequest request = new HttpRequest(HttpMethod.POST, "/api/data");
request.addHeader("Accept", "application/json");
request.addHeader("Accept-Encoding", "gzip, deflate");
request.setHeader("Authorization", "Bearer token123");

// Iterate over headers
request.forEachHeader((name, value) -> {
    System.out.println(name + ": " + value);
});

// Check specific headers
if (request.getHeader("Authorization") != null) {
    System.out.println("Request is authenticated");
}

// Working with attributes (not serialized)
request.setAttribute("request.id", "req-123");
request.setAttribute("start.time", System.currentTimeMillis());

String requestId = (String) request.getAttribute("request.id");

// Content encoding
request.setHeader("Content-Type", "application/json; charset=ISO-8859-1");
Charset encoding = request.getContentEncoding(); // ISO-8859-1

// Set content with specific encoding
String jsonData = "{\"message\":\"Hello World\"}";
request.setContent(Contents.string(jsonData, StandardCharsets.UTF_8));

HTTP Method Enum

/**
 * HTTP method enumeration with all standard methods
 */
public enum HttpMethod {
    DELETE, GET, POST, PUT, OPTIONS, PATCH, HEAD, CONNECT, TRACE;
    
    /**
     * Parses HTTP method from string (case-insensitive)
     * @param method Method name string
     * @return HttpMethod enum value
     * @throws IllegalArgumentException if method is null or invalid
     */
    public static HttpMethod getHttpMethod(String method);
}

Usage Examples:

// Create requests with different methods
HttpRequest getRequest = new HttpRequest(HttpMethod.GET, "/users");
HttpRequest postRequest = new HttpRequest(HttpMethod.POST, "/users");
HttpRequest putRequest = new HttpRequest(HttpMethod.PUT, "/users/123");
HttpRequest deleteRequest = new HttpRequest(HttpMethod.DELETE, "/users/123");

// Parse method from string
HttpMethod method = HttpMethod.getHttpMethod("PATCH"); // Case-insensitive
HttpRequest patchRequest = new HttpRequest(method, "/users/123");

// Invalid method throws exception
try {
    HttpMethod.getHttpMethod("INVALID"); // Throws IllegalArgumentException
} catch (IllegalArgumentException e) {
    System.err.println("Invalid HTTP method");
}

Install with Tessl CLI

npx tessl i tessl/maven-org-seleniumhq-selenium--selenium-http

docs

client-config.md

content-handling.md

filtering.md

http-client.md

index.md

request-response.md

routing.md

websocket.md

tile.json