--- a/proxy_keepalive.t	2015-11-30 14:20:48.311899921 +0000
+++ b/proxy_keepalive.t	2015-11-30 14:20:19.451167311 +0000
@@ -25,7 +25,7 @@
 select STDOUT; $| = 1;
 
 my $t = Test::Nginx->new()->has(qw/http proxy upstream_keepalive ssi rewrite/)
-	->plan(49)->write_file_expand('nginx.conf', <<'EOF');
+	->plan(51)->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
 
@@ -58,6 +58,11 @@
             proxy_buffering off;
         }
 
+        location /unbuffered-request/ {
+            proxy_pass http://backend;
+            proxy_request_buffering off;
+        }
+
         location /inmemory/ {
             ssi on;
             rewrite ^ /ssi.html break;
@@ -206,6 +211,9 @@
 like(http_get('/inmemory/closed1'), qr/200 OK/, 'inmemory closed 1');
 like(http_get('/inmemory/closed2'), qr/200 OK/, 'inmemory closed 2');
 
+like(http_bad_post('/unbuffered-request/empty1'), qr/200 OK/, 'unbuffered initial bad post request');
+like(http_get('/unbuffered-request/empty1'), qr/200 OK/, 'unbuffered subsequent good request');
+
 # check for errors, shouldn't be any
 
 like(`grep -F '[error]' ${\($t->testdir())}/error.log`, qr/^$/s, 'no errors');
@@ -235,6 +243,7 @@
 		while (1) {
 			my $headers = '';
 			my $uri = '';
+			my $method = '';
 
 			while (<$client>) {
 				Test::Nginx::log_core('||', $_);
@@ -245,9 +254,18 @@
 			last if $headers eq '';
 			$rcount++;
 
-			$uri = $1 if $headers =~ /^\S+\s+([^ ]+)\s+HTTP/i;
+			($method, $uri) = ($1, $2) if $headers =~ /^(\S+)\s+([^ ]+)\s+HTTP/i;
 
-			if ($uri =~ m/length/) {
+			if ($method !~ /(GET|HEAD|POST)/i) {
+				print $client
+					"HTTP/1.1 400 Bad Request" . CRLF .
+					"X-Request: $rcount" . CRLF .
+					"X-Connection: $ccount" . CRLF .
+					"Connection: close" . CRLF .
+					"Content-Length: 0" . CRLF . CRLF;
+				last;
+
+			} elsif ($uri =~ m/length/) {
 				print $client
 					"HTTP/1.1 200 OK" . CRLF .
 					"X-Request: $rcount" . CRLF .
@@ -347,10 +365,34 @@
 					"Oops, '$uri' not found" . CRLF;
 				last;
 			}
+
+			# discard body after printing response
+
+			if ($headers =~ /^POST\s+[^ ]+\s+HTTP/i) {
+				my $length = $1 if $headers =~ /^Content-Length: (\d+)/mi;
+				my $body = '';
+				while ($length > 0) {
+					my $n = read $client, $body, $length;
+					$length -= $n;
+					last if $n == 0;
+				}
+			}
 		}
 
 		close $client;
 	}
 }
 
+sub http_bad_post {
+	my ($url, %extra) = @_;
+
+	my $p = "POST $url HTTP/1.1" . CRLF .
+		"Host: localhost" . CRLF .
+		"Content-Length: 10" . CRLF .
+		CRLF .
+		"12345";
+
+	return http($p, %extra);
+}
+
 ###############################################################################
