| 1 | package WWW::Mechanize::PhantomJS::Catalyst; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.10.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use IO::Socket::INET; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Catalyst::EngineLoader; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use HTTP::Server::PSGI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | extends 'WWW::Mechanize::PhantomJS' => { -version => 0.03 }; | |||||||||
| 10 | |||||||||||
| 11 | 2 | our $VERSION = '0.02'; | |||||||||
| 12 | |||||||||||
| 13 | 2 | has app => ( | |||||||||
| 14 | is => 'ro', | ||||||||||
| 15 | isa => 'ClassName', | ||||||||||
| 16 | required => 1, | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | 2 | has debug => ( | |||||||||
| 20 | is => 'rw', | ||||||||||
| 21 | isa => 'Bool', | ||||||||||
| 22 | ); | ||||||||||
| 23 | |||||||||||
| 24 | 2 | has server_pid => ( | |||||||||
| 25 | is => 'rw', | ||||||||||
| 26 | isa => 'Int', | ||||||||||
| 27 | ); | ||||||||||
| 28 | |||||||||||
| 29 | 2 | has server => ( | |||||||||
| 30 | is => 'rw', | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 2 | has port => ( | |||||||||
| 34 | is => 'rw', | ||||||||||
| 35 | isa => 'Int', | ||||||||||
| 36 | ); | ||||||||||
| 37 | |||||||||||
| 38 | sub new | ||||||||||
| 39 | 1 | ▶ | { | ||||||||
|
Subroutine
new: called CC=4 SCAR=26.7
Pod
documented
| |||||||||||
| 40 | 1 | my ($class, @opt) = @_; | |||||||||
| 41 | 1 | my $self = $class->SUPER::new(@opt); | |||||||||
| 42 | 0 | my ( $port, $status) = $self->start_catalyst_server; | |||||||||
| 43 | 0 | ▶ | if ( $status ne 'ready') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | ▶ | print STDERR "[$$] Catalyst server failed to start\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 0 | return undef; | |||||||||
| 46 | } | ||||||||||
| 47 | 0 | $self->port($port); | |||||||||
| 48 | 0 | ▶ | print STDERR "[$$] Catalyst server started ok\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | return $self; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | sub DESTROY | ||||||||||
| 53 | 0 | ▶ | { | ||||||||
|
Subroutine
DESTROY: not called CC=3 SCAR=24.8
| |||||||||||
| 54 | 0 | my ($self) = @_; | |||||||||
| 55 | 0 | ▶ | if ($self->server_pid) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | local $?; | |||||||||
| 57 | 0 | ▶ | print STDERR "[$$] Waiting for Catalyst server [", $self->server_pid, "] to finish..\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | kill 15, $self->server_pid; | |||||||||
| 59 | 0 | local $SIG{PIPE} = 'IGNORE'; | |||||||||
| 60 | 0 | close $self->server; | |||||||||
| 61 | } | ||||||||||
| 62 | 0 | $self->SUPER::DESTROY; | |||||||||
| 63 | }; | ||||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | sub test_port { | ||||||||
|
Subroutine
test_port: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 66 | 0 | my ($port) = @_; | |||||||||
| 67 | 0 | IO::Socket::INET->new( | |||||||||
| 68 | Listen => 5, | ||||||||||
| 69 | Proto => 'tcp', | ||||||||||
| 70 | Reuse => 1, | ||||||||||
| 71 | LocalPort => $port | ||||||||||
| 72 | ); | ||||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub start_catalyst_server { | ||||||||
|
Subroutine
start_catalyst_server: not called CC=5 SCAR=32.9
Pod
undocumented
| |||||||||||
| 76 | 0 | my ($self) = @_; | |||||||||
| 77 | |||||||||||
| 78 | 0 | my $pid; | |||||||||
| 79 | 0 | ▶ | if (my $pid = open my $server, '-|') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | $self->server_pid($pid); | |||||||||
| 81 | 0 | $self->server($server); | |||||||||
| 82 | 0 | my $port = <$server>; | |||||||||
| 83 | 0 | chomp $port; | |||||||||
| 84 | 0 | my $status = <$server>; | |||||||||
| 85 | 0 | chomp $status; | |||||||||
| 86 | 0 | return $port, $status; | |||||||||
| 87 | } | ||||||||||
| 88 | else { | ||||||||||
| 89 | 0 | require Catalyst::ScriptRunner; | |||||||||
| 90 | 0 | require Catalyst::Script::Server; | |||||||||
| 91 | |||||||||||
| 92 | 0 | my $css_pla = \&Catalyst::Script::Server::_plack_loader_args; | |||||||||
| 93 | 0 | ▶ | my $new_css_pla = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 94 | 0 | my %args = $css_pla->(@_); | |||||||||
| 95 | 0 | my $sr = delete $args{server_ready}; | |||||||||
| 96 | 0 | ▶ | $args{server_ready} = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 97 | 0 | print "ready\n"; | |||||||||
| 98 | 0 | ▶ | $sr ? $sr->(@_) : (); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | }; | |||||||||
| 100 | 0 | return %args; | |||||||||
| 101 | 0 | }; | |||||||||
| 102 | |||||||||||
| 103 | 0 | my $css_run = \&Catalyst::Script::Server::_run_application; | |||||||||
| 104 | 0 | ▶ | my $new_css_run = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 105 | 0 | my $ret; | |||||||||
| 106 | 0 | eval { $ret = $css_run->(@_); }; | |||||||||
| 107 | 0 | ▶ | if ( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 0 | my $msg = $@; | |||||||||
| 109 | 0 | print STDERR "$@\n"; | |||||||||
| 110 | 0 | print "fail\n"; | |||||||||
| 111 | 0 | die $@; | |||||||||
| 112 | } else { | ||||||||||
| 113 | 0 | return $ret; | |||||||||
| 114 | } | ||||||||||
| 115 | 0 | }; | |||||||||
| 116 | |||||||||||
| 117 | # avoid race condition between testing and using port | ||||||||||
| 118 | 0 | my $socket; | |||||||||
| 119 | 0 | my $hsp_sl = \&HTTP::Server::PSGI::setup_listener; | |||||||||
| 120 | 0 | ▶ | my $new_hsp_sl = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 121 | 0 | my $self = shift; | |||||||||
| 122 | 0 | $self->{listen_sock} = $socket; | |||||||||
| 123 | 0 | return $hsp_sl->($self,@_); | |||||||||
| 124 | 0 | }; | |||||||||
| 125 | |||||||||||
| 126 | { | ||||||||||
| 127 | 0 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||
| 128 | 0 | *Catalyst::Script::Server::_plack_loader_args = $new_css_pla; | |||||||||
| 129 | 0 | *Catalyst::Script::Server::_run_application = $new_css_run; | |||||||||
| 130 | 0 | *HTTP::Server::PSGI::setup_listener = $new_hsp_sl; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | 0 | my ($port, $catalyst) = (4000); | |||||||||
| 134 | 0 | while (1) { | |||||||||
| 135 | 0 | ▶ | $port++, next unless $socket = test_port($port); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | ▶ | print STDERR "[$$] Starting Catalyst server on port $port..\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | print "$port\n"; | |||||||||
| 138 | 0 | @ARGV = ('-p', $port); | |||||||||
| 139 | 0 | Catalyst::ScriptRunner->run($self->app, 'Server'); | |||||||||
| 140 | 0 | ▶ | print STDERR "[$$] Catalyst server exited early, aborting\n" if $self->debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | print "fail\n"; | |||||||||
| 142 | 0 | exit 0; | |||||||||
| 143 | } | ||||||||||
| 144 | } | ||||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 148 | 0 | my ($self, $url, %options ) = @_; | |||||||||
| 149 | 0 | ▶ | $url = "http://localhost:".$self->port."/$url" unless $url =~ /^http:/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 0 | return $self->SUPER::get($url, %options); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | 2 | 1; | |||||||||
| 154 | |||||||||||
| 155 | __END__ | ||||||||||