JSTAPd/Server/Contents.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 140 CC 12 SCAR 26.6
CC
12
Cov
0%
handler
30.0
AUTOLOAD
20.0
__run_api
12.0
CRAP 14.4
1 package JSTAPd::Server::Contents;
2 use strict;
3 use warnings;
4 use Plack::Response;
5 use Data::UUID;
6  
7 sub handler {
8 my($class, $path, $server, $req, $session, $args) = @_;
9  
10 my @chain = split '/', $path;
11 my $method = pop @chain;
12 my $klass = join '::', __PACKAGE__, @chain;
13  
14 if (@chain && $chain[0] eq 'contents') {
15 shift @chain;
16 $klass = 'JSTAPd::Server::Contents::contents';
17 $method = join '/', @chain, $method;
18 } else {
19 no strict 'refs';
20 eval 'require $klass' unless %{"$klass\::"}; ## no critic
21 }
22 unless (!$@ && ($klass->can($method) || $klass->can('AUTOLOAD'))) {
23 return Plack::Response->new(404, [ 'Content-Type' => 'text/plain' ], 'Not Found' );
24 }
25 warn "$klass -> $method : " . $req->uri unless $server->run_once;
26 $klass->$method($server, $req, $session, $args);
27 }
28  
29 # index page
30 sub index {
31 my($class, $server, $req, $session) = @_;
32 return Plack::Response->new(
33 200,
34 [ 'Content-Type' => 'text/html' ],
35 JSTAPd::Contents->build_index(
36 jstapd_prefix => $server->jstapd_prefix,
37 run_once => $server->run_once,
38 auto_open => $server->auto_open,
39 )
40 );
41 }
42  
43 package JSTAPd::Server::Contents::contents;
44 use JSON::XS ();
45  
46 sub AUTOLOAD {
47 my($class, $server, $req, $session, $args) = @_;
48 my $path = our $AUTOLOAD;
49 $path =~ s/.+:://;
50 my @chain = split '/', $path;
51 my $basename = pop @chain;
52  
53 if ($basename eq 'index') {
54 return _index(@_, \@chain);
55 }
56  
57 # foo.t
58 $server->setup_session_tap($session, $path);
59 $server->set_tests($session, $path);
60  
61 my $content = $server->contents->fetch_file($basename, \@chain);
62  
63 if ($args->{is_api}) {
64 return __run_api($content, $server, $req, $session, $args);
65 }
66  
67 my @include = map {
68 ref($_) eq 'SCALAR' ? sprintf "include('/%s/share/js/%s')", $server->jstapd_prefix, ${ $_ } : "include('$_')"
69 } $content->suite->include_ex, $content->suite->include;
70  
71 my $index = $server->contents->fetch_file('index', \@chain, 1);
72 return Plack::Response->new(
73 200,
74 [ 'Content-Type' => 'text/html' ],
75 $index->build_html(
76 $content->header(
77 jstapd_prefix => $server->jstapd_prefix,
78 session => $session,
79 path => $path,
80 include => join('.', @include),
81 ),
82 $content->suite->html_body,
83 ),
84 );
85 }
86  
87 sub __run_api {
88 my($content, $server, $req, $session, $args) = @_;
89 my $tap = $server->get_tap($session, $args->{path});
90  
91 my $GLOBAL = $tap->global_stash;
92 my $METHOD = $req->method;
93 my $PATH = $req->uri->path;
94  
95 my $ret;
96 my $err;
97 do {
98 local $@;
99 eval {
100 $ret = $content->suite->server_api(
101 $tap->global_stash,
102 $req,
103 $req->method,
104 $req->uri->path,
105 );
106 };
107 $err = $@;
108 };
109 if ($err) {
110 my $body = sprintf "error: %s\n\t%s\n", $args->{path}, $err;
111 warn $body;
112 return Plack::Response->new(500, [ 'Content-Type' => 'text/plain' ], $body );
113 }
114 if (ref($ret)) {
115 return Plack::Response->new(200, [ 'Content-Type' => 'application/json' ], JSON::XS->new->ascii->encode($ret) );
116 } else {
117 return Plack::Response->new(200, [ 'Content-Type' => 'text/plain' ], $ret );
118 }
119 }
120  
121 package JSTAPd::Server::Contents::js;
122  
123 sub AUTOLOAD {
124 my($class, $server, $req, $session, $args) = @_;
125 my $path = our $AUTOLOAD;
126 $path =~ s/.+:://;
127 $path =~ s/\.js$//;
128 $path =~ s/-/_/;
129  
130 my $body;
131 $body = $class->$path if $class->can($path);
132 return Plack::Response->new( 404, [ 'Content-Type' => 'text/plain' ], 'Not Found' ) unless $body;
133 return Plack::Response->new( 200, [ 'Content-Type' => 'text/plain' ], $body );
134 }
135  
136 1;
137