File: daemon.t

package info (click to toggle)
libmojolicious-perl 0.999926-1%2Bsqueeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 949
  • sloc: perl: 17,391; makefile: 4
file content (151 lines) | stat: -rw-r--r-- 5,349 bytes parent folder | download
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
148
149
150
151
#!/usr/bin/env perl

# Copyright (C) 2008-2010, Sebastian Riedel.

use strict;
use warnings;

use Test::More;

use Mojo::Client;
use Mojo::Transaction::HTTP;
use Test::Mojo::Server;

plan skip_all => 'set TEST_DAEMON to enable this test (developer only!)'
  unless $ENV{TEST_DAEMON};
plan tests => 46;

# Daddy, I'm scared. Too scared to even wet my pants.
# Just relax and it'll come, son.
use_ok('Mojo::Server::Daemon');

# Test sane Mojo::Server subclassing capabilities
my $daemon = Mojo::Server::Daemon->new;
my $max    = $daemon->max_clients;
$daemon = Mojo::Server::Daemon->new(max_clients => $max + 10);
is($daemon->max_clients, $max + 10, 'right max clients value');

# Start
my $server = Test::Mojo::Server->new;
$server->start_daemon_ok('server started');
my $port   = $server->port;
my $client = Mojo::Client->new;

# Single request without keep alive
my $tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/0/");
$tx->req->headers->connection('close');
$client->process($tx);
is($tx->state,     'done', 'right state');
is($tx->res->code, 200,    'right status');
like($tx->res->headers->connection, qr/close/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');

# Pipelined with 100 Continue
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/1/");
my $tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse("http://127.0.0.1:$port/2/");
$tx2->req->headers->expect('100-continue');
$tx2->req->body('foo bar baz');
my $tx3 = Mojo::Transaction::HTTP->new;
$tx3->req->method('GET');
$tx3->req->url->parse("http://127.0.0.1:$port/3/");
my $tx4 = Mojo::Transaction::HTTP->new;
$tx4->req->method('GET');
$tx4->req->url->parse("http://127.0.0.1:$port/4/");
$client->process([$tx, $tx2, $tx3, $tx4]);
ok($tx->is_done,  'state is done');
ok($tx2->is_done, 'state is done');
ok($tx3->is_done, 'state is done');
ok($tx4->is_done, 'state is done');
is($tx->res->code,  200, 'right status');
is($tx2->res->code, 200, 'right status');
is($tx2->continued, 1,   'transaction was continued');
is($tx3->res->code, 200, 'right status');
is($tx4->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');

# 100 Continue request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/5/");
$tx->req->headers->expect('100-continue');
$tx->req->body('Hello Mojo!');
$client->process($tx);
is($tx->res->code, 200, 'right status');
is($tx->continued, 1,   'transaction was continued');
like($tx->res->headers->connection,
    qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');

# Second keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/6/");
$client->process($tx);
is($tx->res->code,  200, 'right status');
is($tx->kept_alive, 1,   'connection was kept alive');
like($tx->res->headers->connection,
    qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');

# Third keep alive request
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/7/");
$client->process($tx);
is($tx->res->code,  200, 'right status');
is($tx->kept_alive, 1,   'connection was kept alive');
like($tx->res->headers->connection,
    qr/Keep-Alive/i, 'right "Connection" header');
like($tx->res->body, qr/Mojo is working/, 'right content');

# Pipelined
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/8/");
$tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse("http://127.0.0.1:$port/9/");
$client->process([$tx, $tx2]);
ok($tx->is_done,  'state is done');
ok($tx2->is_done, 'state is done');
is($tx->res->code,  200, 'right status');
is($tx2->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');

# Pipelined with 100 Continue and a chunked response
$tx = Mojo::Transaction::HTTP->new;
$tx->req->method('GET');
$tx->req->url->parse("http://127.0.0.1:$port/10/");
$tx2 = Mojo::Transaction::HTTP->new;
$tx2->req->method('GET');
$tx2->req->url->parse("http://127.0.0.1:$port/11/");
$tx2->req->headers->expect('100-continue');
$tx2->req->body('foo bar baz');
$tx3 = Mojo::Transaction::HTTP->new;
$tx3->req->method('GET');
$tx3->req->url->parse(
    "http://127.0.0.1:$port/diag/chunked_params?a=foo&b=12");
$tx4 = Mojo::Transaction::HTTP->new;
$tx4->req->method('GET');
$tx4->req->url->parse("http://127.0.0.1:$port/13/");
$client->process([$tx, $tx2, $tx3, $tx4]);
ok($tx->is_done,  'state is done');
ok($tx2->is_done, 'state is done');
ok($tx3->is_done, 'state is done');
ok($tx4->is_done, 'state is done');
is($tx->res->code,  200, 'right status');
is($tx2->res->code, 200, 'right status');
is($tx2->continued, 1,   'transaction was continued');
is($tx3->res->code, 200, 'right status');
is($tx4->res->code, 200, 'right status');
like($tx2->res->content->asset->slurp, qr/Mojo is working/, 'right content');
is($tx3->res->content->asset->slurp, 'foo12', 'right content');

# Stop
$server->stop_server_ok('server stopped');