File Coverage

blib/lib/Test2/Tools/HTTP/UA.pm
Criterion Covered Total %
statement 61 67 91.0
branch 12 16 75.0
condition 1 3 33.3
subroutine 18 18 100.0
pod 5 5 100.0
total 97 109 88.9


line stmt bran cond sub pod time code
1             package Test2::Tools::HTTP::UA;
2              
3 9     9   237324 use strict;
  9         19  
  9         372  
4 9     9   72 use warnings;
  9         21  
  9         541  
5 9     9   172 use 5.014;
  9         33  
6 9     9   108 use Carp ();
  9         20  
  9         256  
7 9     9   52 use File::Spec ();
  9         17  
  9         242  
8 9     9   5072 use Test2::Tools::HTTP::Apps;
  9         31  
  9         7254  
9              
10             # ABSTRACT: User agent wrapper for Test2::Tools::HTTP
11             our $VERSION = '0.12'; # VERSION
12              
13              
14             sub _init
15             {
16 5     5   17 foreach my $inc (@INC)
17             {
18 40         413 my $dir = File::Spec->catdir($inc, 'Test2/Tools/HTTP/UA');
19 40 100       938 next unless -d $dir;
20 15         35 my $dh;
21 15         762 opendir $dh, $dir;
22 15         729 my @list = sort grep !/^\./, grep /\.pm$/, readdir $dh;
23 15         273 closedir $dh;
24 15         84 foreach my $pm (@list)
25             {
26 30         58 eval { require "Test2/Tools/HTTP/UA/$pm"; };
  30         5995  
27 30 50       191 if(my $error = $@)
28             {
29 0         0 warn $error;
30             }
31             }
32             }
33             }
34              
35             my %classes;
36             my %instance;
37              
38             sub new
39             {
40 12     12 1 662040 my($class, $ua) = @_;
41              
42 12 100       61 if($class eq __PACKAGE__)
43             {
44 5         55 _init();
45 5         41 my $class;
46              
47 5 50 33     50 if(ref($ua) eq '' && defined $ua)
48             {
49 0         0 ($class) = @{ $classes{$ua} };
  0         0  
50             }
51             else
52             {
53 5         26 foreach my $try (keys %instance)
54             {
55 6 100       13 if(eval { $ua->isa($try) })
  6         101  
56             {
57 5         13 ($class) = @{ $instance{$try} };
  5         43  
58             }
59             }
60             }
61              
62 5 50       43 if(defined $class)
63             {
64 5         82 return $class->new($ua);
65             }
66             else
67             {
68 0         0 Carp::croak("user agent @{[ ref $ua ]} not supported ");
  0         0  
69             }
70             }
71              
72             bless {
73 7         81 ua => $ua,
74             }, $class;
75             }
76              
77              
78             sub ua
79             {
80 83     83 1 28310 shift->{ua};
81             }
82              
83              
84             sub apps
85             {
86 98     98 1 651 Test2::Tools::HTTP::Apps->new;
87             }
88              
89              
90             sub error
91             {
92 1     1 1 1123 my(undef, $message, $res) = @_;
93 1         10 my $error = bless { message => $message, res => $res }, 'Test2::Tools::HTTP::UA::Error';
94 1         10 die $error;
95             }
96              
97              
98             sub register
99             {
100 15     15 1 330388 my(undef, $class, $type) = @_;
101 15         65 my $caller = caller;
102 15 100       76 if($type eq 'class')
    50          
103             {
104 7         19 push @{ $classes{$class} }, $caller;
  7         46  
105             }
106             elsif($type eq 'instance')
107             {
108 8         48 push @{ $instance{$class} }, $caller;
  8         74  
109             }
110             else
111             {
112 0         0 Carp::croak("unknown type for $class: $type");
113             }
114             }
115              
116             package Test2::Tools::HTTP::UA::Error;
117              
118             use overload
119 2     2   7 '""' => sub { shift->as_string },
120 9     9   91 bool => sub { 1 }, fallback => 1;
  9     2   23  
  9         119  
  2         8  
121              
122 2     2   9 sub message { shift->{message} }
123 1     1   4 sub res { shift->{res} }
124 2     2   7 sub as_string { shift->message }
125              
126             1;
127              
128             __END__