| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test2::Tools::HTTP::UA::Mojo; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 27976 | use strict; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 93 |  | 
| 4 | 3 |  |  | 3 |  | 14 | use warnings; | 
|  | 3 |  |  |  |  | 8 |  | 
|  | 3 |  |  |  |  | 76 |  | 
| 5 | 3 |  |  | 3 |  | 69 | use 5.016; | 
|  | 3 |  |  |  |  | 15 |  | 
| 6 | 3 |  |  | 3 |  | 17 | use parent 'Test2::Tools::HTTP::UA'; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 22 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | # ABSTRACT: Mojo user agent wrapper for Test2::Tools::HTTP | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.05'; # VERSION | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new | 
| 13 |  |  |  |  |  |  | { | 
| 14 | 2 |  |  | 2 | 1 | 15331 | my $class = shift; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # we want to require tese on demand here when the wrapper gets | 
| 17 |  |  |  |  |  |  | # created rather than use them up at the top, because this .pm | 
| 18 |  |  |  |  |  |  | # gets used every time Test2::Tool::HTTP::UA gets used, even | 
| 19 |  |  |  |  |  |  | # if we aren't using Mojolicious in the .t file. | 
| 20 | 2 |  |  |  |  | 641 | require Mojolicious; | 
| 21 | 2 |  |  |  |  | 67091 | require Mojo::URL; | 
| 22 | 2 |  |  |  |  | 10 | require IO::Socket::INET; | 
| 23 | 2 |  |  |  |  | 8 | require HTTP::Request; | 
| 24 | 2 |  |  |  |  | 8 | require HTTP::Response; | 
| 25 | 2 |  |  |  |  | 7 | require HTTP::Message::PSGI; | 
| 26 | 2 |  |  |  |  | 1172 | require Test2::Tools::HTTP::UA::Mojo::Proxy; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 2 |  |  |  |  | 34 | $class->SUPER::new(@_); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub instrument | 
| 32 |  |  |  |  |  |  | { | 
| 33 | 2 |  |  | 2 | 0 | 33 | my($self) = @_; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 2 | 100 |  |  |  | 12 | if($self->ua->server->app) | 
| 36 |  |  |  |  |  |  | { | 
| 37 | 1 |  |  |  |  | 20 | $self->apps->base_url($self->ua->server->url->to_string); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | my $proxy_psgi_app = sub { | 
| 41 | 2 |  |  | 2 |  | 12526 | my $env = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 2 |  |  |  |  | 8 | my $app = $self->apps->uri_to_app($env->{REQUEST_URI}); | 
| 44 | 2 | 50 |  |  |  | 337 | $app | 
| 45 |  |  |  |  |  |  | ? $app->($env) | 
| 46 |  |  |  |  |  |  | : [ 404, [ 'Content-Type' => 'text/plain' ], [ '404 Not Found' ] ]; | 
| 47 | 2 |  |  |  |  | 6902 | }; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 2 |  |  |  |  | 19 | my $proxy_mojo_app = Mojolicious->new; | 
| 50 | 2 |  |  |  |  | 29142 | $proxy_mojo_app->plugin('Mojolicious::Plugin::MountPSGI' => { '/' => $proxy_psgi_app }); | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 2 |  |  |  |  | 20091 | my $proxy_url = Mojo::URL->new("http://127.0.0.1"); | 
| 53 | 2 |  |  |  |  | 840 | $proxy_url->port(do { | 
| 54 | 2 |  |  |  |  | 30 | IO::Socket::INET->new( | 
| 55 |  |  |  |  |  |  | Listen    => 5, | 
| 56 |  |  |  |  |  |  | LocalAddr => '127.0.0.1', | 
| 57 |  |  |  |  |  |  | )->sockport; | 
| 58 |  |  |  |  |  |  | }); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 2 |  |  |  |  | 909 | my $proxy_mojo_server = $self->{proxy_mojo_server} = Mojo::Server::Daemon->new( | 
| 61 |  |  |  |  |  |  | ioloop => $self->ua->ioloop, | 
| 62 |  |  |  |  |  |  | silent => 1, | 
| 63 |  |  |  |  |  |  | app    => $proxy_mojo_app, | 
| 64 |  |  |  |  |  |  | listen => ["$proxy_url"], | 
| 65 |  |  |  |  |  |  | ); | 
| 66 | 2 |  |  |  |  | 902 | $proxy_mojo_server->start; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 2 |  |  |  |  | 3541 | my $old_proxy = $self->ua->proxy; | 
| 69 | 2 |  |  |  |  | 52 | my $new_proxy = Test2::Tools::HTTP::UA::Mojo::Proxy->new( | 
| 70 |  |  |  |  |  |  | apps           => $self->apps, | 
| 71 |  |  |  |  |  |  | http           => $old_proxy->http, | 
| 72 |  |  |  |  |  |  | https          => $old_proxy->https, | 
| 73 |  |  |  |  |  |  | not            => $old_proxy->not, | 
| 74 |  |  |  |  |  |  | apps_proxy_url => $proxy_url, | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 2 |  |  |  |  | 78 | $self->ua->proxy($new_proxy); | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub request | 
| 81 |  |  |  |  |  |  | { | 
| 82 | 15 |  |  | 15 | 0 | 143849 | my($self, $req, %options) = @_; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 15 |  |  |  |  | 117 | require Mojo::Transaction::HTTP; | 
| 85 | 15 |  |  |  |  | 60 | require Mojo::Message::Request; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # Add the User-Agent header to the HTTP::Request | 
| 88 |  |  |  |  |  |  | # so that T2::T::HTTP can see it in diagnostics | 
| 89 | 15 | 50 |  |  |  | 85 | $req->header('User-Agent' => $self->ua->transactor->name) | 
| 90 |  |  |  |  |  |  | unless $req->header('User-Agent'); | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 15 |  |  |  |  | 1867 | my $mojo_req = Mojo::Message::Request->new; | 
| 93 | 15 |  |  |  |  | 139 | $mojo_req->parse($req->to_psgi); | 
| 94 | 15 | 50 |  |  |  | 18591 | $mojo_req->url(Mojo::URL->new($req->uri.'')) | 
| 95 |  |  |  |  |  |  | if $req->uri !~ /^\//; | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 15 |  |  |  |  | 1966 | my $tx = Mojo::Transaction::HTTP->new(req => $mojo_req); | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 15 |  |  |  |  | 104 | my $res; | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 15 | 100 |  |  |  | 53 | if($options{follow_redirects}) | 
| 102 |  |  |  |  |  |  | { | 
| 103 | 1 |  |  |  |  | 3 | my $error; | 
| 104 |  |  |  |  |  |  | $self->ua->start_p($tx)->then(sub { | 
| 105 | 1 |  |  | 1 |  | 4951 | my $tx = shift; | 
| 106 | 1 |  |  |  |  | 5 | $res = HTTP::Response->parse($tx->res->to_string); | 
| 107 | 1 |  |  |  |  | 511 | $res->request(HTTP::Request->parse($tx->req->to_string)); | 
| 108 |  |  |  |  |  |  | })->catch(sub { | 
| 109 | 0 |  |  | 0 |  | 0 | $error = shift; | 
| 110 | 1 |  |  |  |  | 4 | })->wait; | 
| 111 | 1 | 50 |  |  |  | 1021 | $self->error("connection error: $error") if $error; | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | else | 
| 114 |  |  |  |  |  |  | { | 
| 115 | 14 |  |  |  |  | 50 | $self->ua->start($tx); | 
| 116 | 14 |  |  |  |  | 1583899 | my $err = $tx->error; | 
| 117 | 14 | 100 | 100 |  |  | 279 | if($err && !$err->{code}) | 
| 118 |  |  |  |  |  |  | { | 
| 119 | 1 |  |  |  |  | 29 | $self->error("connection error: " . $err->{message}); | 
| 120 |  |  |  |  |  |  | } | 
| 121 | 13 |  |  |  |  | 36 | $res = HTTP::Response->parse($tx->res->to_string); | 
| 122 | 13 |  |  |  |  | 10006 | $res->request($req); | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | # trim weird trailing stuff | 
| 126 | 14 |  |  |  |  | 158 | my $message = $res->message; | 
| 127 | 14 |  |  |  |  | 187 | $message =~ s/\s*$//; | 
| 128 | 14 |  |  |  |  | 47 | $res->message($message); | 
| 129 |  |  |  |  |  |  |  | 
| 130 | 14 |  |  |  |  | 195 | $res; | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | __PACKAGE__->register('Mojo::UserAgent', 'instance'); | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | 1; | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | __END__ |