github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/http3/body_test.go (about)

     1  package http3
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/TugasAkhir-QUIC/quic-go"
     7  	mockquic "github.com/TugasAkhir-QUIC/quic-go/internal/mocks/quic"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  	"go.uber.org/mock/gomock"
    12  )
    13  
    14  var _ = Describe("Response Body", func() {
    15  	var reqDone chan struct{}
    16  
    17  	BeforeEach(func() { reqDone = make(chan struct{}) })
    18  
    19  	It("closes the reqDone channel when Read errors", func() {
    20  		str := mockquic.NewMockStream(mockCtrl)
    21  		str.EXPECT().Read(gomock.Any()).Return(0, errors.New("test error"))
    22  		rb := newResponseBody(str, nil, reqDone)
    23  		_, err := rb.Read([]byte{0})
    24  		Expect(err).To(MatchError("test error"))
    25  		Expect(reqDone).To(BeClosed())
    26  	})
    27  
    28  	It("allows multiple calls to Read, when Read errors", func() {
    29  		str := mockquic.NewMockStream(mockCtrl)
    30  		str.EXPECT().Read(gomock.Any()).Return(0, errors.New("test error")).Times(2)
    31  		rb := newResponseBody(str, nil, reqDone)
    32  		_, err := rb.Read([]byte{0})
    33  		Expect(err).To(HaveOccurred())
    34  		Expect(reqDone).To(BeClosed())
    35  		_, err = rb.Read([]byte{0})
    36  		Expect(err).To(HaveOccurred())
    37  	})
    38  
    39  	It("closes responses", func() {
    40  		str := mockquic.NewMockStream(mockCtrl)
    41  		rb := newResponseBody(str, nil, reqDone)
    42  		str.EXPECT().CancelRead(quic.StreamErrorCode(ErrCodeRequestCanceled))
    43  		Expect(rb.Close()).To(Succeed())
    44  	})
    45  
    46  	It("allows multiple calls to Close", func() {
    47  		str := mockquic.NewMockStream(mockCtrl)
    48  		rb := newResponseBody(str, nil, reqDone)
    49  		str.EXPECT().CancelRead(quic.StreamErrorCode(ErrCodeRequestCanceled)).MaxTimes(2)
    50  		Expect(rb.Close()).To(Succeed())
    51  		Expect(reqDone).To(BeClosed())
    52  		Expect(rb.Close()).To(Succeed())
    53  	})
    54  })