[PATCH 2 of 2] Tests: upstream status tests

Maxim Dounin mdounin at mdounin.ru
Fri Aug 8 20:20:06 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1754683511 -10800
#      Fri Aug 08 23:05:11 2025 +0300
# Node ID 393cb49bff677e001e47141089d0c7df7f5eb6e1
# Parent  f43f8260fc6802f1193115a92c6dc1d2f23a0f7d
Tests: upstream status tests.

diff --git a/fastcgi_status.t b/fastcgi_status.t
new file mode 100644
--- /dev/null
+++ b/fastcgi_status.t
@@ -0,0 +1,146 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Test for fastcgi backend returning various status codes.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require FCGI; };
+plan(skip_all => 'FCGI not installed') if $@;
+plan(skip_all => 'win32') if $^O eq 'MSWin32';
+
+my $t = Test::Nginx->new()
+	->has(qw/http fastcgi/)->plan(11)
+	->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    fastcgi_param REQUEST_URI $request_uri;
+    fastcgi_param REQUEST_METHOD $request_method;
+
+    fastcgi_read_timeout 10s;
+    add_header X-Upstream-Status $upstream_status;
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            fastcgi_pass 127.0.0.1:8081;
+        }
+    }
+}
+
+EOF
+
+$t->run_daemon(\&fastcgi_daemon);
+$t->run()->waitforsocket('127.0.0.1:' . port(8081));
+
+###############################################################################
+
+like(http_get('/'), qr!^HTTP/1.1 200 !s, 'status 200');
+like(http_get('/600'), qr!^HTTP/1.1 600 !s, 'status 600 non-standard');
+
+like(http_get('/status-line'), qr!^HTTP/1.1 200 !s, 'status line ignored');
+like(http_get('/status-no-text'), qr!^HTTP/1.1 204 !s, 'status header no text');
+
+like(http_get('/no-status'), qr!^HTTP/1.1 200 !s, 'default status');
+like(http_get('/no-status-location'), qr!^HTTP/1.1 302 !s,
+	'default status with location');
+
+TODO: {
+local $TODO = 'not yet' unless $t->has_version('1.29.1');
+
+# 1xx responses are ignored since 1.29.1, and 101 (Switching Protocols)
+# is rejected
+
+like(http_get('/100'), qr!^HTTP/1.1 200 .*X-Upstream-Status: 200!s,
+	'status 100 ignored');
+like(http_get('/103'), qr!^HTTP/1.1 200 !s, 'status 103 ignored');
+
+like(http_get('/101'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+like(http_get('/101-no-text'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+
+like(http_get('/001'), qr!^HTTP/1.1 502 !s, 'status 001 rejected');
+
+}
+
+###############################################################################
+
+sub fastcgi_daemon {
+	my $socket = FCGI::OpenSocket('127.0.0.1:' . port(8081), 5);
+	my $request = FCGI::Request(\*STDIN, \*STDOUT, \*STDERR, \%ENV,
+		$socket);
+
+	my ($uri, $head);
+
+	while( $request->Accept() >= 0 ) {
+		$uri = $ENV{REQUEST_URI};
+
+		if ($uri eq '/') {
+			print "Status: 200 OK\n\n";
+
+		} elsif ($uri eq '/600') {
+			print "Status: 600 Non-standard\n\n";
+
+		} elsif ($uri eq '/status-line') {
+			print "HTTP/1.0 204 No content\n\n";
+
+		} elsif ($uri eq '/status-no-text') {
+			print "Status: 204\n\n";
+
+		} elsif ($uri eq '/no-status') {
+			print "Content-Type: text/html\n\n";
+
+		} elsif ($uri eq '/no-status-location') {
+			print "Location: /foobar\n\n";
+
+		} elsif ($uri eq '/100') {
+			print "Status: 100 Continue\n\n";
+			print "Status: 200 OK\n\n";
+
+		} elsif ($uri eq '/103') {
+			print "Status: 103 Early Hints\n";
+			print "Link: </foobar>\n\n";
+			print "Status: 200 OK\n\n";
+
+		} elsif ($uri eq '/101') {
+			print "Status: 101 Switching Protocols\n\n";
+
+		} elsif ($uri eq '/101-no-text') {
+			print "Status: 101\n\n";
+
+		} elsif ($uri eq '/001') {
+			print "Status: 001 Invalid\n\n";
+			print "Status: 200 OK\n\n";
+		}
+	}
+
+	FCGI::CloseSocket($socket);
+}
+
+###############################################################################
diff --git a/grpc_status.t b/grpc_status.t
new file mode 100644
--- /dev/null
+++ b/grpc_status.t
@@ -0,0 +1,199 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Tests for grpc backend returning various status codes.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+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 grpc/)->plan(12);
+
+$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;
+
+        grpc_read_timeout 10s;
+        add_header X-Upstream-Status $upstream_status;
+
+        location / {
+            grpc_pass grpc://127.0.0.1:8081;
+        }
+    }
+}
+
+EOF
+
+$t->run_daemon(\&grpc_daemon);
+$t->run()->waitforsocket('127.0.0.1:' . port(8081));
+
+###############################################################################
+
+like(http_get('/'), qr!^HTTP/1.1 200 !s, 'status 200');
+like(http_get('/600'), qr!^HTTP/1.1 600 !s, 'status 600 non-standard');
+
+like(http_get('/no-status'), qr!^HTTP/1.1 502 !s, 'no status rejected');
+like(http_get('/duplicate'), qr!^HTTP/1.1 502 !s, 'duplicate status rejected');
+like(http_get('/spaces'), qr!^HTTP/1.1 502 !s, 'status with spaces rejected');
+like(http_get('/nonfirst'), qr!^HTTP/1.1 502 !s, 'non first status rejected');
+
+TODO: {
+local $TODO = 'not yet' unless $t->has_version('1.29.1');
+
+# 1xx responses are ignored since 1.29.1
+
+like(http_get('/100'), qr!^HTTP/1.1 200 .*X-Upstream-Status: 200!s,
+        'status 100 ignored');
+like(http_get('/103'), qr!^HTTP/1.1 200 !s, 'status 103 ignored');
+
+}
+
+like(http_get('/100-end-stream'), qr!^HTTP/1.1 502 !s,
+	'status 100 with end stream rejected');
+like(http_get('/100-many'), qr!^HTTP/1.1 502 !s,
+	'status 100 many times rejected');
+
+like(http_get('/101'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+like(http_get('/001'), qr!^HTTP/1.1 502 !s, 'status 001 rejected');
+
+###############################################################################
+
+sub grpc_daemon {
+	my $server = IO::Socket::INET->new(
+		Proto => 'tcp',
+		LocalHost => '127.0.0.1:' . port(8081),
+		Listen => 5,
+		Reuse => 1
+	)
+		or die "Can't create listening socket: $!\n";
+
+	local $SIG{PIPE} = 'IGNORE';
+
+	while (my $client = $server->accept()) {
+		$client->autoflush(1);
+
+ 		# preface
+		$client->sysread(my $buf, 24) == 24
+			or next;
+
+		my $c = Test::Nginx::HTTP2->new(
+			1, socket => $client, pure => 1, preface => ""
+		)
+			or next;
+
+		my $frames = $c->read(all => [{ fin => 4 }]);
+		my ($frame) = grep { $_->{type} eq "HEADERS" } @$frames;
+
+		my $sid = $frame->{sid};
+		my $uri = $frame->{headers}{':path'};
+		my $status;
+
+		if ($uri eq '/') {
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/600') {
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '600' },
+			]}, $sid);
+
+		} elsif ($uri eq '/no-status') {
+			$c->new_stream({ headers => [
+				{ name => 'foo', value => 'bar' },
+			]}, $sid);
+
+		} elsif ($uri eq '/duplicate') {
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+				{ name => ':status', value => '204' },
+			]}, $sid);
+
+		} elsif ($uri eq '/spaces') {
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '2 0 0' },
+			]}, $sid);
+
+		} elsif ($uri eq '/nonfirst') {
+			$c->new_stream({ headers => [
+				{ name => 'foo', value => 'bar' },
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/100') {
+			$c->new_stream({ body_more => 1, headers => [
+				{ name => ':status', value => '100' },
+			]}, $sid);
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/100-end-stream') {
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '100' },
+			]}, $sid);
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/100-many') {
+			$c->new_stream({ body_more => 1, headers => [
+				{ name => ':status', value => '100' },
+			]}, $sid)
+				for 1..15;
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/103') {
+			$c->new_stream({ body_more => 1, headers => [
+				{ name => ':status', value => '103' },
+			]}, $sid);
+			$c->new_stream({ headers => [
+				{ name => ':status', value => '200' },
+			]}, $sid);
+
+		} elsif ($uri eq '/101') {
+			$c->new_stream({ body => 'foo', headers => [
+				{ name => ':status', value => '101' },
+			]}, $sid);
+
+		} elsif ($uri eq '/001') {
+			$c->new_stream({ body => 'foo', headers => [
+				{ name => ':status', value => '101' },
+			]}, $sid);
+		}
+
+		close $client;
+	}
+}
+
+###############################################################################
diff --git a/proxy_status.t b/proxy_status.t
new file mode 100644
--- /dev/null
+++ b/proxy_status.t
@@ -0,0 +1,190 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Test for http backend returning various status codes.
+
+###############################################################################
+
+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 proxy/);
+
+$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;
+
+        proxy_read_timeout 10s;
+        add_header X-Upstream-Status $upstream_status;
+
+        location / {
+            proxy_pass http://127.0.0.1:8081;
+        }
+
+        location /allow09/ {
+            proxy_pass http://127.0.0.1:8081/;
+            proxy_allow_http09 on;
+        }
+    }
+}
+
+EOF
+
+$t->run_daemon(\&http_daemon);
+$t->try_run('no proxy_allow_http09')->plan(12);
+$t->waitforsocket('127.0.0.1:' . port(8081));
+
+###############################################################################
+
+like(http_get('/'), qr!^HTTP/1.1 200 !s, 'status 200');
+like(http_get('/600'), qr!^HTTP/1.1 600 !s, 'status 600 non-standard');
+
+like(http_get('/http10'), qr!^HTTP/1.1 200 !s, 'http 1.0 200');
+like(http_get('/duplicate'), qr!^HTTP/1.1 200 !s, 'duplicate status ignored');
+
+# HTTP/0.9 is disabled by default since 1.29.1
+
+like(http_get('/http09'), qr!^HTTP/1.1 502 !s, 'http 0.9');
+like(http_get('/allow09/http09'), qr!^HTTP/1.1 200 .*HTTP/0.9!s,
+	'http 0.9 allowed');
+
+# spaces between digits not allowed since 1.29.1
+
+like(http_get('/spaces'), qr!^HTTP/1.1 502 !s, 'status with spaces rejected');
+like(http_get('/allow09/spaces'), qr!^HTTP/1.1 200 OK.*2 0 0 OK!s,
+	'status with spaces as http 0.9');
+
+# 1xx responses are ignored since 1.29.1, and 101 (Switching Protocols)
+# is rejected unless requested by the client and configured
+
+like(http_get('/100'), qr!^HTTP/1.1 200 .*X-Upstream-Status: 200!s,
+	'status 100 ignored');
+like(http_get('/103'), qr!^HTTP/1.1 200 !s, 'status 103 ignored');
+
+like(http_get('/101'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+
+like(http_get('/001'), qr!^HTTP/1.1 502 !s, 'status 001 rejected');
+
+###############################################################################
+
+sub http_daemon {
+	my $server = IO::Socket::INET->new(
+		Proto => 'tcp',
+		LocalAddr => '127.0.0.1:' . port(8081),
+		Listen => 5,
+		Reuse => 1
+	)
+		or die "Can't create listening socket: $!\n";
+
+	local $SIG{PIPE} = 'IGNORE';
+
+	while (my $client = $server->accept()) {
+		$client->autoflush(1);
+
+		my $headers = '';
+		my $uri = '';
+
+		while (<$client>) {
+			$headers .= $_;
+			last if (/^\x0d?\x0a?$/);
+		}
+
+		$uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
+
+		if ($uri eq '/') {
+
+			print $client
+				'HTTP/1.1 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/600') {
+
+			print $client
+				'HTTP/1.1 600 Non-standard' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/http10') {
+
+			print $client
+				'HTTP/1.0 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/duplicate') {
+
+			print $client
+				'HTTP/1.1 200 OK' . CRLF .
+				'HTTP/1.1 204 No content' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri =~ m!/http09!) {
+
+			print $client 'It is HTTP/0.9 response' . CRLF;
+
+		} elsif ($uri =~ m!/spaces!) {
+
+			print $client
+				'HTTP/1.1 2 0 0 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/100') {
+
+			print $client
+				'HTTP/1.1 100 Continue' . CRLF . CRLF .
+				'HTTP/1.1 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/103') {
+
+			print $client
+				'HTTP/1.1 103 Early Hints' . CRLF .
+				'Link: </foobar>' . CRLF . CRLF .
+				'HTTP/1.1 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/101') {
+
+			print $client
+				'HTTP/1.1 101 Switching' . CRLF . CRLF .
+				'HTTP/1.1 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		} elsif ($uri eq '/001') {
+
+			print $client
+				'HTTP/1.1 001 Invalid' . CRLF . CRLF .
+				'HTTP/1.1 200 OK' . CRLF .
+				'Connection: close' . CRLF . CRLF;
+
+		}
+
+		close $client;
+	}
+}
+
+###############################################################################
diff --git a/scgi_status.t b/scgi_status.t
new file mode 100644
--- /dev/null
+++ b/scgi_status.t
@@ -0,0 +1,153 @@
+#!/usr/bin/perl
+
+# (C) Maxim Dounin
+
+# Test for scgi backend returning various status codes.
+
+###############################################################################
+
+use warnings;
+use strict;
+
+use Test::More;
+
+BEGIN { use FindBin; chdir($FindBin::Bin); }
+
+use lib 'lib';
+use Test::Nginx;
+
+###############################################################################
+
+select STDERR; $| = 1;
+select STDOUT; $| = 1;
+
+eval { require SCGI; };
+plan(skip_all => 'SCGI not installed') if $@;
+
+my $t = Test::Nginx->new()
+	->has(qw/http scgi/)->plan(11)
+	->write_file_expand('nginx.conf', <<'EOF');
+
+%%TEST_GLOBALS%%
+
+daemon off;
+
+events {
+}
+
+http {
+    %%TEST_GLOBALS_HTTP%%
+
+    scgi_param SCGI 1;
+    scgi_param REQUEST_URI $request_uri;
+    scgi_param REQUEST_METHOD $request_method;
+
+    scgi_read_timeout 10s;
+    add_header X-Upstream-Status $upstream_status;
+
+    server {
+        listen       127.0.0.1:8080;
+        server_name  localhost;
+
+        location / {
+            scgi_pass 127.0.0.1:8081;
+        }
+    }
+}
+
+EOF
+
+$t->run_daemon(\&scgi_daemon);
+$t->run()->waitforsocket('127.0.0.1:' . port(8081));
+
+###############################################################################
+
+like(http_get('/'), qr!^HTTP/1.1 200 !s, 'status 200');
+like(http_get('/600'), qr!^HTTP/1.1 600 !s, 'status 600 non-standard');
+
+like(http_get('/status-line'), qr!^HTTP/1.1 204 !s, 'status line');
+like(http_get('/status-no-text'), qr!^HTTP/1.1 204 !s, 'status header no text');
+
+like(http_get('/no-status'), qr!^HTTP/1.1 200 !s, 'default status');
+like(http_get('/no-status-location'), qr!^HTTP/1.1 302 !s,
+	'default status with location');
+
+TODO: {
+local $TODO = 'not yet' unless $t->has_version('1.29.1');
+
+# 1xx responses are ignored since 1.29.1, and 101 (Switching Protocols)
+# is rejected unless requested by the client
+
+like(http_get('/100'), qr!^HTTP/1.1 200 .*X-Upstream-Status: 200!s,
+	'status 100 ignored');
+like(http_get('/103'), qr!^HTTP/1.1 200 !s, 'status 103 ignored');
+
+like(http_get('/101'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+like(http_get('/101-no-text'), qr!^HTTP/1.1 502 !s, 'status 101 rejected');
+
+like(http_get('/001'), qr!^HTTP/1.1 502 !s, 'status 001 rejected');
+
+}
+
+###############################################################################
+
+sub scgi_daemon {
+	my $server = IO::Socket::INET->new(
+		Proto => 'tcp',
+		LocalHost => '127.0.0.1:' . port(8081),
+		Listen => 5,
+		Reuse => 1
+	)
+		or die "Can't create listening socket: $!\n";
+
+	my $scgi = SCGI->new($server, blocking => 1);
+	my ($c, $uri);
+
+	while (my $request = $scgi->accept()) {
+		eval { $request->read_env(); };
+		next if $@;
+		
+		$uri = $request->env->{REQUEST_URI};
+
+		$c = $request->connection();
+
+		if ($uri eq '/') {
+			$c->print("Status: 200 OK\n\n");
+
+		} elsif ($uri eq '/600') {
+			$c->print("Status: 600 Non-standard\n\n");
+
+		} elsif ($uri eq '/status-line') {
+			$c->print("HTTP/1.0 204 No content\n\n");
+
+		} elsif ($uri eq '/status-no-text') {
+			$c->print("Status: 204\n\n");
+
+		} elsif ($uri eq '/no-status') {
+			$c->print("Content-Type: text/html\n\n");
+
+		} elsif ($uri eq '/no-status-location') {
+			$c->print("Location: /foobar\n\n");
+
+		} elsif ($uri eq '/100') {
+			$c->print("Status: 100 Continue\n\n");
+			$c->print("Status: 200 OK\n\n");
+
+		} elsif ($uri eq '/103') {
+			$c->print("Status: 103 Early Hints\n");
+			$c->print("Link: </foobar>\n\n");
+			$c->print("Status: 200 OK\n\n");
+
+		} elsif ($uri eq '/101') {
+			$c->print("Status: 101 Switching Protocols\n\n");
+
+		} elsif ($uri eq '/101-no-text') {
+			$c->print("Status: 101\n\n");
+
+		} elsif ($uri eq '/001') {
+			$c->print("Status: 001 Invalid\n\n");
+		}
+	}
+}
+
+###############################################################################



More information about the nginx-devel mailing list