comparison limit_conn.t @ 313:9daf28f20766

Tests: added tests for limit_conn module.
author Sergey Kandaurov <pluknet@nginx.com>
date Mon, 15 Jul 2013 13:06:42 +0400
parents
children 68b94b83412b
comparison
equal deleted inserted replaced
312:b639e76ba923 313:9daf28f20766
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4
5 # limit_req based tests for nginx limit_conn module.
6
7 ###############################################################################
8
9 use warnings;
10 use strict;
11
12 use Test::More;
13
14 use Socket qw/ CRLF /;
15
16 BEGIN { use FindBin; chdir($FindBin::Bin); }
17
18 use lib 'lib';
19 use Test::Nginx;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 plan(skip_all => 'win32') if $^O eq 'MSWin32';
27
28 my $t = Test::Nginx->new()->has(qw/http proxy limit_conn limit_req/)->plan(10);
29
30 $t->write_file_expand('nginx.conf', <<'EOF');
31
32 %%TEST_GLOBALS%%
33
34 daemon off;
35
36 events {
37 }
38
39 http {
40 %%TEST_GLOBALS_HTTP%%
41
42 limit_req_zone $binary_remote_addr zone=req:1m rate=1r/m;
43
44 limit_conn_zone $binary_remote_addr zone=zone:1m;
45 limit_conn_zone $binary_remote_addr zone=zone2:1m;
46 limit_conn_zone $binary_remote_addr zone=custom:1m;
47 limit_zone legacy $binary_remote_addr 1m;
48
49 server {
50 listen 127.0.0.1:8081;
51 server_name localhost;
52
53 location /w {
54 limit_req zone=req burst=10;
55 }
56 }
57
58 server {
59 listen 127.0.0.1:8080;
60 server_name localhost;
61
62 location / {
63 proxy_pass http://127.0.0.1:8081;
64 limit_conn zone 1;
65 }
66
67 location /1 {
68 limit_conn zone 1;
69 }
70
71 location /zone {
72 limit_conn zone2 1;
73 }
74
75 location /unlim {
76 limit_conn zone 5;
77 }
78
79 location /custom {
80 proxy_pass http://127.0.0.1:8081/;
81 limit_conn_log_level info;
82 limit_conn_status 501;
83 limit_conn custom 1;
84 }
85
86 location /legacy {
87 proxy_pass http://127.0.0.1:8081/;
88 limit_conn legacy 1;
89 }
90 }
91 }
92
93 EOF
94
95 $t->run();
96
97 ###############################################################################
98
99 # charge limit_req
100
101 http_get('/w');
102
103 # same and other zones in different locations
104
105 my $s = http_start('/w');
106 like(http_get('/'), qr/^HTTP\/1.. 503 /, 'rejected');
107 like(http_get('/1'), qr/^HTTP\/1.. 503 /, 'rejected different location');
108 unlike(http_get('/zone'), qr/^HTTP\/1.. 503 /, 'passed different zone');
109
110 close $s;
111 unlike(http_get('/1'), qr/^HTTP\/1.. 503 /, 'passed');
112
113 # custom error code and log level
114
115 $s = http_start('/custom/w');
116 like(http_get('/custom'), qr/^HTTP\/1.. 501 /, 'limit_conn_status');
117
118 like(`grep -F '[info]' ${\($t->testdir())}/error.log`,
119 qr/limiting connections by zone "custom"/s,
120 'limit_conn_log_level');
121
122 # limit_zone
123
124 $s = http_start('/legacy/w');
125 like(http_get('/legacy'), qr/^HTTP\/1.. 503 /, 'legacy rejected');
126
127 $s->close;
128 unlike(http_get('/legacy'), qr/^HTTP\/.. 503 /, 'legacy passed');
129
130 # limited after unlimited
131
132 $s = http_start('/w');
133 like(http_get('/unlim'), qr/404 Not Found/, 'unlimited passed');
134 like(http_get('/'), qr/503 Service/, 'limited rejected');
135
136 ###############################################################################
137
138 sub http_start {
139 my ($uri) = @_;
140
141 my $s;
142 my $request = "GET $uri HTTP/1.0" . CRLF . CRLF;
143
144 eval {
145 local $SIG{ALRM} = sub { die "timeout\n" };
146 local $SIG{PIPE} = sub { die "sigpipe\n" };
147 alarm(3);
148 $s = IO::Socket::INET->new(
149 Proto => 'tcp',
150 PeerAddr => '127.0.0.1:8080'
151 );
152 log_out($request);
153 $s->print($request);
154 alarm(0);
155 };
156 alarm(0);
157 if ($@) {
158 log_in("died: $@");
159 return undef;
160 }
161 return $s;
162 }
163
164 ###############################################################################