Test2::Tools::HTTP::Tx - Object representing the last transaction for Test2::Tools::HTTP
version 0.12
use Test2::V0; use Test2::Tools::HTTP; use HTTP::Request::Common; http_request GET('http://example.test'); # get the HTTP::Request/Response object my $req = http_tx->req; my $res = http_tx->res; # send a diagnostic of the most recent # transaction as a note. http_tx->note; done_testing;
This class provides an interface to the most recent transaction performed by http_request
in Test2::Tools::HTTP.
my $req = http_tx->req;
The HTTP::Request object.
my $res = http_tx->res;
The HTTP::Response object. May or may not be defined in the case of a connection error.
my $bool = http_tx->ok;
True if the most recent call to http_request
passed.
my $string = http_tx->connection_error;
The connection error if any from the most recent http_reequest
.
The Location
header converted to an absolute URL, if provided by the response.
http_tx->note;
Send the request, response and ok to Test2's "note" output. Note that the message bodies may be decoded, but the headers will not be modified.
Send the request, response and ok to Test2's "diag" output. Note that the message bodies may be decoded, but the headers will not be modified.
Test2::Tools::HTTP::Tx->add_helper( $name, $code );
Adds a transaction helper to the given class. For example.
Test2::Tools::HTTP::Tx->add_helper( 'tx.foo' => sub { my $tx = shift; ... } ); Test2::Tools::HTTP::Tx->add_helper( 'req.bar' => sub { my $req = shift; ... } ); Test2::Tools::HTTP::Tx->add_helper( 'res.baz' => sub { my $res = shift; ... } );
Lets you call these helpers thus:
http_tx->foo; http_tx->req->bar; http_tx->res->baz;
A useful application of this technique is to provide conversion of the response body:
use JSON::PP qw( decode_json ); Test2::Tools::HTTP::Tx->add_helper( 'res.json' => sub { my $res = shift; decode_json( $res->decoded_content ); });
You cannot add helpers that replace existing methods.
Graham Ollis <plicease@cpan.org>
This software is copyright (c) 2018-2022 by Graham Ollis.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.