annotate limit_conn_dry_run.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 5ac6efbe5552
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1532
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
1 #!/usr/bin/perl
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
2
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
3 # (C) Sergey Kandaurov
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
4 # (C) Nginx, Inc.
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
5
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
6 # Tests for limit_conn_dry_run directive, limit_conn_status variable.
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
7
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
8 ###############################################################################
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
9
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
10 use warnings;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
11 use strict;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
12
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
13 use Test::More;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
14
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
16
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
17 use lib 'lib';
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
18 use Test::Nginx;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
19
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
20 ###############################################################################
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
21
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
22 select STDERR; $| = 1;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
23 select STDOUT; $| = 1;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
24
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
25 my $t = Test::Nginx->new()->has(qw/http proxy limit_conn limit_req/);
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
26
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
27 $t->write_file_expand('nginx.conf', <<'EOF');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
28
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
29 %%TEST_GLOBALS%%
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
30
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
31 daemon off;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
32
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
33 events {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
34 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
35
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
36 http {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
37 %%TEST_GLOBALS_HTTP%%
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
38
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
39 limit_req_zone $binary_remote_addr zone=req:1m rate=30r/m;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
40 limit_conn_zone $binary_remote_addr zone=zone:1m;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
41
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
42 log_format test $uri:$limit_conn_status;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
43
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
44 server {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
45 listen 127.0.0.1:8081;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
46 server_name localhost;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
47
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
48 location /w {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
49 limit_req zone=req burst=10;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
50 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
51 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
52
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
53 server {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
54 listen 127.0.0.1:8080;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
55 server_name localhost;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
56
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
57 add_header X-Status $limit_conn_status always;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
58 access_log %%TESTDIR%%/test.log test;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
59
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
60 location /reject {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
61 proxy_pass http://127.0.0.1:8081/w;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
62 limit_conn zone 1;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
63 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
64
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
65 location /dry {
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
66 limit_conn zone 1;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
67 limit_conn_dry_run on;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
68 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
69
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
70 location / { }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
71 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
72 }
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
73
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
74 EOF
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
75
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
76 $t->write_file('w', '');
1693
5ac6efbe5552 Tests: removed TODO and try_run() checks for legacy versions.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1532
diff changeset
77 $t->run()->plan(6);
1532
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
78
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
79 ###############################################################################
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
80
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
81 like(http_get('/reject'), qr/ 200 .*PASSED/s, 'passed');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
82
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
83 my $s = http_get('/reject', start => 1);
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
84 like(http_get('/reject'), qr/ 503 .*REJECTED(?!_)/s, 'rejected');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
85 like(http_get('/dry'), qr/ 404 .*REJECTED_DRY_RUN/s, 'rejected dry run');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
86 unlike(http_get('/'), qr/X-Status/, 'no limit');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
87
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
88 close $s;
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
89
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
90 $t->stop();
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
91
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
92 like($t->read_file('error.log'), qr/limiting connections, dry/, 'log dry run');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
93 like($t->read_file('test.log'), qr|^/:-|m, 'log not found');
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
94
9d5996c3f5b8 Tests: limit_conn_dry_run, $limit_conn_status tests.
Sergey Kandaurov <pluknet@nginx.com>
parents:
diff changeset
95 ###############################################################################