@@ -714,6 +714,31 @@ func testTCPConnectionCloses(t *testing.T, req string, h Handler) {
714
714
}
715
715
}
716
716
717
+ func testTCPConnectionStaysOpen (t * testing.T , req string , handler Handler ) {
718
+ defer afterTest (t )
719
+ ts := httptest .NewServer (handler )
720
+ defer ts .Close ()
721
+ conn , err := net .Dial ("tcp" , ts .Listener .Addr ().String ())
722
+ if err != nil {
723
+ t .Fatal (err )
724
+ }
725
+ defer conn .Close ()
726
+ br := bufio .NewReader (conn )
727
+ for i := 0 ; i < 2 ; i ++ {
728
+ if _ , err := io .WriteString (conn , req ); err != nil {
729
+ t .Fatal (err )
730
+ }
731
+ res , err := ReadResponse (br , nil )
732
+ if err != nil {
733
+ t .Fatalf ("res %d: %v" , i + 1 , err )
734
+ }
735
+ if _ , err := io .Copy (ioutil .Discard , res .Body ); err != nil {
736
+ t .Fatalf ("res %d body copy: %v" , i + 1 , err )
737
+ }
738
+ res .Body .Close ()
739
+ }
740
+ }
741
+
717
742
// TestServeHTTP10Close verifies that HTTP/1.0 requests won't be kept alive.
718
743
func TestServeHTTP10Close (t * testing.T ) {
719
744
testTCPConnectionCloses (t , "GET / HTTP/1.0\r \n \r \n " , HandlerFunc (func (w ResponseWriter , r * Request ) {
@@ -749,6 +774,24 @@ func TestHTTP2UpgradeClosesConnection(t *testing.T) {
749
774
}))
750
775
}
751
776
777
+ func send204 (w ResponseWriter , r * Request ) { w .WriteHeader (204 ) }
778
+ func send304 (w ResponseWriter , r * Request ) { w .WriteHeader (304 ) }
779
+
780
+ // Issue 15647: 204 responses can't have bodies, so HTTP/1.0 keep-alive conns should stay open.
781
+ func TestHTTP10KeepAlive204Response (t * testing.T ) {
782
+ testTCPConnectionStaysOpen (t , "GET / HTTP/1.0\r \n Connection: keep-alive\r \n \r \n " , HandlerFunc (send204 ))
783
+ }
784
+
785
+ func TestHTTP11KeepAlive204Response (t * testing.T ) {
786
+ testTCPConnectionStaysOpen (t , "GET / HTTP/1.1\r \n Host: foo\r \n \r \n " , HandlerFunc (send204 ))
787
+ }
788
+
789
+ func TestHTTP10KeepAlive304Response (t * testing.T ) {
790
+ testTCPConnectionStaysOpen (t ,
791
+ "GET / HTTP/1.0\r \n Connection: keep-alive\r \n If-Modified-Since: Mon, 02 Jan 2006 15:04:05 GMT\r \n \r \n " ,
792
+ HandlerFunc (send304 ))
793
+ }
794
+
752
795
func TestSetsRemoteAddr_h1 (t * testing.T ) { testSetsRemoteAddr (t , h1Mode ) }
753
796
func TestSetsRemoteAddr_h2 (t * testing.T ) { testSetsRemoteAddr (t , h2Mode ) }
754
797
0 commit comments