This repository has been archived by the owner on May 13, 2023. It is now read-only.
forked from dgrr/http2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
frameHeader_test.go
89 lines (70 loc) · 1.55 KB
/
frameHeader_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package http2
import (
"bufio"
"bytes"
"io"
"testing"
"github.com/dgrr/http2/http2utils"
)
const (
testStr = "make fasthttp great again"
)
func TestFrameWrite(t *testing.T) {
fr := AcquireFrameHeader()
defer ReleaseFrameHeader(fr)
data := AcquireFrame(FrameData).(*Data)
fr.SetBody(data)
n, err := io.WriteString(data, testStr)
if err != nil {
t.Fatal(err)
}
if nn := len(testStr); n != nn {
t.Fatalf("unexpected size %d<>%d", n, nn)
}
var bf = bytes.NewBuffer(nil)
var bw = bufio.NewWriter(bf)
fr.WriteTo(bw)
bw.Flush()
b := bf.Bytes()
if str := string(b[9:]); str != testStr {
t.Fatalf("mismatch %s<>%s", str, testStr)
}
}
func TestFrameRead(t *testing.T) {
var h [9]byte
bf := bytes.NewBuffer(nil)
br := bufio.NewReader(bf)
http2utils.Uint24ToBytes(h[:3], uint32(len(testStr)))
n, err := bf.Write(h[:9])
if err != nil {
t.Fatal(err)
}
if n != 9 {
t.Fatalf("unexpected written bytes %d<>9", n)
}
n, err = io.WriteString(bf, testStr)
if err != nil {
t.Fatal(err)
}
if n != len(testStr) {
t.Fatalf("unexpected written bytes %d<>%d", n, len(testStr))
}
fr := AcquireFrameHeader()
defer ReleaseFrameHeader(fr)
nn, err := fr.ReadFrom(br)
if err != nil {
t.Fatal(err)
}
n = int(nn)
if n != len(testStr)+9 {
t.Fatalf("unexpected read bytes %d<>%d", n, len(testStr)+9)
}
if fr.Type() != FrameData {
t.Fatalf("unexpected frame type: %s. Expected Data", fr.Type())
}
data := fr.Body().(*Data)
if str := string(data.Data()); str != testStr {
t.Fatalf("mismatch %s<>%s", str, testStr)
}
}
// TODO: continue