Mercurial > hg > nginx-tests
annotate limit_req.t @ 1025:94f8cd9b41dc
Tests: stream $status tests.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 05 Sep 2016 22:57:44 +0300 |
parents | 196d33c2bb45 |
children | 62e2baa3bc60 |
rev | line source |
---|---|
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
1 #!/usr/bin/perl |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
2 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
3 # (C) Maxim Dounin |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
4 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
5 # Tests for nginx limit_req module. |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
6 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
7 ############################################################################### |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
8 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
9 use warnings; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
10 use strict; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
11 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
12 use Test::More; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
13 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
14 BEGIN { use FindBin; chdir($FindBin::Bin); } |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
15 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
16 use lib 'lib'; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
17 use Test::Nginx; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
18 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
19 ############################################################################### |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
20 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
21 select STDERR; $| = 1; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
22 select STDOUT; $| = 1; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
23 |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
24 my $t = Test::Nginx->new()->has(qw/http limit_req/)->plan(5); |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
25 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
26 $t->write_file_expand('nginx.conf', <<'EOF'); |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
27 |
107
1c0ec30614c6
Tests: add TEST_GLOBALS and TEST_GLOBALS_HTTP config chunks.
Maxim Dounin <mdounin@mdounin.ru>
parents:
103
diff
changeset
|
28 %%TEST_GLOBALS%% |
1c0ec30614c6
Tests: add TEST_GLOBALS and TEST_GLOBALS_HTTP config chunks.
Maxim Dounin <mdounin@mdounin.ru>
parents:
103
diff
changeset
|
29 |
249
6a0d934950bc
Tests: remove extra spaces in "daemon off".
Maxim Dounin <mdounin@mdounin.ru>
parents:
223
diff
changeset
|
30 daemon off; |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
31 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
32 events { |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
33 } |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
34 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
35 http { |
107
1c0ec30614c6
Tests: add TEST_GLOBALS and TEST_GLOBALS_HTTP config chunks.
Maxim Dounin <mdounin@mdounin.ru>
parents:
103
diff
changeset
|
36 %%TEST_GLOBALS_HTTP%% |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
37 |
503
071e8941e3bf
Tests: reduce shared memory zone sizes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
249
diff
changeset
|
38 limit_req_zone $binary_remote_addr zone=one:1m rate=2r/s; |
071e8941e3bf
Tests: reduce shared memory zone sizes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
249
diff
changeset
|
39 limit_req_zone $binary_remote_addr zone=long:1m rate=2r/s; |
071e8941e3bf
Tests: reduce shared memory zone sizes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
249
diff
changeset
|
40 limit_req_zone $binary_remote_addr zone=fast:1m rate=1000r/s; |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
41 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
42 server { |
974
882267679006
Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
952
diff
changeset
|
43 listen 127.0.0.1:8080; |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
44 server_name localhost; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
45 location / { |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
46 limit_req zone=one burst=1 nodelay; |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
47 } |
79
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
48 location /long { |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
49 limit_req zone=long burst=5; |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
50 } |
103
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
51 location /fast { |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
52 limit_req zone=fast burst=1; |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
53 } |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
54 } |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
55 } |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
56 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
57 EOF |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
58 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
59 $t->write_file('test1.html', 'XtestX'); |
79
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
60 $t->write_file('long.html', "1234567890\n" x (1 << 16)); |
103
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
61 $t->write_file('fast.html', 'XtestX'); |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
62 $t->run(); |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
63 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
64 ############################################################################### |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
65 |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
66 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'request'); |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
67 http_get('/test1.html'); |
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
68 like(http_get('/test1.html'), qr/^HTTP\/1.. 503 /m, 'request rejected'); |
103
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
69 http_get('/test1.html'); |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
70 http_get('/test1.html'); |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
71 |
79
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
72 # Second request will be delayed by limit_req, make sure it isn't truncated. |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
73 # The bug only manifests itself if buffer will be filled, so sleep for a while |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
74 # before reading response. |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
75 |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
76 my $l1 = length(http_get('/long.html')); |
172
d099e407fff5
Tests: tune sleep in limit_req.t to avoid spurious timeouts.
Maxim Dounin <mdounin@mdounin.ru>
parents:
166
diff
changeset
|
77 my $l2 = length(http_get('/long.html', sleep => 0.6)); |
79
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
78 is($l2, $l1, 'delayed big request not truncated'); |
d5330d926fac
Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents:
78
diff
changeset
|
79 |
103
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
80 # make sure rejected requests are not counted, and access is again allowed |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
81 # after 1/rate seconds |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
82 |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
83 like(http_get('/test1.html'), qr/^HTTP\/1.. 200 /m, 'rejects not counted'); |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
84 |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
85 # make sure negative excess values are handled properly |
113
5d31f920eda9
Tests: whitespace cleanup, no functional changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
107
diff
changeset
|
86 |
103
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
87 http_get('/fast.html'); |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
88 select undef, undef, undef, 0.1; |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
89 like(http_get('/fast.html'), qr/^HTTP\/1.. 200 /m, 'negative excess'); |
4ae2198b97ec
Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents:
97
diff
changeset
|
90 |
78
c893908c1a44
Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
91 ############################################################################### |