File Coverage

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


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