annotate limit_conn_complex.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 196d33c2bb45
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # limit_req based tests for limit_conn module with complex keys.
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 use IO::Select;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19 use lib 'lib';
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 use Test::Nginx;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 ###############################################################################
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24 select STDERR; $| = 1;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 select STDOUT; $| = 1;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
1020
196d33c2bb45 Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 974
diff changeset
27 my $t = Test::Nginx->new()->has(qw/http proxy limit_conn limit_req/)
574
2cd00179f4b2 Tests: has_feature() introduced in Test::Nginx.
Sergey Kandaurov <pluknet@nginx.com>
parents: 568
diff changeset
28 ->plan(4);
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30 $t->write_file_expand('nginx.conf', <<'EOF');
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32 %%TEST_GLOBALS%%
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 daemon off;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 events {
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 http {
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 %%TEST_GLOBALS_HTTP%%
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41
826
64abcc611242 Tests: adjusted limit_conn_complex.t test timeouts.
Sergey Kandaurov <pluknet@nginx.com>
parents: 792
diff changeset
42 limit_req_zone $binary_remote_addr$arg_r zone=req:1m rate=1r/m;
64abcc611242 Tests: adjusted limit_conn_complex.t test timeouts.
Sergey Kandaurov <pluknet@nginx.com>
parents: 792
diff changeset
43 limit_req_zone $binary_remote_addr zone=re2:1m rate=1r/m;
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 limit_conn_zone $binary_remote_addr$arg_c zone=conn:1m;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 server {
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 968
diff changeset
47 listen 127.0.0.1:8080;
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 server_name localhost;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 location / {
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 limit_conn conn 1;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52 }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 location /w {
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 limit_conn conn 1;
974
882267679006 Tests: simplified parallel modifications in tests.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 968
diff changeset
56 proxy_pass http://127.0.0.1:8080/req2;
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59 location /req {
477
c94fc8e41f5f Tests: adjusted limit_conn_complex.t tests.
Sergey Kandaurov <pluknet@nginx.com>
parents: 475
diff changeset
60 limit_req zone=req burst=2;
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 }
725
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
62
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
63 location /req2 {
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
64 limit_req zone=re2 burst=2;
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
65 }
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 }
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69 EOF
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70
968
ce687b25ea49 Tests: avoid spurious timeout in limit_conn_complex.t.
Sergey Kandaurov <pluknet@nginx.com>
parents: 952
diff changeset
71 $t->write_file('req', '');
568
907e89fba9c3 Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 477
diff changeset
72 $t->run();
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 ###############################################################################
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 my $s;
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
77
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78 # charge limit_req
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80 http_get('/req');
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82 # limit_req tests
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 $s = http_get('/req', start => 1);
826
64abcc611242 Tests: adjusted limit_conn_complex.t test timeouts.
Sergey Kandaurov <pluknet@nginx.com>
parents: 792
diff changeset
85 ok(!IO::Select->new($s)->can_read(1), 'limit_req same key');
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86
968
ce687b25ea49 Tests: avoid spurious timeout in limit_conn_complex.t.
Sergey Kandaurov <pluknet@nginx.com>
parents: 952
diff changeset
87 like(http_get('/req?r=2'), qr/200 OK/, 'limit_req different key');
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89 # limit_conn tests
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90
725
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
91 http_get('/req2');
e5b1495299bd Tests: made sure to test limit_conn with rate limited request.
Sergey Kandaurov <pluknet@nginx.com>
parents: 574
diff changeset
92
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 $s = http_get('/w', start => 1);
477
c94fc8e41f5f Tests: adjusted limit_conn_complex.t tests.
Sergey Kandaurov <pluknet@nginx.com>
parents: 475
diff changeset
94 select undef, undef, undef, 0.2;
475
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
96 like(http_get('/'), qr/^HTTP\/1.. 503 /, 'limit_conn same key');
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
97 unlike(http_get('/?c=2'), qr/^HTTP\/1.. 503 /, 'limit_conn different key');
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
98
c872b2c9645f Tests: tests for limit_conn and limit_req with complex value.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
99 ###############################################################################