From d3f95be85571a643fe0fb8d9428a03fb70a540e1 Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Wed, 14 Sep 2022 20:56:17 +0200 Subject: [PATCH] return write error when encoding header fields (#28) --- encoder.go | 4 ++-- encoder_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/encoder.go b/encoder.go index 13e0ad2..ad69535 100644 --- a/encoder.go +++ b/encoder.go @@ -51,9 +51,9 @@ func (e *Encoder) WriteField(f HeaderField) error { e.writeLiteralFieldWithoutNameReference(f) } - e.w.Write(e.buf) + _, err := e.w.Write(e.buf) e.buf = e.buf[:0] - return nil + return err } // Close declares that the encoding is complete and resets the Encoder diff --git a/encoder_test.go b/encoder_test.go index 493f621..37165a6 100644 --- a/encoder_test.go +++ b/encoder_test.go @@ -2,6 +2,7 @@ package qpack import ( "bytes" + "io" "golang.org/x/net/http2/hpack" @@ -9,14 +10,28 @@ import ( . "github.com/onsi/gomega" ) +// errWriter wraps bytes.Buffer and optionally fails on every write +// useful for testing misbehaving writers +type errWriter struct { + bytes.Buffer + fail bool +} + +func (ew *errWriter) Write(b []byte) (int, error) { + if ew.fail { + return 0, io.ErrClosedPipe + } + return ew.Buffer.Write(b) +} + var _ = Describe("Encoder", func() { var ( encoder *Encoder - output *bytes.Buffer + output *errWriter ) BeforeEach(func() { - output = &bytes.Buffer{} + output = &errWriter{} encoder = NewEncoder(output) }) @@ -82,6 +97,12 @@ var _ = Describe("Encoder", func() { Expect(data).To(BeEmpty()) }) + It("encodes fails to encode when writer errs", func() { + hf := HeaderField{Name: "foobar", Value: "lorem ipsum"} + output.fail = true + Expect(encoder.WriteField(hf)).To(MatchError("io: read/write on closed pipe")) + }) + It("encodes multiple fields", func() { hf1 := HeaderField{Name: "foobar", Value: "lorem ipsum"} hf2 := HeaderField{Name: "raboof", Value: "dolor sit amet"}