Skip to content

Commit

Permalink
bugfix: did not skip test cases that proxy_pass or directly connect t…
Browse files Browse the repository at this point in the history
…o request server port. (openresty#139)
  • Loading branch information
zhuizhuhaomeng authored Dec 3, 2021
1 parent 514f8cb commit 6f2ff14
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/Test/Nginx/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,19 @@ sub send_http_req_by_curl ($$$) {
warn "running cmd @$cmd";
}

if (use_http2($block)) {
my $total_tries = $TotalConnectingTimeouts ? 20 : 50;
while ($total_tries-- > 0) {
if (is_tcp_port_used($ServerPortForClient)) {
last;
}

warn "$name - waiting for nginx to listen on port "
. "$ServerPortForClient, Retry connecting after 1 sec\n";
sleep 1;
}
}

if (use_http3($block)) {
my $total_tries = $TotalConnectingTimeouts ? 20 : 50;
while ($total_tries-- > 0) {
Expand Down Expand Up @@ -2329,7 +2342,9 @@ sub gen_curl_cmd_from_req ($$) {
push @args, '-I';

} else {
push @args, "-X", $meth;
if ($meth ne 'GET') {
push @args, "-X", $meth;
}
}

if ($http_ver ne '1.1') {
Expand Down Expand Up @@ -4325,6 +4340,10 @@ One can enable HTTP/2 mode for an individual test block by specifying the L<http
--- http2
One can disable HTTP/2 mode for an individual test block by specifying the L<no_http2> section, as in
--- no_http2
=head2 TEST_NGINX_USE_HTTP3
Enables the "http3" test mode by enforcing using the HTTP/3 protocol to send the
Expand All @@ -4342,6 +4361,10 @@ One can enable HTTP/3 mode for an individual test block by specifying the L<http
--- http3
One can disable HTTP/3 mode for an individual test block by specifying the L<no_http3> section, as in
--- no_http3
=head2 TEST_NGINX_HTTP3_CRT
When running in http3 mode, you need to specify the default certificate.
Expand Down
58 changes: 58 additions & 0 deletions lib/Test/Nginx/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ sub is_udp_port_used($) {
return 0;
}

sub is_tcp_port_used($) {
my $port = shift;
my $filename = "/proc/net/tcp";

open my $fh, $filename or die "Could not open $filename. $!";
while (<$fh>) {
my $line = $_;
if ($line =~ /^ *\d+: [0-9A-F]+:([0-9A-F]+) /) {
if ($port == hex($1)) {
close $fh;
return 1;
}
}
}

close $fh;
return 0;
}

sub no_long_string () {
$NoLongString = 1;
}
Expand Down Expand Up @@ -528,6 +547,7 @@ our @EXPORT = qw(
no_nginx_manager
use_hup
is_udp_port_used
is_tcp_port_used
);


Expand Down Expand Up @@ -2789,6 +2809,10 @@ sub use_http2 ($) {
return $cached;
}

if (defined $block->no_http2) {
return undef;
}

if (defined $block->http2) {
if ($block->raw_request) {
bail_out("cannot use --- http2 with --- raw_request");
Expand Down Expand Up @@ -2837,6 +2861,36 @@ sub use_http2 ($) {
return undef;
}

my $pat = qr{(proxy_pass .*:\$(server_port|TEST_NGINX_SERVER_PORT)
| ngx.req.raw_header
| lua_check_client_abort
| ngx.location.capture
| ngx.req.socket
| ngx.req.read_body)}x;
if (defined $block->config) {
if ($block->config =~ $pat) {
warn "WARNING: ", $block->name, " - $1 does not support in HTTP/2\n";
$block->set_value("test_nginx_enabled_http2", 0);
return undef;
}
}

if (defined $block->user_files) {
if ($block->user_files =~ $pat) {
warn "WARNING: ", $block->name, " - $1 does not support HTTP/2\n";
$block->set_value("test_nginx_enabled_http2", 0);
return undef;
}
}

if (defined $block->http_config) {
if ($block->http_config=~ $pat) {
warn "WARNING: ", $block->name, " - can not listen on HTTP and HTTP/2 at the same time, so will no use HTTP2\n";
$block->set_value("test_nginx_enabled_http2", 0);
return undef;
}
}

$block->set_value("test_nginx_enabled_http2", 1);

if (!$LoadedIPCRun) {
Expand All @@ -2860,6 +2914,10 @@ sub use_http3 ($) {
return $cached;
}

if (defined $block->no_http2) {
return undef;
}

if (defined $block->http3) {
if ($block->raw_request) {
bail_out("cannot use --- http3 with --- raw_request");
Expand Down

0 comments on commit 6f2ff14

Please sign in to comment.