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

Canonicalize Connect Streaming Trailer names #528

Merged
merged 1 commit into from
Jun 21, 2023
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
7 changes: 7 additions & 0 deletions protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,13 @@ func (u *connectStreamingUnmarshaler) Unmarshal(message any) *Error {
if err := json.Unmarshal(env.Data.Bytes(), &end); err != nil {
return errorf(CodeInternal, "unmarshal end stream message: %w", err)
}
for name, value := range end.Trailer {
canonical := http.CanonicalHeaderKey(name)
if name != canonical {
delete(end.Trailer, name)
end.Trailer[canonical] = append(end.Trailer[canonical], value...)
}
}
u.trailer = end.Trailer
u.endStreamErr = end.Error.asError()
return errSpecialEnvelope
Expand Down
39 changes: 39 additions & 0 deletions protocol_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package connect

import (
"bytes"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -54,3 +56,40 @@ func TestConnectErrorDetailMarshalingNoDescriptor(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, string(encoded), raw)
}

func TestConnectEndOfResponseCanonicalTrailers(t *testing.T) {
t.Parallel()

buffer := bytes.Buffer{}
bufferPool := newBufferPool()

endStreamMessage := connectEndStreamMessage{Trailer: make(http.Header)}
endStreamMessage.Trailer["not-canonical-header"] = []string{"a"}
endStreamMessage.Trailer["mixed-Canonical"] = []string{"b"}
endStreamMessage.Trailer["Mixed-Canonical"] = []string{"b"}
endStreamMessage.Trailer["Canonical-Header"] = []string{"c"}
endStreamData, err := json.Marshal(endStreamMessage)
assert.Nil(t, err)

writer := envelopeWriter{
writer: &buffer,
bufferPool: bufferPool,
}
err = writer.Write(&envelope{
Flags: connectFlagEnvelopeEndStream,
Data: bytes.NewBuffer(endStreamData),
})
assert.Nil(t, err)

unmarshaler := connectStreamingUnmarshaler{
envelopeReader: envelopeReader{
reader: &buffer,
bufferPool: bufferPool,
},
}
err = unmarshaler.Unmarshal(nil) // parameter won't be used
assert.ErrorIs(t, err, errSpecialEnvelope)
assert.Equal(t, unmarshaler.Trailer().Values("Not-Canonical-Header"), []string{"a"})
assert.Equal(t, unmarshaler.Trailer().Values("Mixed-Canonical"), []string{"b", "b"})
assert.Equal(t, unmarshaler.Trailer().Values("Canonical-Header"), []string{"c"})
}