Skip to content

Commit

Permalink
return write error when encoding header fields (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
kokes committed Sep 14, 2022
1 parent c931ad1 commit d3f95be
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions encoder.go
Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions encoder_test.go
Expand Up @@ -2,21 +2,36 @@ package qpack

import (
"bytes"
"io"

"golang.org/x/net/http2/hpack"

. "github.com/onsi/ginkgo"
. "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)
})

Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit d3f95be

Please sign in to comment.