File Coverage

blib/lib/Plack/App/DAIA/Test.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1 4     4   93400 use strict;
  4         10  
  4         144  
2 4     4   21 use warnings;
  4         6  
  4         247  
3 4     4   53 use v5.10.1;
  4         12  
  4         331  
4             package Plack::App::DAIA::Test;
5             #ABSTRACT: Test DAIA Servers
6             our $VERSION = '0.55'; #VERSION
7 4     4   31 use base 'Test::Builder::Module';
  4         7  
  4         631  
8             our @EXPORT = qw(test_daia_psgi test_daia daia_app);
9              
10 4     4   3171 use URI::Escape;
  4         12227  
  4         343  
11 4     4   30 use Test::More;
  4         8  
  4         28  
12 4     4   4985 use Plack::Test;
  4         6877  
  4         218  
13 4     4   2276 use Plack::App::DAIA;
  0            
  0            
14             use Scalar::Util qw(reftype blessed);
15             use HTTP::Request::Common;
16             use Test::JSON::Entails;
17             use JSON;
18              
19             sub test_daia {
20             my $app = daia_app(shift) || do {
21             __PACKAGE__->builder->ok(0,"Could not construct DAIA application");
22             return;
23             };
24             my $test_name = "test_daia";
25             $test_name = pop(@_) if @_ % 2;
26             while (@_) {
27             my $id = shift;
28             my $expected = shift;
29             my $res = $app->retrieve($id);
30             if (!_if_daia_check( $res, $expected, $test_name )) {
31             $@ = "retrieve method returned a DAIA::Response" unless $@;
32             __PACKAGE__->builder->ok(0, $@);
33             }
34             }
35             }
36              
37             sub test_daia_psgi {
38             my $app = shift;
39              
40             # TODO: load psgi file if string given and allow for URL
41             my $test_name = "test_daia";
42             $test_name = pop(@_) if @_ % 2;
43             while (@_) {
44             my $id = shift;
45             my $expected = shift;
46             test_psgi $app, sub {
47             my $req = shift->(GET "/?id=".uri_escape($id).'&format=xml');
48             my $res = eval { DAIA::parse( $req->content ); };
49             if ($@) {
50             $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
51             $@ =~ s/ at .* line.*//g;
52             $@ =~ s/\s*$//sg;
53             }
54             if (!_if_daia_check( $res, $expected, $test_name )) {
55             $@ = "application returned a DAIA::Response" unless $@;
56             __PACKAGE__->builder->ok(0, $@);
57             }
58             };
59             }
60             }
61              
62             sub daia_app {
63             my $app = shift;
64             if ( blessed($app) and $app->isa('Plack::App::DAIA') ) {
65             return $app;
66             } elsif ( $app =~ qr{^https?://} ) {
67             my $baseurl = $app . ($app =~ /\?/ ? '&id=' : '?id=');
68             $app = sub {
69             my $id = shift;
70             my $url = $baseurl.$id."&format=json";
71             my @daia = eval { DAIA->parse($url) };
72             if (!@daia) {
73             $@ ||= '';
74             if ($@) {
75             $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
76             $@ =~ s/ at .* line.*//g;
77             $@ =~ s/\s*$//sg;
78             }
79             $@ = "invalid DAIA from $url: $@";
80             }
81             return $daia[0];
82             };
83             }
84             if ( ref($app) and reftype($app) eq 'CODE' ) {
85             return Plack::App::DAIA->new( code => $app );
86             }
87             return;
88             }
89              
90             # Call C<$code> with C<$daia> and set as C<$_>, if C<$daia> is a L
91             # and return C<$daia> on success. Return C otherwise.
92             sub _if_daia_check {
93             my ($daia, $expected, $test_name) = @_;
94             if ( blessed($daia) and $daia->isa('DAIA::Response') ) {
95             if ( (reftype($expected)||'') eq 'CODE') {
96             local $_ = $daia;
97             $expected->($daia);
98             } else {
99             local $Test::Builder::Level = $Test::Builder::Level + 2;
100             my $json = decode_json( $daia->json );
101             entails $json, $expected, $test_name;
102             }
103             return $daia;
104             }
105             }
106              
107             1;
108              
109             __END__