blib/lib/Mojo/UserAgent/SecureServer.pm

filter: Statementstmt 100% 35 / 35 Branchbran 91.6% 11 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 95.0% 58 / 61 CC 10 SCAR 13.7
CC
10
Cov
96%
_url
6.0
from_ua
5.0
BEGIN
1.0
CRAP 4.0
↑ Summary
1 package Mojo::UserAgent::SecureServer;
2 3 ▶use Mojo::Base 'Mojo::UserAgent::Server';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Net::SSLeay ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.04';
8  
9 3 ▶has listen => sub { Mojo::URL->new('https://127.0.0.1') };
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 5 ▶sub from_ua {
Subroutine from_ua: called CC=5 SCAR=16.1
Pod documented
12 5 ▶ my $self = ref $_[0] ? shift : shift->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 2 5
13 5 my $ua = shift;
14 5 my $server = $ua->server;
15  
16 5 $self->$_($server->$_) for qw(app ioloop);
17 5 $self->listen->query->param($_ => $ua->$_) for grep { $ua->$_ } qw(ca cert key);
18 5 ▶ $self->listen->query->param(verify => Net::SSLeay::VERIFY_PEER()) unless $ua->insecure;
Branch: 1 branch
BranchTrueFalseTotal
unless $ua->insecure 4 1 5
19  
20 5 return $self;
21 }
22  
23 1 ▶sub nb_url { shift->_url(1, @_) }
Subroutine nb_url: called CC=1 SCAR=0.0
Pod documented
24 2 ▶sub url { shift->_url(0, @_) }
Subroutine url: called CC=1 SCAR=0.0
Pod documented
25  
26 3 ▶sub _url {
Subroutine _url: called CC=6 SCAR=18.0
27 3 my ($self, $nb) = @_;
28  
29 3 ▶ my $port_key = $nb ? 'nb_port' : 'port';
Branch: 1 branch
BranchTrueFalseTotal
$nb ? : 1 2 3
30 3 ▶ unless ($self->{$port_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{$port_key}) 3 0 3
31 3 ▶ my $server_key = $nb ? 'nb_server' : 'server';
Branch: 1 branch
BranchTrueFalseTotal
$nb ? : 1 2 3
32 3 my $url = $self->listen->clone;
33 3 my @daemon_attrs = (silent => 1);
34 3 ▶ push @daemon_attrs, ioloop => $self->ioloop unless $nb;
Branch: 1 branch
BranchTrueFalseTotal
unless $nb 2 1 3
35  
36 3 my $server = $self->{$server_key} = Mojo::Server::Daemon->new(@daemon_attrs);
37 3 weaken $server->app($self->app)->{app};
38 3 ▶ $url->port($self->{port} || undef);
Condition50%1/2
$self->{'port'} || undef
l!l
03
MC/DC0%0/1
$self->{'port'} || undef
$self->{'port'}
Truth table50%1/2
A
A= $self->{'port'}
Aresult
11
00
39 3 $self->{$port_key} = $server->listen([$url->to_string])->start->ports->[0];
40 }
41  
42 3 return Mojo::URL->new("https://127.0.0.1:$self->{$port_key}/");
43 }
44  
45 3 1;
46  
47 =encoding utf8
48  
49 =head1 NAME
50  
51 Mojo::UserAgent::SecureServer - Secure application server for Mojo::UserAgent
52  
53 =head1 SYNOPSIS
54  
55 # Construct from Mojo::UserAgent
56 my $ua = Mojo::UserAgent->new;
57 $ua->ca('ca.pem')->cert('cert.pem')->key('key.pem');
58 $ua->server(Mojo::UserAgent::SecureServer->from_ua($ua));
59  
60 # Construct manually
61 my $ua = Mojo::UserAgent->new;
62 my $server = Mojo::UserAgent::SecureServer->new;
63 $server->listen(Mojo::URL->new('https://127.0.0.1?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt'));
64 $ua->server($server);
65  
66 # Test::Mojo
67 my $app = Mojolicious->new;
68 $app->routes->get('/' => sub {
69 my $c = shift;
70 my $handle = Mojo::IOLoop->stream($c->tx->connection)->handle;
71 $c->render(json => {cn => $handle->peer_certificate('cn')});
72 });
73  
74 my $t = Test::Mojo->new($app);
75 $t->ua->insecure(0);
76 $t->ua->ca('t/pki/certs/ca-chain.cert.pem')
77 ->cert('t/pki/mojo.example.com.cert.pem')
78 ->key('t/pki/mojo.example.com.key.pem');
79 $t->ua->server(Mojo::UserAgent::SecureServer->from_ua($t->ua));
80  
81 $t->get_ok('/')->status_is(200)->json_is('/cn', 'mojo.example.com');
82  
83 =head1 DESCRIPTION
84  
85 L<Mojo::UserAgent::SecureServer> allows you to test your L<Mojolicious> web
86 application with custom SSL/TLS key/cert/ca.
87  
88 =head1 ATTRIBUTES
89  
90 L<Mojo::UserAgent::SecureServer> inherits all attributes from
91 L<Mojo::UserAgent::Server> and implements the following new ones.
92  
93 =head2 listen
94  
95 $url = $server->listen;
96 $server = $server->listen(Mojo::URL->new('https://127.0.0.1'));
97  
98 The base listen URL for L<Mojo::Server::Daemon> created by L</nb_url> and
99 L</url>. The "port" will be discarded, while other
100 L<Mojo::Server::Daemon/listen> parameters are kept.
101  
102 =head1 METHODS
103  
104 L<Mojo::UserAgent::SecureServer> inherits all methods from
105 L<Mojo::UserAgent::Server> and implements the following new ones.
106  
107 =head2 from_ua
108  
109 $server = Mojo::UserAgent::SecureServer->from_ua($ua);
110 $server = $server->from_ua($ua);
111  
112 Used to construct a new object and/or copy attributes from a L<Mojo::UserAgent>
113 object. Here is the long version:
114  
115 $server->app($ua->server->app);
116 $server->ioloop($ua->server->ioloop);
117 $server->listen->query->param(ca => $ua->ca);
118 $server->listen->query->param(cert => $ua->cert);
119 $server->listen->query->param(key => $ua->key);
120 $server->listen->query->param(verify => Net::SSLeay::VERIFY_PEER()) unless $ua->insecure
121  
122 =head2 nb_url
123  
124 $url = $server->nb_url;
125  
126 Get absolute L<Mojo::URL> object for server processing non-blocking requests
127 with L<Mojo::UserAgent::Server/app>.
128  
129 =head2 url
130  
131 $url = $server->url;
132  
133 Get absolute L<Mojo::URL> object for server processing non-blocking requests
134 with L<Mojo::UserAgent::Server/app>.
135  
136 =head1 AUTHOR
137  
138 Jan Henning Thorsen
139  
140 =head1 COPYRIGHT AND LICENSE
141  
142 Copyright (C) Jan Henning Thorsen.
143  
144 This program is free software, you can redistribute it and/or modify it under
145 the terms of the Artistic License version 2.0.
146  
147 =head1 SEE ALSO
148  
149 L<Mojo::UserAgent>, L<Mojo::UserAgent::Server> and L<Test::Mojo>.
150  
151 =cut
↑ Summary