File Coverage

blib/lib/Plack/Test/Server.pm
Criterion Covered Total %
statement 36 40 90.0
branch 0 2 0.0
condition 3 7 42.8
subroutine 11 12 91.6
pod 0 3 0.0
total 50 64 78.1


line stmt bran cond sub pod time code
1             package Plack::Test::Server;
2 10     10   61 use strict;
  10         15  
  10         423  
3 10     10   40 use warnings;
  10         17  
  10         587  
4 10     10   44 use Carp;
  10         23  
  10         724  
5 10     10   612 use HTTP::Request;
  10         24345  
  10         281  
6 10     10   4664 use HTTP::Response;
  10         61954  
  10         379  
7 10     10   4415 use Test::TCP;
  10         579264  
  10         714  
8 10     10   4402 use Plack::Loader;
  10         31  
  10         304  
9 10     10   3885 use Plack::LWPish;
  10         30  
  10         2209  
10              
11             sub new {
12 11     11 0 32 my($class, $app, %args) = @_;
13              
14 11   50     72 my $host = $args{host} || '127.0.0.1';
15             my $server = Test::TCP->new(
16             listen => $args{listen},
17             host => $host,
18             code => sub {
19 0     0   0 my $sock_or_port = shift;
20             my $server = Plack::Loader->auto(
21             ($args{listen} ? (
22 0 0       0 listen_sock => $sock_or_port,
23             ):(
24             port => $sock_or_port,
25             host => $host,
26             ))
27             );
28 0         0 $server->run($app);
29 0         0 exit;
30             },
31 11         126 );
32              
33 11         671894 bless { server => $server, %args }, $class;
34             }
35              
36             sub port {
37 15     15 0 131 my $self = shift;
38 15         104 $self->{server}->port;
39             }
40              
41             sub request {
42 15     15 0 49 my($self, $req) = @_;
43              
44 15   33     623 my $ua = $self->{ua} || Plack::LWPish->new( no_proxy => [qw/127.0.0.1/] );
45              
46 15         130 $req->uri->scheme('http');
47 15   50     22349 $req->uri->host($self->{host} || '127.0.0.1');
48 15         4516 $req->uri->port($self->port);
49              
50 15         1377 return $ua->request($req);
51             }
52              
53             1;
54              
55             __END__