1- package self
1+ package qpack_test
22
33import (
44 "bytes"
5+ "fmt"
56 "math/rand/v2"
67 "testing"
78 _ "unsafe" // for go:linkname
@@ -34,19 +35,7 @@ func getEncoder() (*qpack.Encoder, *bytes.Buffer) {
3435 return qpack .NewEncoder (output ), output
3536}
3637
37- func TestEncodingAndDecodingSingleHeaderField (t * testing.T ) {
38- hf := qpack.HeaderField {
39- Name : randomString (15 ),
40- Value : randomString (15 ),
41- }
42- encoder , output := getEncoder ()
43- require .NoError (t , encoder .WriteField (hf ))
44- headerFields , err := qpack .NewDecoder (nil ).DecodeFull (output .Bytes ())
45- require .NoError (t , err )
46- require .Equal (t , []qpack.HeaderField {hf }, headerFields )
47- }
48-
49- func TestEncodingAndDecodingMultipleHeaderFields (t * testing.T ) {
38+ func TestEncodeDecode (t * testing.T ) {
5039 hfs := []qpack.HeaderField {
5140 {Name : "foo" , Value : "bar" },
5241 {Name : "lorem" , Value : "ipsum" },
@@ -61,34 +50,6 @@ func TestEncodingAndDecodingMultipleHeaderFields(t *testing.T) {
6150 require .Equal (t , hfs , headerFields )
6251}
6352
64- func TestEncodingAndDecodingMultipleRequests (t * testing.T ) {
65- hfs1 := []qpack.HeaderField {{Name : "foo" , Value : "bar" }}
66- hfs2 := []qpack.HeaderField {
67- {Name : "lorem" , Value : "ipsum" },
68- {Name : randomString (15 ), Value : randomString (20 )},
69- }
70- encoder , output := getEncoder ()
71- for _ , hf := range hfs1 {
72- require .NoError (t , encoder .WriteField (hf ))
73- }
74- req1 := append ([]byte {}, output .Bytes ()... )
75- output .Reset ()
76- for _ , hf := range hfs2 {
77- require .NoError (t , encoder .WriteField (hf ))
78- }
79- req2 := append ([]byte {}, output .Bytes ()... )
80-
81- var headerFields []qpack.HeaderField
82- decoder := qpack .NewDecoder (func (hf qpack.HeaderField ) { headerFields = append (headerFields , hf ) })
83- _ , err := decoder .Write (req1 )
84- require .NoError (t , err )
85- require .Equal (t , hfs1 , headerFields )
86- headerFields = nil
87- _ , err = decoder .Write (req2 )
88- require .NoError (t , err )
89- require .Equal (t , hfs2 , headerFields )
90- }
91-
9253// replace one character by a random character at a random position
9354func replaceRandomCharacter (s string ) string {
9455 pos := rand .IntN (len (s ))
@@ -105,13 +66,22 @@ func replaceRandomCharacter(s string) string {
10566
10667func check (t * testing.T , encoded []byte , hf qpack.HeaderField ) {
10768 t .Helper ()
69+
10870 headerFields , err := qpack .NewDecoder (nil ).DecodeFull (encoded )
10971 require .NoError (t , err )
11072 require .Len (t , headerFields , 1 )
11173 require .Equal (t , hf , headerFields [0 ])
11274}
11375
114- func TestUsingStaticTableForFieldNamesWithoutValues (t * testing.T ) {
76+ func TestStaticTableForFieldNamesWithoutValues (t * testing.T ) {
77+ for i := range 10 {
78+ t .Run (fmt .Sprintf ("run %d" , i ), func (t * testing.T ) {
79+ testStaticTableForFieldNamesWithoutValues (t )
80+ })
81+ }
82+ }
83+
84+ func testStaticTableForFieldNamesWithoutValues (t * testing.T ) {
11585 var hf qpack.HeaderField
11686 for {
11787 if entry := staticTable [rand .IntN (len (staticTable ))]; len (entry .Value ) == 0 {
@@ -131,7 +101,15 @@ func TestUsingStaticTableForFieldNamesWithoutValues(t *testing.T) {
131101 require .Greater (t , output .Len (), encodedLen )
132102}
133103
134- func TestUsingStaticTableForFieldNamesWithCustomValues (t * testing.T ) {
104+ func TestStaticTableForFieldNamesWithCustomValues (t * testing.T ) {
105+ for i := range 10 {
106+ t .Run (fmt .Sprintf ("run %d" , i ), func (t * testing.T ) {
107+ testStaticTableForFieldNamesWithCustomValues (t )
108+ })
109+ }
110+ }
111+
112+ func testStaticTableForFieldNamesWithCustomValues (t * testing.T ) {
135113 var hf qpack.HeaderField
136114 for {
137115 if entry := staticTable [rand .IntN (len (staticTable ))]; len (entry .Value ) == 0 {
@@ -155,6 +133,14 @@ func TestUsingStaticTableForFieldNamesWithCustomValues(t *testing.T) {
155133}
156134
157135func TestStaticTableForFieldNamesWithValues (t * testing.T ) {
136+ for i := range 10 {
137+ t .Run (fmt .Sprintf ("run %d" , i ), func (t * testing.T ) {
138+ testStaticTableForFieldNamesWithValues (t )
139+ })
140+ }
141+ }
142+
143+ func testStaticTableForFieldNamesWithValues (t * testing.T ) {
158144 var hf qpack.HeaderField
159145 for {
160146 // Only use values with at least 2 characters.
@@ -180,6 +166,14 @@ func TestStaticTableForFieldNamesWithValues(t *testing.T) {
180166}
181167
182168func TestStaticTableForFieldValues (t * testing.T ) {
169+ for i := range 10 {
170+ t .Run (fmt .Sprintf ("run %d" , i ), func (t * testing.T ) {
171+ testStaticTableForFieldValues (t )
172+ })
173+ }
174+ }
175+
176+ func testStaticTableForFieldValues (t * testing.T ) {
183177 var hf qpack.HeaderField
184178 for {
185179 // Only use values with at least 2 characters.
0 commit comments