Source file src/net/http/internal/http2/config_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http2_test
     6  
     7  import (
     8  	"net/http"
     9  	"testing"
    10  	"time"
    11  
    12  	. "net/http/internal/http2"
    13  )
    14  
    15  func TestConfigServerSettings(t *testing.T) { synctestTest(t, testConfigServerSettings) }
    16  func testConfigServerSettings(t testing.TB) {
    17  	config := &http.HTTP2Config{
    18  		MaxConcurrentStreams:          1,
    19  		MaxDecoderHeaderTableSize:     1<<20 + 2,
    20  		MaxEncoderHeaderTableSize:     1<<20 + 3,
    21  		MaxReadFrameSize:              1<<20 + 4,
    22  		MaxReceiveBufferPerConnection: 64<<10 + 5,
    23  		MaxReceiveBufferPerStream:     64<<10 + 6,
    24  	}
    25  	const maxHeaderBytes = 4096 + 7
    26  	st := newServerTester(t, nil, func(s *http.Server) {
    27  		s.MaxHeaderBytes = maxHeaderBytes
    28  		s.HTTP2 = config
    29  	})
    30  	st.writePreface()
    31  	st.writeSettings()
    32  	st.wantSettings(map[SettingID]uint32{
    33  		SettingMaxConcurrentStreams: uint32(config.MaxConcurrentStreams),
    34  		SettingHeaderTableSize:      uint32(config.MaxDecoderHeaderTableSize),
    35  		SettingInitialWindowSize:    uint32(config.MaxReceiveBufferPerStream),
    36  		SettingMaxFrameSize:         uint32(config.MaxReadFrameSize),
    37  		SettingMaxHeaderListSize:    maxHeaderBytes + (32 * 10),
    38  	})
    39  }
    40  
    41  func TestConfigTransportSettings(t *testing.T) { synctestTest(t, testConfigTransportSettings) }
    42  func testConfigTransportSettings(t testing.TB) {
    43  	config := &http.HTTP2Config{
    44  		MaxConcurrentStreams:          1, // ignored by Transport
    45  		MaxDecoderHeaderTableSize:     1<<20 + 2,
    46  		MaxEncoderHeaderTableSize:     1<<20 + 3,
    47  		MaxReadFrameSize:              1<<20 + 4,
    48  		MaxReceiveBufferPerConnection: 64<<10 + 5,
    49  		MaxReceiveBufferPerStream:     64<<10 + 6,
    50  	}
    51  	const maxHeaderBytes = 4096 + 7
    52  	tc := newTestClientConn(t, func(tr *http.Transport) {
    53  		tr.HTTP2 = config
    54  		tr.MaxResponseHeaderBytes = maxHeaderBytes
    55  	})
    56  	tc.wantSettings(map[SettingID]uint32{
    57  		SettingHeaderTableSize:   uint32(config.MaxDecoderHeaderTableSize),
    58  		SettingInitialWindowSize: uint32(config.MaxReceiveBufferPerStream),
    59  		SettingMaxFrameSize:      uint32(config.MaxReadFrameSize),
    60  		SettingMaxHeaderListSize: maxHeaderBytes + (32 * 10),
    61  	})
    62  	tc.wantWindowUpdate(0, uint32(config.MaxReceiveBufferPerConnection))
    63  }
    64  
    65  func TestConfigPingTimeoutServer(t *testing.T) { synctestTest(t, testConfigPingTimeoutServer) }
    66  func testConfigPingTimeoutServer(t testing.TB) {
    67  	st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
    68  	}, func(h2 *http.HTTP2Config) {
    69  		h2.SendPingTimeout = 2 * time.Second
    70  		h2.PingTimeout = 3 * time.Second
    71  	})
    72  	st.greet()
    73  
    74  	time.Sleep(2 * time.Second)
    75  	_ = readFrame[*PingFrame](t, st)
    76  	time.Sleep(3 * time.Second)
    77  	st.wantClosed()
    78  }
    79  
    80  func TestConfigPingTimeoutTransport(t *testing.T) { synctestTest(t, testConfigPingTimeoutTransport) }
    81  func testConfigPingTimeoutTransport(t testing.TB) {
    82  	tc := newTestClientConn(t, func(h2 *http.HTTP2Config) {
    83  		h2.SendPingTimeout = 2 * time.Second
    84  		h2.PingTimeout = 3 * time.Second
    85  	})
    86  	tc.greet()
    87  
    88  	req, _ := http.NewRequest("GET", "https://dummy.tld/", nil)
    89  	rt := tc.roundTrip(req)
    90  	tc.wantFrameType(FrameHeaders)
    91  
    92  	time.Sleep(2 * time.Second)
    93  	tc.wantFrameType(FramePing)
    94  	time.Sleep(3 * time.Second)
    95  	err := rt.err()
    96  	if err == nil {
    97  		t.Fatalf("expected connection to close")
    98  	}
    99  }
   100  

View as plain text