comparison stub_status.t @ 597:66f4d8cb719e

Tests: some stub_status tests.
author Andrey Zelenkov <zelenkov@nginx.com>
date Thu, 28 May 2015 20:40:47 +0300
parents
children 868ff7169ec8
comparison
equal deleted inserted replaced
596:e36b267021e3 597:66f4d8cb719e
1 #!/usr/bin/perl
2
3 # (C) Andrey Zelenkov
4 # (C) Nginx, Inc.
5
6 # Tests for stub status module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http stub_status/)->plan(34);
26
27 $t->write_file_expand('nginx.conf', <<'EOF');
28
29 %%TEST_GLOBALS%%
30
31 daemon off;
32
33 events {
34 }
35
36 http {
37 %%TEST_GLOBALS_HTTP%%
38
39 server {
40 listen 127.0.0.1:8080;
41 server_name localhost;
42
43 add_header X-Active $connections_active;
44 add_header X-Reading $connections_reading;
45 add_header X-Writing $connections_writing;
46 add_header X-Waiting $connections_waiting;
47
48 location / { }
49 location /rate {
50 limit_rate 15;
51 }
52 location /stub {
53 stub_status;
54 }
55 }
56 }
57
58 EOF
59
60 $t->write_file('index.html', '');
61 $t->run();
62
63 ###############################################################################
64
65 my %status = status('/stub');
66 like(http_get('/stub'), qr/200 OK/, 'get request');
67 is($status{'active'}, 1, 'open connection');
68 is($status{'requests'}, 1, 'first request');
69 is($status{'accepts'}, 1, 'first request accepted');
70 is($status{'handled'}, 1, 'first request handled');
71 is($status{'writing'}, 1, 'first response');
72 is($status{'reading'}, 0, 'not reading');
73
74 # pipelined requests
75
76 my $s = http(<<EOF, start => 1);
77 GET / HTTP/1.1
78 Host: localhost
79
80 GET / HTTP/1.1
81 Host: localhost
82
83 EOF
84
85 %status = status('/stub');
86 is($status{'requests'}, 5, 'requests increased by 2');
87 is($status{'accepts'}, 4, 'accepts increased by 1');
88
89 $s->close();
90
91 # states
92
93 $s = http('', start => 1);
94
95 %status = status('/stub');
96 is($status{'active'}, 2, 'active');
97 is($status{'waiting'}, 1, 'waiting state');
98 is($status{'reading'}, 0, 'waiting state - not reading');
99 is($status{'writing'}, 1, 'waiting state - not writing');
100
101 http(<<EOF, start => 1, socket => $s);
102 GET /rate HTTP/1.0
103 EOF
104
105 %status = status('/stub');
106 is($status{'waiting'}, 0, 'reading state - not waiting');
107 is($status{'reading'}, 1, 'reading state');
108 is($status{'writing'}, 1, 'reading state - not writing');
109
110 http(<<EOF, start => 1, socket => $s);
111 Host: localhost
112
113 EOF
114
115 %status = status('/stub');
116 is($status{'waiting'}, 0, 'writing state - not waiting');
117 is($status{'reading'}, 0, 'writing state - not reading');
118 is($status{'writing'}, 2, 'writing state');
119
120 $s->close();
121
122 # head and post requests
123
124 like(http_head('/stub'), qr/200 OK/, 'head request');
125 like(http_post('/stub'), qr/405 Not Allowed/, 'post request');
126
127 # embedded variables in headers
128
129 my $r = http_get('/stub');
130 like($r, qr/X-Active: 1/, 'get - var active');
131 like($r, qr/X-Reading: 0/, 'get - var reading');
132 like($r, qr/X-Writing: 1/, 'get - var writing');
133 like($r, qr/X-Waiting: 0/, 'get - var waiting');
134
135 $r = http_head('/stub');
136 like($r, qr/X-Active: 1/, 'head - var active');
137 like($r, qr/X-Reading: 0/, 'head - var reading');
138 like($r, qr/X-Writing: 1/, 'head - var writing');
139 like($r, qr/X-Waiting: 0/, 'head - var waiting');
140 is(get_body($r), '', 'head - empty body');
141
142 $r = http_get('/');
143 like($r, qr/X-Active: 1/, 'no stub - var active');
144 like($r, qr/X-Reading: 0/, 'no stub - var reading');
145 like($r, qr/X-Writing: 1/, 'no stub - var writing');
146 like($r, qr/X-Waiting: 0/, 'no stub - var waiting');
147
148 ###############################################################################
149
150 sub get_body {
151 my ($r) = @_;
152 $r =~ /.*?\x0d\x0a?\x0d\x0a?(.*)/ms;
153 return $1;
154 }
155
156 sub http_post {
157 my ($url) = @_;
158 return http(<<EOF);
159 POST $url HTTP/1.0
160 Host: localhost
161
162 EOF
163 }
164
165 sub status {
166 my ($url) = @_;
167 my $r = http_get($url);
168
169 $r =~ /
170 Active\ connections:\ +(\d+)
171 \s+server\ accepts\ handled\ requests
172 \s+(\d+)\ +(\d+)\ +(\d+)
173 \s+Reading:\ +(\d+)
174 \s+Writing:\ +(\d+)
175 \s+Waiting:\ +(\d+)
176 /sx;
177
178 return ('active' => $1,
179 'accepts' => $2,
180 'handled' => $3,
181 'requests' => $4,
182 'reading' => $5,
183 'writing' => $6,
184 'waiting' => $7,
185 );
186 }
187
188 ###############################################################################