NAME

PAGI::Test::Response - HTTP response wrapper for testing

SYNOPSIS

use PAGI::Test::Client;

my $client = PAGI::Test::Client->new(app => $app);
my $res = $client->get('/');

# Status
say $res->status;        # 200
say $res->is_success;    # true

# Headers
say $res->header('Content-Type');  # 'application/json'
say $res->headers->{location};     # for redirects

# Body
say $res->content;       # raw bytes
say $res->text;          # decoded text
say $res->json->{key};   # parsed JSON

DESCRIPTION

PAGI::Test::Response wraps HTTP response data from test requests, providing convenient accessors for status, headers, and body content.

CONSTRUCTOR

new

my $res = PAGI::Test::Response->new(
    status  => 200,
    headers => [['content-type', 'text/plain']],
    body    => 'Hello',
);

Creates a new response object. Typically you don't call this directly; it's created by PAGI::Test::Client methods.

STATUS METHODS

status

my $code = $res->status;

Returns the HTTP status code (e.g., 200, 404, 500).

is_success

if ($res->is_success) { ... }

True if status is 2xx.

is_redirect

if ($res->is_redirect) { ... }

True if status is 3xx.

is_error

if ($res->is_error) { ... }

True if status is 4xx or 5xx.

exception

if (my $err = $res->exception) {
    like $err, qr/Can't call method/;
}

Returns the exception that was thrown by the application, if any. This is only populated when the test client traps an exception (the default behavior). See "raise_app_exceptions" in PAGI::Test::Client.

Returns undef if no exception occurred.

HEADER METHODS

my $value = $res->header('Content-Type');

Returns the value of a header. Case-insensitive lookup. Returns undef if header not present.

headers

my $hashref = $res->headers;

Returns all headers as a hashref. Header names are lowercased. If a header appears multiple times, the last value wins.

BODY METHODS

content

my $bytes = $res->content;

Returns the raw response body as bytes.

text

my $string = $res->text;

Returns the response body decoded as text. Uses the charset from Content-Type header if present, otherwise assumes UTF-8.

json

my $data = $res->json;

Parses the response body as JSON and returns the data structure. Dies if the body is not valid JSON, with a diagnostic message that includes the HTTP status code, Content-Type header, and body content preview. This helps diagnose cases where the server returned an error page instead of JSON.

The body preview is truncated to $JSON_ERROR_BODY_LIMIT bytes (default 1500). See "CONFIGURATION" to adjust this.

CONVENIENCE METHODS

content_type

my $ct = $res->content_type;

Shortcut for $res->header('content-type').

content_length

my $len = $res->content_length;

Shortcut for $res->header('content-length').

location

my $url = $res->location;

Shortcut for $res->header('location'). Useful for redirects.

CONFIGURATION

$JSON_ERROR_BODY_LIMIT

$PAGI::Test::Response::JSON_ERROR_BODY_LIMIT = 3000;  # increase limit

Controls the maximum number of bytes of response body to include in error messages when json() fails to parse the response. Default is 1500 bytes.

Set this higher if your error pages are verbose and you need more context to diagnose failures. Set it lower if the output is too noisy.

SEE ALSO

PAGI::Test::Client