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

Fix message encode/decode #609

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions ably/proto_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func unencodableDataErr(data interface{}) error {
return fmt.Errorf("message data type %T must be string, []byte, or a value that can be encoded as a JSON object or array", data)
}

// withEncodedData - Used to encode string, binary([]byte) or json data (TM3).
// withEncodedData - Used to encode string, binary([]byte) or json data (TM3, RSL4).
// Updates/Mutates Message.Data and Message.Encoding.
func (m Message) withEncodedData(cipher channelCipher) (Message, error) {
if m.Data == nil {
Expand Down Expand Up @@ -133,7 +133,7 @@ func (m Message) withEncodedData(cipher channelCipher) (Message, error) {
return m, nil
}

// withDecodedData - Used to decode received encoded data into string, binary([]byte) or json (TM3).
// withDecodedData - Used to decode received encoded data into string, binary([]byte) or json (TM3, RSL6).
func (m Message) withDecodedData(cipher channelCipher) (Message, error) {
// strings.Split on empty string returns []string{""}
if m.Data == nil || m.Encoding == "" {
Expand Down
13 changes: 13 additions & 0 deletions ably/proto_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ func TestMessage_EncodeDecode_TM3(t *testing.T) {
decoded: "a string",
encodedJSON: `{"data":"a string"}`,
},
{
desc: "with valid utf-8 json data in string format",
data: `{"key":"value"}`,
decoded: `{"key":"value"}`,
encodedJSON: `{"data":"{\"key\":\"value\"}"}`,
},
{
// invalid utf-8 string data should be base64 encoded
desc: "with invalid utf-8 string data",
Expand Down Expand Up @@ -87,6 +93,13 @@ func TestMessage_EncodeDecode_TM3(t *testing.T) {
decoded: []byte(ably.EncBase64),
encodedJSON: `{"data":"YmFzZTY0","encoding":"base64"}`,
},
{
desc: "with valid utf-8 json data in string format and cipher enabled",
data: `{"key":"value"}`,
decoded: `{"key":"value"}`,
opts: opts,
encodedJSON: `{"data":"HO4cYSP8LybPYBPZPHQOtlLxASbzZOh5h8lGaP3dX+M=","encoding":"utf-8/cipher+aes-128-cbc/base64"}`,
},
{
desc: "with json/utf-8/cipher+aes-128-cbc/base64",
data: map[string]interface{}{
Expand Down