Repository navigation
Expand file tree
/
Copy pathpadding_test.go
More file actions
75 lines (69 loc) · 2.11 KB
/
Copy pathpadding_test.go
File metadata and controls
75 lines (69 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package rhttp
import (
"io"
"net/http"
"testing"
"golang.org/x/net/http2"
)
// TestPaddedDataFlowControlCredit: a padded DATA frame counts its full
// payload (pad-length byte + data + padding) toward flow control per RFC
// 9113 §6.9.1, so the WINDOW_UPDATE refills must credit the full amount —
// crediting only the data bytes would slowly starve the sender's view of
// our receive window.
func TestPaddedDataFlowControlCredit(t *testing.T) {
bodyCh := make(chan []byte, 1)
peer, _, cleanup := newTestPeer(t, ServerOptions{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
bodyCh <- b
w.WriteHeader(http.StatusOK)
}),
})
defer cleanup()
peer.sendHeaders(1, false, ":method", "POST", ":path", "/")
data := []byte("hello")
pad := make([]byte, 10)
// Full flow-controlled payload: 1 pad-length byte + 5 data + 10 padding.
const wantCredit = 1 + 5 + 10
if err := peer.framer.WriteDataPadded(1, true, data, pad); err != nil {
t.Fatalf("write padded DATA: %v", err)
}
var connCredit, streamCredit uint32
for {
f := peer.readFrame()
switch f := f.(type) {
case *http2.WindowUpdateFrame:
switch f.Header().StreamID {
case 0:
connCredit += f.Increment
case 1:
streamCredit += f.Increment
}
case *http2.HeadersFrame:
if f.StreamID != 1 {
continue
}
if !f.StreamEnded() {
// Response body frames may follow; keep reading (the default
// 200 handler ends on HEADERS though).
continue
}
if connCredit != wantCredit {
t.Fatalf("conn-level WINDOW_UPDATE credited %d, want %d (padding must count)", connCredit, wantCredit)
}
if streamCredit != wantCredit {
t.Fatalf("stream-level WINDOW_UPDATE credited %d, want %d (padding must count)", streamCredit, wantCredit)
}
if got := <-bodyCh; string(got) != "hello" {
t.Fatalf("handler read body %q, want %q (padding must not leak into the body)", got, "hello")
}
return
case *http2.DataFrame:
if f.StreamEnded() {
t.Fatal("unexpected DATA END_STREAM before response HEADERS")
}
default:
t.Fatalf("unexpected frame %T", f)
}
}
}