Skip to content

Commit 805b90e

Browse files
committed
add test
1 parent c1701af commit 805b90e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

Diff for: http2/h2c/h2c_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net"
1414
"net/http"
1515
"net/http/httptest"
16+
"strings"
1617
"testing"
1718

1819
"golang.org/x/net/http2"
@@ -74,3 +75,62 @@ func TestContext(t *testing.T) {
7475
t.Fatal(err)
7576
}
7677
}
78+
79+
func TestPropagation(t *testing.T) {
80+
var (
81+
server *http.Server
82+
// double the limit because http2 will compress header
83+
headerSize = 1 << 11
84+
headerLimit = 1 << 10
85+
)
86+
87+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
88+
if r.ProtoMajor != 2 {
89+
t.Errorf("Request wasn't handled by h2c. Got ProtoMajor=%v", r.ProtoMajor)
90+
}
91+
if r.Context().Value(http.ServerContextKey).(*http.Server) != server {
92+
t.Errorf("Request doesn't have expected http server: %v", r.Context())
93+
}
94+
if len(r.Header.Get("Long-Header")) != headerSize {
95+
t.Errorf("Request doesn't have expected http header length: %v", len(r.Header.Get("Long-Header")))
96+
}
97+
fmt.Fprint(w, "Hello world")
98+
})
99+
100+
h2s := &http2.Server{}
101+
h1s := httptest.NewUnstartedServer(NewHandler(handler, h2s))
102+
103+
server = h1s.Config
104+
server.MaxHeaderBytes = headerLimit
105+
server.ConnState = func(conn net.Conn, state http.ConnState) {
106+
t.Logf("server conn state: conn %s -> %s, status changed to %s", conn.RemoteAddr(), conn.LocalAddr(), state)
107+
}
108+
109+
h1s.Start()
110+
defer h1s.Close()
111+
112+
client := &http.Client{
113+
Transport: &http2.Transport{
114+
AllowHTTP: true,
115+
DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
116+
conn, err := net.Dial(network, addr)
117+
if conn != nil {
118+
t.Logf("client dial tls: %s -> %s", conn.RemoteAddr(), conn.LocalAddr())
119+
}
120+
return conn, err
121+
},
122+
},
123+
}
124+
125+
req, err := http.NewRequest("GET", h1s.URL, nil)
126+
if err != nil {
127+
t.Fatal(err)
128+
}
129+
130+
req.Header.Set("Long-Header", strings.Repeat("A", headerSize))
131+
132+
_, err = client.Do(req)
133+
if err == nil {
134+
t.Fatal("expected server err, got nil")
135+
}
136+
}

0 commit comments

Comments
 (0)