Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 168 lines (144 sloc) 3.986 kB
8419b0b @stash Support IO::Handle-like responses from PSGI handlers.
authored
1 #!perl
2 use warnings;
3 use strict;
529fbd7 @stash Fix io::handle-read problems exposed during plack-suite testing
authored
4 use Test::More tests => 34;
8419b0b @stash Support IO::Handle-like responses from PSGI handlers.
authored
5 use lib 't'; use Utils;
6 use File::Temp qw/tempfile/;
7 use Encode qw/decode_utf8/;
8
9 BEGIN { use_ok('Feersum') };
10
11 my ($socket,$port) = get_listen_socket();
12 ok $socket, "made listen socket";
13
14 my $evh = Feersum->new();
15 {
16 no warnings 'redefine';
17 *Feersum::DIED = sub {
18 my $err = shift;
19 fail "Died during request handler: $err";
20 };
21 }
22 $evh->use_socket($socket);
23
24 {
25 package FakeIOHandle;
26 sub new { return bless {lines => $_[1]}, __PACKAGE__ }
27 sub getline {
28 my $self = shift;
5d2d6ce @stash Move IO::Handle pumper into XS
authored
29 Test::More::ok(ref($/) && ${$/} == 4096, '$/ is \4096');
529fbd7 @stash Fix io::handle-read problems exposed during plack-suite testing
authored
30 return @{$self->{lines}} ? shift @{$self->{lines}} : undef;
31 }
32 sub close {
33 Test::More::pass("called close");
8419b0b @stash Support IO::Handle-like responses from PSGI handlers.
authored
34 }
35 }
36
37 my $APP = <<'EOAPP';
38 my $app = sub {
39 my $env = shift;
40 Test::More::pass "called app";
41 my $io = FakeIOHandle->new([
42 "line one\n",
43 "line two\n"
44 ]);
45 return [200,['Content-Type'=>'text/plain'],$io];
46 };
47 EOAPP
48
49 my $app = eval $APP;
50 ok $app, 'got an app' || diag $@;
51 $evh->psgi_request_handler($app);
52
53 returning_mock: {
54 my $cv = AE::cv;
55
56 $cv->begin;
57 my $h; $h = simple_client GET => '/', sub {
58 my ($body, $headers) = @_;
59 is $headers->{'Status'}, 200, "Response OK";
60 is $headers->{'content-type'}, 'text/plain';
61 is $body, qq(line one\nline two\n);
62 $cv->end;
63 undef $h;
64 };
65
66 $cv->recv;
67 pass "all done app 1";
68 }
69
70 my ($tempfh, $tempname) = tempfile(UNLINK=>1);
71 print $tempfh "temp line one\n";
72 print $tempfh "temp line two\n";
73 close $tempfh;
74
75 my $APP2 = <<'EOAPP';
76 my $app2 = sub {
77 my $env = shift;
78 Test::More::pass "called app2";
79 open my $io, '<', $tempname;
80 return [200,['Content-Type'=>'text/plain'],$io];
81 };
82 EOAPP
83
84 my $app2 = eval $APP2;
85 ok $app2, 'got app 2' || diag $@;
86 $evh->psgi_request_handler($app2);
87
88 returning_glob: {
89 my $cv = AE::cv;
90 $cv->begin;
91 my $h; $h = simple_client GET => '/', sub {
92 my ($body, $headers) = @_;
93 is $headers->{'Status'}, 200, "Response OK";
94 is $headers->{'content-type'}, 'text/plain';
95 is $body, qq(temp line one\ntemp line two\n);
96 $cv->end;
97 undef $h;
98 };
99 $cv->recv;
100 }
101
102 pass "all done app 2";
103
104 my $APP3 = <<'EOAPP';
105 my $app3 = sub {
106 my $env = shift;
107 Test::More::pass "called app3";
108 require IO::File;
109 my $io = IO::File->new($tempname,"r");
110 return [200,['Content-Type'=>'text/plain'],$io];
111 };
112 EOAPP
113
114 my $app3 = eval $APP3;
115 ok $app3, 'got app 3' || diag $@;
116 $evh->psgi_request_handler($app3);
117
118 returning_io_file: {
119 my $cv = AE::cv;
120 $cv->begin;
121 my $h; $h = simple_client GET => '/', sub {
122 my ($body, $headers) = @_;
123 is $headers->{'Status'}, 200, "Response OK";
124 is $headers->{'content-type'}, 'text/plain', "C-T";
125 is $body, qq(temp line one\ntemp line two\n), "body";
126 $cv->end;
127 undef $h;
128 };
129 $cv->recv;
130 }
131
132 pass "all done app 3";
133
134 {
135 open my $fh, '>:encoding(UTF-16LE)',$tempname;
136 print $fh "\x{2603}\n"; # U+2603 SNOWMAN, UTF-8: E2 98 83
137 close $fh;
138 }
139
140 my $APP4 = <<'EOAPP';
141 my $app4 = sub {
142 my $env = shift;
143 Test::More::pass "called app4";
144 open my $io, '<:encoding(UTF-16LE)',$tempname;
145 return [200,['Content-Type'=>'text/plain; charset=UTF-8'],$io];
146 };
147 EOAPP
148
149 my $app4 = eval $APP4;
150 ok $app4, 'got app 4' || diag $@;
151 $evh->psgi_request_handler($app4);
152
153 returning_perlio_layer: {
154 my $cv = AE::cv;
155 $cv->begin;
156 my $h; $h = simple_client GET => '/', sub {
157 my ($body, $headers) = @_;
158 is $headers->{'Status'}, 200, "Response OK";
159 is $headers->{'content-type'}, 'text/plain; charset=UTF-8', "C-T";
160 is decode_utf8($body), qq(\x{2603}\n), "utf8 body";
161 $cv->end;
162 undef $h;
163 };
164 $cv->recv;
165 }
166
167 pass "all done app 4";
Something went wrong with that request. Please try again.