]> git.kaiwu.me - njs.git/commitdiff
Tests: fail debug context tests on invalid responses
authorDmitry Volyntsev <xeioex@nginx.com>
Sat, 24 Oct 2026 05:45:29 +0000 (22:45 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Thu, 19 Nov 2026 06:44:10 +0000 (22:44 -0800)
Previously, missing engine IDs were treated as evidence of a non-debug
build.  An endpoint failure could therefore skip a debug test, while
merge tests could pass after ignoring invalid responses from some
locations or servers.  The response-based guards were introduced in
c4389399.

Check --with-debug independently before starting nginx.  Require a
successful HTTP response and a complete numeric engine ID from every
endpoint, or a complete numeric stream response, before comparing
contexts.  Let startup and response failures fail the tests instead
of reporting unsupported functionality.

nginx/t/js_import2.t
nginx/t/js_merge_location_blocks.t
nginx/t/js_merge_server_blocks.t
nginx/t/stream_js_import2.t

index de58811b9343a910f1e9bc7e031f7509016b16d6..45e0c829a6b5fe4a8b0e5363ba620865cb9fcea0 100644 (file)
@@ -22,8 +22,12 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)
-       ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http proxy rewrite/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+       unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
 
@@ -124,37 +128,44 @@ $t->write_file('main.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs available');
+$t->run()->plan(5);
 
 ###############################################################################
 
-my ($mainid) = http_get('/test_foo') =~ /MAIN-TEST:(\d+)/s;
-
-plan(skip_all => 'ngx.engine_id requires --with-debug')
-    unless defined $mainid;
-
-$t->plan(5);
-
-my ($barid) = http_get('/test_bar') =~ /MAIN-TEST:(\d+)/s;
+my $mainid = engine_id('/test_foo', 'MAIN-TEST');
+my $barid = engine_id('/test_bar', 'MAIN-TEST');
 
 ok($barid == $mainid, 'same context for main.js');
 
-my ($libid) = http_get('/test_lib') =~ /LIB-TEST:(\d+)/s;
+my $libid = engine_id('/test_lib', 'LIB-TEST');
 
 ok($libid != $mainid, 'different context for lib.js');
 
-my ($funid) = http_get('/test_fun') =~ /FUN-TEST:(\d+)/s;
+my $funid = engine_id('/test_fun', 'FUN-TEST');
 
 ok($funid != $mainid && $funid != $libid,
    'different context for fun.js');
 
-my ($pfunid) = http_get('/proxy/test_fun') =~ /FUN-TEST:(\d+)/s;
+my $pfunid = engine_id('/proxy/test_fun', 'FUN-TEST');
 
 ok($pfunid != $funid && $pfunid != $mainid && $pfunid != $libid,
    'different context for fun.js in proxy');
 
-my ($varid) = http_get('/test_var') =~ /P-TEST:(\d+)/s;
+my $varid = engine_id('/test_var', 'P-TEST');
 
 ok($varid == $mainid, 'variable from main.js');
 
 ###############################################################################
+
+sub engine_id {
+       my ($uri, $prefix) = @_;
+
+       my $response = http_get($uri);
+       my ($headers, $body) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
+       my ($id) = ($body // '') =~ /\A\Q$prefix\E:(\d+)\z/;
+
+       die "invalid engine ID response from $uri\n"
+               unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /;
+
+       return $id;
+}
index dfb6ec4efdd497e2da3508b85b038a1d2030d43e..2be7bf612d87c3049359e8f9a7516061f7dade12 100644 (file)
@@ -22,8 +22,12 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http/)
-       ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+       unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
 
@@ -70,20 +74,21 @@ $t->write_file('main.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs available');
+$t->run()->plan(1);
 
 ###############################################################################
 
 my %ids;
 for my $uri ('/a', '/b', '/c', '/d') {
-       http_get($uri) =~ /\x0d\x0a\x0d\x0a(\d+)/ms;
-       $ids{$1} = 1 if defined $1;
-}
+       my $response = http_get($uri);
+       my ($headers, $id) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
 
-plan(skip_all => 'ngx.engine_id requires --with-debug')
-    unless scalar keys %ids;
+       die "invalid engine ID response from $uri\n"
+               unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /
+               && $id =~ /\A\d+\z/;
 
-$t->plan(1);
+       $ids{$id} = 1;
+}
 
 is(scalar keys %ids, 1, 'http js block imported once');
 
index 987d136c07a95cd7bf3a5c39a6e287c5f1285351..c363d2ff186f346cf93e57975e955abe3770d5ff 100644 (file)
@@ -22,8 +22,12 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http/)
-       ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+       unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
 
@@ -81,22 +85,22 @@ $t->write_file('main.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs available');
+$t->run()->plan(1);
 
 ###############################################################################
 
 my %ids;
 for my $port (port(8080), port(8081), port(8082), port(8083)) {
-       http("GET / HTTP/1.0\nHost: localhost\n\n",
-               PeerAddr => "127.0.0.1:$port")
-               =~ /\x0d\x0a\x0d\x0a(\d+)/ms;
-       $ids{$1} = 1 if defined $1;
-}
+       my $response = http("GET / HTTP/1.0\nHost: localhost\n\n",
+               PeerAddr => "127.0.0.1:$port");
+       my ($headers, $id) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
 
-plan(skip_all => 'ngx.engine_id requires --with-debug')
-    unless scalar keys %ids;
+       die "invalid engine ID response from port $port\n"
+               unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /
+               && $id =~ /\A\d+\z/;
 
-$t->plan(1);
+       $ids{$id} = 1;
+}
 
 is(scalar keys %ids, 1, 'http js block imported once');
 
index d1f0b1ff21ed08f3528753c8ae35167df1c8d662..173f1b3d313ce95e011dc7598e60f853433e58d9 100644 (file)
@@ -23,8 +23,12 @@ use Test::Nginx::Stream qw/ stream /;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/stream stream_return/)
-       ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/stream stream_return/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+       unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
 
@@ -74,22 +78,29 @@ $t->write_file('main.js', <<EOF);
 
 EOF
 
-$t->try_run('no njs available');
+$t->run()->plan(3);
 
 ###############################################################################
 
-my ($id_82) = stream('127.0.0.1:' . port(8082))->read() =~ /(\d+)/;
-plan(skip_all => 'ngx.engine_id requires --with-debug')
-    unless defined $id_82;
-
-$t->plan(3);
+my $id_82 = engine_id(8082);
 
 is(stream('127.0.0.1:' . port(8081))->read(), 'P-TEST', 'foo.bar.p');
 
-my ($id_83) = stream('127.0.0.1:' . port(8083))->read() =~ /(\d+)/;
-my ($id_84) = stream('127.0.0.1:' . port(8084))->read() =~ /(\d+)/;
+my $id_83 = engine_id(8083);
+my $id_84 = engine_id(8084);
 
 ok($id_82 == $id_83, 'same context for stream level import');
 ok($id_84 != $id_82, 'different context for server level import');
 
 ###############################################################################
+
+sub engine_id {
+       my ($port) = @_;
+
+       my $id = stream('127.0.0.1:' . port($port))->read();
+
+       die "invalid engine ID response from port $port\n"
+               unless defined $id && $id =~ /\A\d+\z/;
+
+       return $id;
+}