github.com/tumi8/quic-go@v0.37.4-tum/http3/capsule_test.go (about)

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