-
Notifications
You must be signed in to change notification settings - Fork 1
/
40_streaming.t
81 lines (63 loc) · 1.46 KB
/
40_streaming.t
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
use strict;
use Test::More;
use Plack::Test;
use Plack::Builder;
use Plack::Middleware::Cached;
use HTTP::Request::Common;
use lib 't/';
require 'mock-cache';
my $cache = Mock::Cache->new;
my $counter = 1;
my $capp = sub {
my ($app, @args) = @_;
builder {
enable 'Cached', cache => $cache, @args;
$app;
};
};
my $app_static = sub {
my ($env) = @_;
$env->{counter} = $counter;
[ 200, [ 'Content-Type' => 'text/plain' ], [ $counter++ ] ]
};
test_psgi $capp->($app_static), sub {
my $cb = shift;
my $res = $cb->(GET "/a");
is $res->code, 200;
is $res->content, 1;
} foreach (1..2);
my $app_delayed = sub {
my ($app) = @_;
return sub {
my ($env) = @_;
return sub {
my ($responder) = @_;
$responder->( $app->() );
};
};
};
test_psgi $capp->($app_delayed->($app_static)), sub {
my $cb = shift;
my $res = $cb->(GET "/b");
is $res->code, 200;
is $res->content, 2;
} foreach (1..2);
my $app_streaming = sub {
return sub {
my ($responder) = @_;
my $writer = $responder->(
[ 200, [ 'Content-Type' => 'text/plain' ] ]
);
$writer->write($counter);
$writer->write($counter);
$writer->close;
return;
};
};
test_psgi $capp->($app_streaming), sub {
my $cb = shift;
my $res = $cb->(GET "/c");
is $res->code, 200;
is $res->content, 33;
} foreach (1..2);
done_testing;