File: basic.t

package info (click to toggle)
libtwiggy-tls-perl 0.0020-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 164 kB
  • sloc: perl: 1,516; makefile: 2
file content (62 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::TCP;
use Plack::Loader;
use LWP::UserAgent;
use FindBin '$Bin';
use Socket;

my $host       = "localhost";
my $ca_cert    = "$Bin/ca.pem";
my $server_pem = "$Bin/server.pem";

subtest 'tls connection' => sub {
    my ($success, $content);

    test_tcp(
        client => sub {
            my $port = shift;

            alarm 2;
            local $SIG{ALRM} = sub {die};

            my $ua =
              LWP::UserAgent->new(
                ssl_opts => {verify_hostname => 1, SSL_ca_file => $ca_cert});
            my $res = $ua->get("https://$host:$port");
            $success = $res->is_success or die $res->status_line;
            $content = $res->decoded_content;
        },
        server => sub {
            my $port   = shift;
            my $server = Plack::Loader->load(
                'Twiggy::TLS',
                host     => inet_ntoa(inet_aton($host)),
                port     => $port,
                tls_key  => $server_pem,
                tls_cert => $server_pem,
            );

            $server->run(
                sub {
                    my $env = shift;

                    return [
                        200,
                        ['Content-Type' => 'text/plain'],
                        [$env->{"psgi.url_scheme"}]
                    ];
                }
            );
        }
    );

    ok $success, "https connection success";
    is $content, "https", "returned content is right";
};

done_testing;