summaryrefslogtreecommitdiff
path: root/t/tests/success-failure.t
blob: c537627c8222f397f06169cff5bcaf078cad2ba2 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!perl 
use strict;
use warnings;
 
use IO::Async::PSGI;
 
use IO::Async::Loop;
use IO::Async::Test;
use HTTP::Request;
use HTTP::Message::PSGI;
use Plack::Util;
use Plack::Builder;
use HTTP::Exception;
 
use Test2::Bundle::Extended;
use Data::Printer;
 
my $loop = IO::Async::Loop->new;
testing_loop $loop;
 
my $app_f = sub {
    my $f = $_[0]->{'io.async.loop'}
        ->delay_future(after=>0.2);
 
    if ($_[0]->{PATH_INFO} =~ /fail/) {
        return $f->then_fail("Expected failure\n");
    }
 
    if ($_[0]->{PATH_INFO} =~ /exc/) {
        return $f->then_fail(
            HTTP::Exception::401->throw(
                status_message => 'Expected failure',
            ),
        );
    }
 
    return $f->then_done([200,[],['coderef']]);
};
 
my $psgi = IO::Async::PSGI->new({
    app => $app_f,
});
my $app = $psgi->psgi_app;
 
my $success = [200,[],['coderef']];
my $fail_500 = [ 500, array { etc; }, [ "Expected failure\n" ] ];
my $fail_401 = [ 401, array { etc; }, [ 'Expected failure' ] ];
 
my %cases = (
    success => $success,
    '/success' => $success,
    fail => $fail_500,
    '/fail' => $fail_500,
    exception => $fail_401,
    '/exception' => $fail_401,
);
 
sub run_test {
    my ($use_loop) = @_;
 
    for my $c (sort keys %cases) {
        subtest $c => sub {
            my $req = HTTP::Request->new(
                GET => "http://localhost/$c",
            )->to_psgi;
 
            $req->{'io.async.loop'}=$loop
                if $use_loop;
 
            my $raw_res = $app->($req);
            my $res;
            ref_ok(
                $raw_res,
                'CODE',
                'got a delayed response',
            );
 
            $raw_res->(sub{$res=shift});
 
            wait_for { $res } if $use_loop;
 
            Plack::Util::header_remove($res->[1],'X-Request-Id');
 
            is(
                $res,
                $cases{$c},
                'correct response',
                np $res,
            );
        }
    }
}
 
subtest 'with loop' => sub { run_test(1) };
subtest 'without loop' => sub { run_test(0) };
 
done_testing;