Skip to content
Open
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
3 changes: 2 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,13 @@ func (s *Service) saveInboxMessage(frame *Frame, from protocol.Addr) error {
msg := map[string]interface{}{
"type": TypeName(frame.Type),
"from": from.String(),
"data": string(frame.Payload),
"bytes": len(frame.Payload),
"received_at": ts.Format(time.RFC3339Nano),
}
if s.cfg.IncludeBase64 {
msg["data_b64"] = base64.StdEncoding.EncodeToString(frame.Payload)
} else {
msg["data"] = string(frame.Payload)
}
data, err := json.Marshal(msg)
if err != nil {
Expand Down
26 changes: 20 additions & 6 deletions zz_more_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package dataexchange
import (
"bytes"
"context"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
Expand Down Expand Up @@ -252,12 +253,25 @@ func TestService_SaveInboxMessage_WritesJSON(t *testing.T) {
if msg["type"] != "TEXT" {
t.Errorf("type = %v, want TEXT", msg["type"])
}
if msg["data"] != "hello" {
t.Errorf("data = %v, want hello", msg["data"])
}
_, hasB64 := msg["data_b64"]
if hasB64 != b64 {
t.Errorf("has data_b64 = %v, want %v", hasB64, b64)
if b64 {
// When IncludeBase64 is true, data_b64 is the canonical field;
// data must NOT be present to avoid 2× payload blowup.
if _, hasRaw := msg["data"]; hasRaw {
t.Errorf("data = %v, want absent when IncludeBase64=true", msg["data"])
}
b64Val, hasB64 := msg["data_b64"]
if !hasB64 {
t.Error("data_b64 missing when IncludeBase64=true")
} else if b64Val != base64.StdEncoding.EncodeToString([]byte("hello")) {
t.Errorf("data_b64 = %v, want %q", b64Val, base64.StdEncoding.EncodeToString([]byte("hello")))
}
} else {
if msg["data"] != "hello" {
t.Errorf("data = %v, want hello", msg["data"])
}
if _, hasB64 := msg["data_b64"]; hasB64 {
t.Error("data_b64 present when IncludeBase64=false")
}
}
})
}
Expand Down
Loading