Skip to content

Commit d3f95be

Browse files
authored
return write error when encoding header fields (#28)
1 parent c931ad1 commit d3f95be

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

encoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func (e *Encoder) WriteField(f HeaderField) error {
5151
e.writeLiteralFieldWithoutNameReference(f)
5252
}
5353

54-
e.w.Write(e.buf)
54+
_, err := e.w.Write(e.buf)
5555
e.buf = e.buf[:0]
56-
return nil
56+
return err
5757
}
5858

5959
// Close declares that the encoding is complete and resets the Encoder

encoder_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@ package qpack
22

33
import (
44
"bytes"
5+
"io"
56

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

89
. "github.com/onsi/ginkgo"
910
. "github.com/onsi/gomega"
1011
)
1112

13+
// errWriter wraps bytes.Buffer and optionally fails on every write
14+
// useful for testing misbehaving writers
15+
type errWriter struct {
16+
bytes.Buffer
17+
fail bool
18+
}
19+
20+
func (ew *errWriter) Write(b []byte) (int, error) {
21+
if ew.fail {
22+
return 0, io.ErrClosedPipe
23+
}
24+
return ew.Buffer.Write(b)
25+
}
26+
1227
var _ = Describe("Encoder", func() {
1328
var (
1429
encoder *Encoder
15-
output *bytes.Buffer
30+
output *errWriter
1631
)
1732

1833
BeforeEach(func() {
19-
output = &bytes.Buffer{}
34+
output = &errWriter{}
2035
encoder = NewEncoder(output)
2136
})
2237

@@ -82,6 +97,12 @@ var _ = Describe("Encoder", func() {
8297
Expect(data).To(BeEmpty())
8398
})
8499

100+
It("encodes fails to encode when writer errs", func() {
101+
hf := HeaderField{Name: "foobar", Value: "lorem ipsum"}
102+
output.fail = true
103+
Expect(encoder.WriteField(hf)).To(MatchError("io: read/write on closed pipe"))
104+
})
105+
85106
It("encodes multiple fields", func() {
86107
hf1 := HeaderField{Name: "foobar", Value: "lorem ipsum"}
87108
hf2 := HeaderField{Name: "raboof", Value: "dolor sit amet"}

0 commit comments

Comments
 (0)