-
Notifications
You must be signed in to change notification settings - Fork 15
/
09-magic.t
126 lines (114 loc) · 3.07 KB
/
09-magic.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
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
115
116
117
118
119
120
121
122
123
124
125
126
#!perl
use warnings;
use strict;
use Test::More tests => 25;
use Test::Exception;
use utf8;
use lib 't'; use Utils;
BEGIN { use_ok('Feersum') };
my ($socket,$port) = get_listen_socket();
ok $socket, "made listen socket";
ok $socket->fileno, "has a fileno";
my $evh = Feersum->new();
$evh->use_socket($socket);
{
package My::MagicScalar;
use Tie::Scalar;
use base 'Tie::StdScalar';
sub FETCH {
my $self = shift;
return uc($self->SUPER::FETCH(@_));
}
}
{
package My::MagicArray;
use Tie::Array;
use base 'Tie::StdArray';
sub FETCH {
my $self = shift;
my $e = $self->SUPER::FETCH(@_);
return ref($e) ? $e : uc($e);
}
sub SHIFT {
my $self = shift;
my $e = $self->SUPER::SHIFT(@_);
return ref($e) ? $e : uc($e);
}
}
$evh->request_handler(sub {
my $r = shift;
isa_ok $r, 'Feersum::Connection', 'got an object!';
my $env = $r->env();
ok $env, "got env";
my $type = $env->{HTTP_X_MAGIC_TYPE};
if ($type eq 'SCALAR') {
# magic scalar
tie my $ms, 'My::MagicScalar';
$ms = "foobar";
lives_ok(sub {
$r->send_response("200 OK", [
'Content-Type' => 'text/plain',
], \$ms);
}, "sent response for $type");
}
elsif ($type eq 'ARRAY') {
# magic array
tie my @ma, 'My::MagicArray';
@ma = ("aaaa","bbb");
lives_ok(sub {
$r->send_response("200 OK", [
'Content-Type' => 'text/plain',
], \@ma);
}, "sent response for $type");
}
else {
tie my $ms, 'My::MagicScalar';
$ms = "dddd";
tie my @ma, 'My::MagicArray';
@ma = ("cccc",\$ms);
lives_ok(sub {
$r->send_response("200 OK", [
'Content-Type' => 'text/plain',
], \@ma);
}, "sent response for $type");
}
});
my $cv = AE::cv;
$cv->begin;
my $w = simple_client GET => '/',
name => 'scalar',
headers => { 'X-Magic-Type' => 'SCALAR' },
timeout => 3,
sub {
my ($body, $hdr) = @_;
is $hdr->{Status}, 200, "client 1 got 200";
is $hdr->{'content-length'}, 6, 'content-length was overwritten by the engine';
is $body, 'FOOBAR', "magic body used for scalar";
$cv->end;
};
$cv->begin;
my $w2 = simple_client GET => '/',
name => 'array',
headers => { 'X-Magic-Type' => 'ARRAY' },
timeout => 3,
sub {
my ($body, $hdr) = @_;
is $hdr->{Status}, 200, "client 1 got 200";
is $hdr->{'content-length'}, 7, 'content-length';
is $body, 'AAAABBB', "magic body used for array";
$cv->end;
};
$cv->begin;
my $w3 = simple_client GET => '/',
name => 'array',
headers => { 'X-Magic-Type' => 'SCALAR-in-ARRAY' },
timeout => 3,
sub {
my ($body, $hdr) = @_;
is $hdr->{Status}, 200, "client 1 got 200";
is $hdr->{'content-length'}, 8, 'content-length';
is $body, 'CCCCDDDD', "magic body used for scalar in array";
$cv->end;
};
$cv->recv;
pass "all done";