# HG changeset patch # User Maxim Dounin # Date 1716500172 -10800 # Node ID 2d58bb10ff5d78c07563af5d120a881c6e106c99 # Parent 79753dd514e60b47375e36c54739ea434e04a5b6 Tests: max_client_headers test. diff -r 79753dd514e6 -r 2d58bb10ff5d h2_max_headers.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/h2_max_headers.t Fri May 24 00:36:12 2024 +0300 @@ -0,0 +1,92 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin + +# Tests for max_headers directive, HTTP/2. + +############################################################################### + +use warnings; +use strict; + +use Test::More; +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; +use Test::Nginx::HTTP2; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http http_v2 rewrite/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + server { + listen 127.0.0.1:8080; + server_name localhost; + + http2 on; + max_headers 5; + + location / { + return 204; + } + } +} + +EOF + +$t->try_run('no max_headers')->plan(3); + +############################################################################### + +like(get('/'), qr/ 204/, 'two headers'); +like(get('/', ('Foo: bar') x 3), qr/ 204/, 'five headers'); +like(get('/', ('Foo: bar') x 4), qr/ 400/, 'six headers rejected'); + +############################################################################### + +sub get { + my ($url, @headers) = @_; + + my $s = Test::Nginx::HTTP2->new(); + my $sid = $s->new_stream({ + headers => [ + { name => ':method', value => 'GET' }, + { name => ':scheme', value => 'http' }, + { name => ':path', value => $url }, + { name => ':authority', value => 'localhost' }, + { name => 'foo', value => 'bar', mode => 2 }, + { name => 'foo', value => 'bar', mode => 2 }, + map { + my ($n, $v) = split /:/; + { name => lc $n, value => $v, mode => 2 }; + } @headers + ] + }); + + my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); + + my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; + + return join("\n", map { "$_: " . $frame->{headers}->{$_}; } + keys %{$frame->{headers}}); +} + +############################################################################### diff -r 79753dd514e6 -r 2d58bb10ff5d h3_max_headers.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/h3_max_headers.t Fri May 24 00:36:12 2024 +0300 @@ -0,0 +1,112 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin + +# Tests for max_headers directive, HTTP/3. + +############################################################################### + +use warnings; +use strict; + +use Test::More; +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; +use Test::Nginx::HTTP3; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http http_v3 rewrite cryptx/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + ssl_certificate localhost.crt; + ssl_certificate_key localhost.key; + + server { + listen 127.0.0.1:%%PORT_8980_UDP%% quic; + server_name localhost; + + max_headers 5; + + location / { + return 204; + } + } +} + +EOF + +$t->write_file('openssl.conf', <testdir(); + +foreach my $name ('localhost') { + system('openssl req -x509 -new ' + . "-config $d/openssl.conf -subj /CN=$name/ " + . "-out $d/$name.crt -keyout $d/$name.key " + . ">>$d/openssl.out 2>&1") == 0 + or die "Can't create certificate for $name: $!\n"; +} + +$t->try_run('no max_headers')->plan(3); + +############################################################################### + +like(get('/'), qr/ 204/, 'two headers'); +like(get('/', ('Foo: bar') x 3), qr/ 204/, 'five headers'); +like(get('/', ('Foo: bar') x 4), qr/ 400/, 'six headers rejected'); + +############################################################################### + +sub get { + my ($url, @headers) = @_; + + my $s = Test::Nginx::HTTP3->new(); + my $sid = $s->new_stream({ + headers => [ + { name => ':method', value => 'GET' }, + { name => ':scheme', value => 'http' }, + { name => ':path', value => $url }, + { name => ':authority', value => 'localhost' }, + { name => 'foo', value => 'bar' }, + { name => 'foo', value => 'bar' }, + map { + my ($n, $v) = split /:/; + { name => lc $n, value => $v }; + } @headers + ] + }); + + my $frames = $s->read(all => [{ sid => $sid, fin => 1 }]); + + my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames; + + return join("\n", map { "$_: " . $frame->{headers}->{$_}; } + keys %{$frame->{headers}}); +} + +############################################################################### diff -r 79753dd514e6 -r 2d58bb10ff5d http_max_headers.t --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http_max_headers.t Fri May 24 00:36:12 2024 +0300 @@ -0,0 +1,73 @@ +#!/usr/bin/perl + +# (C) Maxim Dounin + +# Tests for max_headers directive. + +############################################################################### + +use warnings; +use strict; + +use Test::More; +use Socket qw/ CRLF /; + +BEGIN { use FindBin; chdir($FindBin::Bin); } + +use lib 'lib'; +use Test::Nginx; + +############################################################################### + +select STDERR; $| = 1; +select STDOUT; $| = 1; + +my $t = Test::Nginx->new()->has(qw/http rewrite/); + +$t->write_file_expand('nginx.conf', <<'EOF'); + +%%TEST_GLOBALS%% + +daemon off; + +events { +} + +http { + %%TEST_GLOBALS_HTTP%% + + server { + listen 127.0.0.1:8080; + server_name localhost; + + max_headers 5; + + location / { + return 204; + } + } +} + +EOF + +$t->try_run('no max_headers')->plan(3); + +############################################################################### + +like(get('/'), qr/ 204/, 'two headers'); +like(get('/', ('Foo: bar') x 3), qr/ 204/, 'five headers'); +like(get('/', ('Foo: bar') x 4), qr/ 400/, 'six headers rejected'); + +############################################################################### + +sub get { + my ($url, @headers) = @_; + return http( + "GET $url HTTP/1.1" . CRLF . + 'Host: localhost' . CRLF . + 'Connection: close' . CRLF . + join(CRLF, @headers) . CRLF . CRLF + ); +} + +###############################################################################