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

View as plain text