comparison h3_limit_req.t @ 1879:12d950e1165c

Tests: HTTP/3 tests with limit_req.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 12 Jan 2023 20:53:50 +0400
parents
children cc13f7b098db
comparison
equal deleted inserted replaced
1878:b69bae343c53 1879:12d950e1165c
1 #!/usr/bin/perl
2
3 # (C) Sergey Kandaurov
4 # (C) Nginx, Inc.
5
6 # Tests for HTTP/3 protocol with limit_req.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19 use Test::Nginx::HTTP3;
20
21 ###############################################################################
22
23 select STDERR; $| = 1;
24 select STDOUT; $| = 1;
25
26 eval { require Crypt::Misc; die if $Crypt::Misc::VERSION < 0.067; };
27 plan(skip_all => 'CryptX version >= 0.067 required') if $@;
28
29 my $t = Test::Nginx->new()->has(qw/http http_v3 proxy limit_req/)
30 ->has_daemon('openssl')->plan(6);
31
32 $t->write_file_expand('nginx.conf', <<'EOF');
33
34 %%TEST_GLOBALS%%
35
36 daemon off;
37
38 events {
39 }
40
41 http {
42 %%TEST_GLOBALS_HTTP%%
43
44 ssl_certificate_key localhost.key;
45 ssl_certificate localhost.crt;
46
47 limit_req_zone $binary_remote_addr zone=req:1m rate=1r/s;
48
49 log_format test $status;
50
51 server {
52 listen 127.0.0.1:%%PORT_8980_UDP%% quic;
53 listen 127.0.0.1:8081;
54 server_name localhost;
55
56 location / {
57 add_header X-Body $request_body;
58 add_header X-Body-File $request_body_file;
59 client_body_in_file_only on;
60 proxy_pass http://127.0.0.1:8081/stub;
61 limit_req zone=req burst=2;
62 access_log %%TESTDIR%%/test.log test;
63 }
64
65 location /stub { }
66 }
67 }
68
69 EOF
70
71 $t->write_file('openssl.conf', <<EOF);
72 [ req ]
73 default_bits = 2048
74 encrypt_key = no
75 distinguished_name = req_distinguished_name
76 [ req_distinguished_name ]
77 EOF
78
79 my $d = $t->testdir();
80
81 foreach my $name ('localhost') {
82 system('openssl req -x509 -new '
83 . "-config $d/openssl.conf -subj /CN=$name/ "
84 . "-out $d/$name.crt -keyout $d/$name.key "
85 . ">>$d/openssl.out 2>&1") == 0
86 or die "Can't create certificate for $name: $!\n";
87 }
88
89 $t->write_file('stub', '');
90 $t->run();
91
92 ###############################################################################
93
94 # request body delayed in limit_req
95
96 my $s = Test::Nginx::HTTP3->new();
97 my $sid = $s->new_stream({ path => '/', body_more => 1 });
98 $s->h3_body('TEST', $sid);
99 my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
100
101 my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
102 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST',
103 'request body - limit req');
104
105 $s = Test::Nginx::HTTP3->new();
106 $sid = $s->new_stream({ path => '/', body_more => 1 });
107 select undef, undef, undef, 1.1;
108 $s->h3_body('TEST', $sid);
109 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
110
111 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
112 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST',
113 'request body - limit req - limited');
114
115 # request body with request
116
117 $s = Test::Nginx::HTTP3->new();
118 $sid = $s->new_stream({ path => '/', body => 'TEST2' });
119 select undef, undef, undef, 1.1;
120 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
121
122 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
123 is(read_body_file($frame->{headers}->{'x-body-file'}), 'TEST2',
124 'request body - limit req - with headers');
125
126 # delayed with an empty DATA frame
127
128 $s = Test::Nginx::HTTP3->new();
129 $sid = $s->new_stream({ path => '/', body_more => 1 });
130 $s->h3_body('', $sid);
131 select undef, undef, undef, 1.1;
132 $frames = $s->read(all => [{ sid => $sid, fin => 1 }]);
133
134 ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
135 is($frame->{headers}->{':status'}, 200, 'request body - limit req - empty');
136
137 # detect RESET_STREAM while request is delayed
138
139 $s = Test::Nginx::HTTP3->new();
140 $sid = $s->new_stream({ path => '/', body_more => 1 });
141 $s->reset_stream($sid, 0x010c);
142 $frames = $s->read(all => [{ type => 'DECODER_C' }]);
143
144 ($frame) = grep { $_->{type} eq "DECODER_C" } @$frames;
145 is($frame->{'val'}, $sid, 'reset stream - cancellation');
146
147 $t->stop();
148
149 like($t->read_file('test.log'), qr/499/, 'reset stream - log');
150
151 ###############################################################################
152
153 sub read_body_file {
154 my ($path) = @_;
155 return unless $path;
156 open FILE, $path or return "$!";
157 local $/;
158 my $content = <FILE>;
159 close FILE;
160 return $content;
161 }
162
163 ###############################################################################