github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/http3/capsule_test.go (about)

     1  package http3
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  
     7  	"github.com/apernet/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  		WriteCapsule(&buf, 1337, []byte("foobar"))
    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  				Expect(err).To(MatchError(io.ErrUnexpectedEOF))
    48  				continue
    49  			}
    50  			Expect(ct).To(BeEquivalentTo(1337))
    51  			_, err = io.ReadAll(r)
    52  			Expect(err).To(Equal(io.ErrUnexpectedEOF))
    53  		}
    54  	})
    55  })