File Coverage

blib/lib/Web/API/Mock.pm
Criterion Covered Total %
statement 71 71 100.0
branch 11 14 78.5
condition 2 3 66.6
subroutine 12 12 100.0
pod 0 3 0.0
total 96 103 93.2


line stmt bran cond sub pod time code
1             package Web::API::Mock;
2 3     3   393008 use 5.008005;
  3         13  
3 3     3   18 use strict;
  3         7  
  3         114  
4 3     3   17 use warnings;
  3         6  
  3         195  
5              
6 3     3   2054 use Plack::Request;
  3         288583  
  3         178  
7 3     3   765 use Web::API::Mock::Parser;
  3         9  
  3         113  
8 3     3   19 use Web::API::Mock::Resource;
  3         6  
  3         125  
9             use Class::Accessor::Lite (
10 3         27 new => 1,
11             rw => [ qw/config files not_implemented_urls map/ ],
12 3     3   21 );
  3         6  
13              
14             our $VERSION = "0.11";
15              
16             sub setup {
17 1     1 0 4281 my ($self, $files, $not_implemented_url_file) = @_;
18 1         4 my $markdown;
19 1         13 for my $file (@{$files}) {
  1         4  
20 2 50   1   102 open my $fh, "<:encoding(utf8)", $file or die "cannot open file. $file:$!";
  1         1056  
  1         21  
  1         10  
21 2         1740 while ( my $line = <$fh> ) {
22 150         444 $markdown .= $line;
23             }
24 2         50 close($fh);
25             }
26 1         12 my $parser = Web::API::Mock::Parser->new();
27 1         31 $parser->md($markdown);
28 1         20 $self->map($parser->create_map());
29              
30 1         15 $self->not_implemented_urls([]);
31 1 50       8 if ($not_implemented_url_file) {
32 1 50       70 open my $fh, "<:encoding(utf8)", $not_implemented_url_file or die "cannot open file. $not_implemented_url_file:$!";
33 1         156 while ( my $line = <$fh> ) {
34 2         26 chomp $line;
35 2         10 $line =~ s/\ //g;
36 2         4 push @{$self->not_implemented_urls}, $line;
  2         21  
37             }
38 1         39 close($fh);
39             }
40             }
41              
42             sub psgi {
43 2     2 0 20942 my $self = shift;
44             sub {
45 13     13   193107 my $env = shift;
46              
47 13         117 my $req = Plack::Request->new($env);
48 13         177 my $plack_response = $req->new_response(404);
49 13         6955 my $response = Web::API::Mock::Resource->status_404;
50              
51 13 100       55 if ($self->check_implemented_url($req->method, $req->path_info)) {
52 2         12 $response = Web::API::Mock::Resource->status_501;
53             }
54             else {
55 11         86 $response = $self->map->request($req->method, $req->path_info);
56 11 100 66     219 if (!$response || !$response->{status}) {
57 3         16 $response = Web::API::Mock::Resource->status_404;
58             }
59             }
60              
61 13         69 $plack_response->headers($response->{header});
62 13         497 $plack_response->content_type($response->{content_type});
63 13         416 $plack_response->status($response->{status});
64 13         120 $plack_response->body($response->{body});
65 13         88 $plack_response->finalize;
66 2         16 };
67             }
68              
69             sub check_implemented_url {
70 13     13 0 257 my ($self, $method, $path) = @_;
71              
72 13 100       52 return if ( ref $self->not_implemented_urls ne 'ARRAY');
73              
74 8         71 my $target = join(',', $method,$path);
75 8         14 my ($url) = grep { m!^$target$! } @{$self->not_implemented_urls};
  16         274  
  8         18  
76             # TODO 再帰
77 8 100       27 unless ($url) {
78 7         67 $target =~ s!^(.+\/).+?$!$1\{.+?\}!;
79 7         16 ($url) = grep { m!^$target$! } @{$self->not_implemented_urls};
  14         463  
  7         21  
80             }
81 8         33 return $url;
82             }
83              
84             1;
85             __END__