Created
September 24, 2020 23:30
-
-
Save renatocron/22fff2fb26bf959ee17a3ed3510d7588 to your computer and use it in GitHub Desktop.
mojo-pretty-user-agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojo::Transaction::Role::PrettyDebug { | |
use Mojo::Base -role; | |
use Mojo::Util 'term_escape'; | |
use DDP; | |
use constant PRETTY => $ENV{TRACE} || $ENV{MOJO_CLIENT_PRETTY_DEBUG} || 0; | |
after client_read => sub { | |
my ($self, $chunk) = @_; | |
my $url = $self->req->url->to_abs; | |
my $err = $chunk =~ /1\.1\s[45]0/ ? '31' : '32'; | |
if (PRETTY) { | |
my $tmp | |
= $self->res->json && !$ENV{TRACE_JSON} | |
? $self->res->code . ' ' | |
. $self->res->message . "\n" | |
. $self->res->headers->to_string() . "\n" | |
. np($self->res->json, caller_info => 0) | |
: term_escape($chunk); | |
warn "\x{1b}[${err}m" . term_escape("-- Server response for $url\n") . $tmp . "\x{1b}[0m\n"; | |
} | |
}; | |
around client_write => sub { | |
my $orig = shift; | |
my $self = shift; | |
my $chunk = $self->$orig(@_); | |
my $url = $self->req->url->to_abs; | |
warn "\x{1b}[32m" . term_escape("-- Client requesting $url...\n$chunk") . "\x{1b}[0m\n" if PRETTY; | |
return $chunk; | |
}; | |
}; | |
# no seu testes: | |
my $t = Test::Mojo->new('Penhas'); | |
$t->ua->on( | |
start => sub { | |
my ($ua, $tx) = @_; | |
$tx->with_roles('Mojo::Transaction::Role::PrettyDebug'); | |
} | |
); | |
# tambem deve funcionar se vc mudar isso $app->ua durante o startup phase |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment