/
Test.pm6
115 lines (94 loc) · 3.31 KB
/
Test.pm6
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use v6;
use HTTP::Supply::Test;
unit class HTTP::Supply::Response::Test is HTTP::Supply::Test;
use Test;
use HTTP::Supply::Response;
method run-test($resps, @expected is copy, :%quits) {
my @processing-resps;
# capture test results in closures for later final evaluation
my @output;
react {
whenever $resps -> @res {
my @exp := try { @expected.shift } // @();
CATCH {
default {
.note; .rethrow;
}
}
@output.push: {
flunk 'unexpected response received: ', @res.perl
without @exp;
};
my $code = @res[0];
my @headers := @res[1];
my $output = @res[2];
my @trailers = @exp.elems > 3 ?? |@exp[3] !! ();
@output.push: {
self.headers-equivalent: @headers, @exp[1];
};
my $acc = buf8.new;
push @processing-resps, start {
react {
whenever $output -> $chunk {
given $chunk {
when Blob { $acc ~= $chunk }
when List {
self.headers-equivalent($chunk, @trailers, :test<trailers>);
@trailers := ();
}
default {
@output.push: { flunk 'unknown body output' };
}
}
LAST {
@output.push: {
is $acc.decode('utf8'), @exp[2], 'message body looks good';
flunk 'trailers were not received' if @trailers;
};
done;
}
QUIT {
when %quits<body> {
@output.push: {
pass 'Body quit on expected error.';
}
}
default {
.note;
@output.push: {
flunk 'Body quit on expected error.';
}
}
}
}
}
}
LAST {
if %quits<on> :exists {
@output.push: {
flunk "Quit on expected error.";
}
};
done
}
QUIT {
when %quits<on> {
@output.push: {
pass "Quit on expected error.";
}
}
default {
.note;
@output.push: { flunk $_ };
}
}
}
}
self.await-or-timeout(@processing-resps, :message<processing test responses>);
# emit test results in order, single threaded
for @output -> $test-ok {
$test-ok.();
}
is @expected.elems, 0, 'last request received, no more expected?';
}
method test-class { HTTP::Supply::Response }