Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return write error when encoding header fields #28

Merged
merged 1 commit into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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