github.com/quic-go/quic-go@v0.44.0/http3/capsule_test.go (about) 1 package http3 2 3 import ( 4 "bytes" 5 "io" 6 7 "github.com/quic-go/quic-go/quicvarint" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 ) 12 13 var _ = Describe("Capsule", func() { 14 It("parses Capsules", func() { 15 b := quicvarint.Append(nil, 1337) 16 b = quicvarint.Append(b, 6) 17 b = append(b, []byte("foobar")...) 18 19 ct, r, err := ParseCapsule(bytes.NewReader(b)) 20 Expect(err).ToNot(HaveOccurred()) 21 Expect(ct).To(BeEquivalentTo(1337)) 22 val, err := io.ReadAll(r) 23 Expect(err).ToNot(HaveOccurred()) 24 Expect(string(val)).To(Equal("foobar")) 25 }) 26 27 It("writes capsules", func() { 28 var buf bytes.Buffer 29 Expect(WriteCapsule(&buf, 1337, []byte("foobar"))).To(Succeed()) 30 31 ct, r, err := ParseCapsule(&buf) 32 Expect(err).ToNot(HaveOccurred()) 33 Expect(ct).To(BeEquivalentTo(1337)) 34 val, err := io.ReadAll(r) 35 Expect(err).ToNot(HaveOccurred()) 36 Expect(string(val)).To(Equal("foobar")) 37 }) 38 39 It("errors on EOF", func() { 40 b := quicvarint.Append(nil, 1337) 41 b = quicvarint.Append(b, 6) 42 b = append(b, []byte("foobar")...) 43 44 for i := range b { 45 ct, r, err := ParseCapsule(bytes.NewReader(b[:i])) 46 if err != nil { 47 if i == 0 { 48 Expect(err).To(MatchError(io.EOF)) 49 } else { 50 Expect(err).To(MatchError(io.ErrUnexpectedEOF)) 51 } 52 continue 53 } 54 Expect(ct).To(BeEquivalentTo(1337)) 55 _, err = io.ReadAll(r) 56 Expect(err).To(Equal(io.ErrUnexpectedEOF)) 57 } 58 }) 59 })