Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 165 lines (141 sloc) 3.846 kB
8419b0b @stash Support IO::Handle-like responses from PSGI handlers.
authored
1 #!perl
2 use warnings;
3 use strict;
4 use Test::More tests => 30;
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;
29 return shift @{$self->{lines}};
30 }
31 sub close {}
32 }
33
34 my $APP = <<'EOAPP';
35 my $app = sub {
36 my $env = shift;
37 Test::More::pass "called app";
38 my $io = FakeIOHandle->new([
39 "line one\n",
40 "line two\n"
41 ]);
42 return [200,['Content-Type'=>'text/plain'],$io];
43 };
44 EOAPP
45
46 my $app = eval $APP;
47 ok $app, 'got an app' || diag $@;
48 $evh->psgi_request_handler($app);
49
50 returning_mock: {
51 my $cv = AE::cv;
52
53 $cv->begin;
54 my $h; $h = simple_client GET => '/', sub {
55 my ($body, $headers) = @_;
56 is $headers->{'Status'}, 200, "Response OK";
57 is $headers->{'content-type'}, 'text/plain';
58 is $body, qq(line one\nline two\n);
59 $cv->end;
60 undef $h;
61 };
62
63 $cv->recv;
64 pass "all done app 1";
65 }
66
67 my ($tempfh, $tempname) = tempfile(UNLINK=>1);
68 print $tempfh "temp line one\n";
69 print $tempfh "temp line two\n";
70 close $tempfh;
71
72 my $APP2 = <<'EOAPP';
73 my $app2 = sub {
74 my $env = shift;
75 Test::More::pass "called app2";
76 open my $io, '<', $tempname;
77 return [200,['Content-Type'=>'text/plain'],$io];
78 };
79 EOAPP
80
81 my $app2 = eval $APP2;
82 ok $app2, 'got app 2' || diag $@;
83 $evh->psgi_request_handler($app2);
84
85 returning_glob: {
86 my $cv = AE::cv;
87 $cv->begin;
88 my $h; $h = simple_client GET => '/', sub {
89 my ($body, $headers) = @_;
90 is $headers->{'Status'}, 200, "Response OK";
91 is $headers->{'content-type'}, 'text/plain';
92 is $body, qq(temp line one\ntemp line two\n);
93 $cv->end;
94 undef $h;
95 };
96 $cv->recv;
97 }
98
99 pass "all done app 2";
100
101 my $APP3 = <<'EOAPP';
102 my $app3 = sub {
103 my $env = shift;
104 Test::More::pass "called app3";
105 require IO::File;
106 my $io = IO::File->new($tempname,"r");
107 return [200,['Content-Type'=>'text/plain'],$io];
108 };
109 EOAPP
110
111 my $app3 = eval $APP3;
112 ok $app3, 'got app 3' || diag $@;
113 $evh->psgi_request_handler($app3);
114
115 returning_io_file: {
116 my $cv = AE::cv;
117 $cv->begin;
118 my $h; $h = simple_client GET => '/', sub {
119 my ($body, $headers) = @_;
120 is $headers->{'Status'}, 200, "Response OK";
121 is $headers->{'content-type'}, 'text/plain', "C-T";
122 is $body, qq(temp line one\ntemp line two\n), "body";
123 $cv->end;
124 undef $h;
125 };
126 $cv->recv;
127 }
128
129 pass "all done app 3";
130
131 {
132 open my $fh, '>:encoding(UTF-16LE)',$tempname;
133 print $fh "\x{2603}\n"; # U+2603 SNOWMAN, UTF-8: E2 98 83
134 close $fh;
135 }
136
137 my $APP4 = <<'EOAPP';
138 my $app4 = sub {
139 my $env = shift;
140 Test::More::pass "called app4";
141 open my $io, '<:encoding(UTF-16LE)',$tempname;
142 return [200,['Content-Type'=>'text/plain; charset=UTF-8'],$io];
143 };
144 EOAPP
145
146 my $app4 = eval $APP4;
147 ok $app4, 'got app 4' || diag $@;
148 $evh->psgi_request_handler($app4);
149
150 returning_perlio_layer: {
151 my $cv = AE::cv;
152 $cv->begin;
153 my $h; $h = simple_client GET => '/', sub {
154 my ($body, $headers) = @_;
155 is $headers->{'Status'}, 200, "Response OK";
156 is $headers->{'content-type'}, 'text/plain; charset=UTF-8', "C-T";
157 is decode_utf8($body), qq(\x{2603}\n), "utf8 body";
158 $cv->end;
159 undef $h;
160 };
161 $cv->recv;
162 }
163
164 pass "all done app 4";
Something went wrong with that request. Please try again.