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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);
use Test::More tests => 45;
use Encode qw(decode encode);
my $temp_dir;
BEGIN {
# Подготовка объекта тестирования для работы с utf8
my $builder = Test::More->builder;
binmode $builder->output, ":utf8";
binmode $builder->failure_output, ":utf8";
binmode $builder->todo_output, ":utf8";
use_ok 'Coro::Twiggy';
use_ok 'File::Temp', 'tempdir';
use_ok 'File::Path', 'rmtree';
use_ok 'File::Spec::Functions', 'catfile';
use_ok 'Coro';
use_ok 'Coro::AnyEvent';
use_ok 'Coro::Handle';
use_ok 'AnyEvent::Socket';
use_ok 'AnyEvent';
}
$temp_dir = tempdir;
ok -d $temp_dir, "-d $temp_dir";
my $socket = catfile $temp_dir, 'socket';
{
my $server = Coro::Twiggy->new(host => 'unix/', port => $socket);
isa_ok $server => 'Coro::Twiggy';
for( 1 .. 10 ) {
Coro::AnyEvent::sleep 0.1;
last if -S $socket;
}
ok -S $socket, "-S $socket - socket was opened";
tcp_connect 'unix/', $socket, Coro::rouse_cb;
my $cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
my $resp;
{ local $/; $resp = <$cs> }
ok $resp, "response";
like $resp, qr{^HTTP/1\.[01]\s+503}, 'code';
like $resp, qr{no registered PSGI service}, 'message';
my $env;
$server->register_service(sub { ($env) = @_; });
tcp_connect 'unix/', $socket, Coro::rouse_cb;
$cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
{ local $/; $resp = <$cs> }
ok $resp, "response";
like $resp, qr{^HTTP/1\.[01]\s+500}, 'code';
like $resp, qr{application have to return an ARRAYREF}, 'message';
ok $env, 'PSGI application was called';
$env = undef;
$server->register_service(sub { ($env) = @_; ['abc'] });
tcp_connect 'unix/', $socket, Coro::rouse_cb;
$cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
{ local $/; $resp = <$cs> }
ok $resp, "response";
like $resp, qr{^HTTP/1\.[01]\s+500}, 'code';
like $resp, qr{wrong response}, 'message';
ok $env, 'PSGI application was called';
$env = undef;
$server->register_service(sub {
($env) = @_;
[200, ['Content-Type', 'text/plain'], ['test passed']]
});
tcp_connect 'unix/', $socket, Coro::rouse_cb;
$cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
{ local $/; $resp = <$cs> }
ok $resp, "response";
like $resp, qr{^HTTP/1\.[01]\s+200}, 'code';
like $resp, qr{test passed}, 'message';
ok $env, 'PSGI application was called';
my $started = AnyEvent::now();
$env = undef;
$server->register_service(sub {
($env) = @_;
Coro::AnyEvent::sleep .5;
[200, ['Content-Type', 'text/plain'], ['test passed']]
});
tcp_connect 'unix/', $socket, Coro::rouse_cb;
$cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
{ local $/; $resp = <$cs> }
ok $resp, "response";
like $resp, qr{^HTTP/1\.[01]\s+200}, 'code';
like $resp, qr{test passed}, 'message';
ok $env, 'PSGI application was called';
my $delay = AnyEvent::now() - $started;
cmp_ok $delay, '>=', 0.5, 'async process took more that 0.5 seconds';
$server->register_service(sub { die "привет" });
tcp_connect 'unix/', $socket, Coro::rouse_cb;
$cs = unblock +(Coro::rouse_wait)[0];
ok $cs, 'connected to server';
print $cs "GET / HTTP/1.0\015\012\015\12";
{ local $/; $resp = <$cs> }
ok $resp, "response";
ok eval { utf8::decode $resp }, 'response was decoded';
like $resp, qr{^HTTP/1\.[01]\s+500}, 'code';
like $resp, qr{привет at}, 'message';
ok $env, 'PSGI application was called';
}
Coro::AnyEvent::sleep 0.5;
my ($resp, $env);
tcp_connect 'unix/', $socket, Coro::rouse_cb;
my $cs = Coro::rouse_wait;
ok !$cs, 'Socket was closed';
END {
if ($temp_dir) {
rmtree $temp_dir;
ok !-d $temp_dir, "!-d $temp_dir";
}
}
|