🌻 📖 Test2::Tools::HTTP::Tx

NAME

Test2::Tools::HTTP::Tx - Object representing the last transaction for Test2::Tools::HTTP

VERSION

version 0.11

SYNOPSIS

 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;

DESCRIPTION

This class provides an interface to the most recent transaction performed by http_request in Test2::Tools::HTTP.

METHODS

req

 my $req = http_tx->req;

The HTTP::Request object.

res

 my $res = http_tx->res;

The HTTP::Response object. May or may not be defined in the case of a connection error.

ok

 my $bool = http_tx->ok;

True if the most recent call to http_request passed.

connection_error

 my $string = http_tx->connection_error;

The connection error if any from the most recent http_reequest.

location

The Location header converted to an absolute URL, if provided by the response.

note

 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.

diag

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.

add_helper

 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.

AUTHOR

Graham Ollis <plicease@cpan.org>

COPYRIGHT AND LICENSE

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.