annotate limit_req.t @ 1961:fe6f22da53ec default tip

Tests: tests for usage of discarded body. The client_max_body_size limit should be ignored when the request body is already discarded. In HTTP/1.x, this is done by checking the r->discard_body flag when the body is being discarded, and because r->headers_in.content_length_n is 0 when it's already discarded. This, however, does not happen with HTTP/2 and HTTP/3, and therefore "error_page 413" does not work without relaxing the limit. Further, with proxy_pass, r->headers_in.content_length_n is used to determine length of the request body, and therefore is not correct if discarding of the request body isn't yet complete. While discarding the request body, r->headers_in.content_length_n contains the rest of the body to discard (or, in case of chunked request body, the rest of the current chunk to discard). Similarly, the $content_length variable uses r->headers_in.content_length if available, and also incorrect. The $content_length variable is used when proxying with fastcgi_pass, grpc_pass, and uwsgi_pass (scgi_pass uses the value calculated based on the actual request body buffers, and therefore works correctly).
author Maxim Dounin <mdounin@mdounin.ru>
date Sat, 27 Apr 2024 18:55:50 +0300
parents 62e2baa3bc60
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1364
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
24 my $t = Test::Nginx->new()->has(qw/http limit_req/)->plan(6);
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 }
1364
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
48 location /status {
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
49 limit_req zone=one burst=1 nodelay;
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
50
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
51 limit_req_status 501;
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
52 }
79
d5330d926fac Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents: 78
diff changeset
53 location /long {
d5330d926fac Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents: 78
diff changeset
54 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
55 }
103
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
56 location /fast {
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
57 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
58 }
78
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 }
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60 }
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
62 EOF
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 $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
65 $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
66 $t->write_file('fast.html', 'XtestX');
78
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 $t->run();
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 ###############################################################################
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71 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
72 http_get('/test1.html');
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73 like(http_get('/test1.html'), qr/^HTTP\/1.. 503 /m, 'request rejected');
1364
62e2baa3bc60 Tests: added limit_req_status test.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1020
diff changeset
74 like(http_get('/status.html'), qr/^HTTP\/1.. 501 /m, 'request rejected status');
103
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
75 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
76 http_get('/test1.html');
78
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77
79
d5330d926fac Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents: 78
diff changeset
78 # 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
79 # 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
80 # 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
81
d5330d926fac Tests: add test for limit_req not clearing write timeout on delay.
Maxim Dounin <mdounin@mdounin.ru>
parents: 78
diff changeset
82 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
83 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
84 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
85
103
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
86 # 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
87 # 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
88
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('/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
90
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
91 # 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
92
103
4ae2198b97ec Tests: limit_req tests for 0.8.18 changes and bug.
Maxim Dounin <mdounin@mdounin.ru>
parents: 97
diff changeset
93 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
94 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
95 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
96
78
c893908c1a44 Tests: add some limit_req module tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
97 ###############################################################################