From: Dmitry Volyntsev Date: Sat, 24 Oct 2026 05:45:29 +0000 (-0700) Subject: Tests: fail debug context tests on invalid responses X-Git-Url: https://git.kaiwu.me/http/control.html?a=commitdiff_plain;h=e46cdc6f1a72579083a864b7965ffae70da03ffc;p=njs.git Tests: fail debug context tests on invalid responses 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. --- diff --git a/nginx/t/js_import2.t b/nginx/t/js_import2.t index de58811b..45e0c829 100644 --- a/nginx/t/js_import2.t +++ b/nginx/t/js_import2.t @@ -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', <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; +} diff --git a/nginx/t/js_merge_location_blocks.t b/nginx/t/js_merge_location_blocks.t index dfb6ec4e..2be7bf61 100644 --- a/nginx/t/js_merge_location_blocks.t +++ b/nginx/t/js_merge_location_blocks.t @@ -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', <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'); diff --git a/nginx/t/js_merge_server_blocks.t b/nginx/t/js_merge_server_blocks.t index 987d136c..c363d2ff 100644 --- a/nginx/t/js_merge_server_blocks.t +++ b/nginx/t/js_merge_server_blocks.t @@ -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', <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'); diff --git a/nginx/t/stream_js_import2.t b/nginx/t/stream_js_import2.t index d1f0b1ff..173f1b3d 100644 --- a/nginx/t/stream_js_import2.t +++ b/nginx/t/stream_js_import2.t @@ -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', <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; +}