186 lines
4.1 KiB
Go
186 lines
4.1 KiB
Go
package envelopes
|
|
|
|
import (
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestMarshal(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dst []byte
|
|
label string
|
|
content []byte
|
|
expected string
|
|
}{
|
|
{
|
|
name: "empty dst and simple label",
|
|
dst: nil,
|
|
label: "EVENT",
|
|
content: []byte(`{"id":"test"}`),
|
|
expected: `["EVENT",{"id":"test"}]`,
|
|
},
|
|
{
|
|
name: "with existing dst",
|
|
dst: []byte("prefix"),
|
|
label: "REQ",
|
|
content: []byte(`"subscription"`),
|
|
expected: `prefix["REQ","subscription"]`,
|
|
},
|
|
{
|
|
name: "empty label",
|
|
dst: nil,
|
|
label: "",
|
|
content: []byte(`"test"`),
|
|
expected: `["","test"]`,
|
|
},
|
|
{
|
|
name: "empty content",
|
|
dst: nil,
|
|
label: "CLOSE",
|
|
content: []byte(""),
|
|
expected: `["CLOSE",]`,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
marshaller := func(dst []byte) []byte {
|
|
return append(dst, tt.content...)
|
|
}
|
|
result := Marshal(tt.dst, tt.label, marshaller)
|
|
if string(result) != tt.expected {
|
|
t.Errorf("Marshal() = %q, want %q", string(result), tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSkipToTheEnd(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input []byte
|
|
expectedRem []byte
|
|
expectedErr error
|
|
}{
|
|
{
|
|
name: "empty input",
|
|
input: []byte{},
|
|
expectedRem: []byte{},
|
|
expectedErr: nil,
|
|
},
|
|
{
|
|
name: "input with closing bracket",
|
|
input: []byte(`some data]`),
|
|
expectedRem: []byte{},
|
|
expectedErr: nil,
|
|
},
|
|
{
|
|
name: "input with closing bracket in middle",
|
|
input: []byte(`data]more`),
|
|
expectedRem: []byte{},
|
|
expectedErr: nil,
|
|
},
|
|
{
|
|
name: "input without closing bracket",
|
|
input: []byte(`some data without bracket`),
|
|
expectedRem: []byte{},
|
|
expectedErr: io.EOF,
|
|
},
|
|
{
|
|
name: "single closing bracket",
|
|
input: []byte(`]`),
|
|
expectedRem: []byte{},
|
|
expectedErr: nil,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
rem, err := SkipToTheEnd(tt.input)
|
|
if string(rem) != string(tt.expectedRem) {
|
|
t.Errorf("SkipToTheEnd() rem = %q, want %q", string(rem), string(tt.expectedRem))
|
|
}
|
|
if err != tt.expectedErr {
|
|
t.Errorf("SkipToTheEnd() err = %v, want %v", err, tt.expectedErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIdentify(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input []byte
|
|
expectedT string
|
|
expectedRem []byte
|
|
}{
|
|
{
|
|
name: "valid EVENT envelope",
|
|
input: []byte(`["EVENT",{"id":"test"}]`),
|
|
expectedT: "EVENT",
|
|
expectedRem: []byte(`{"id":"test"}]`),
|
|
},
|
|
{
|
|
name: "valid REQ envelope",
|
|
input: []byte(`["REQ","subscription",{"kinds":[1]}]`),
|
|
expectedT: "REQ",
|
|
expectedRem: []byte(`"subscription",{"kinds":[1]}]`),
|
|
},
|
|
{
|
|
name: "valid CLOSE envelope",
|
|
input: []byte(`["CLOSE","subscription"]`),
|
|
expectedT: "CLOSE",
|
|
expectedRem: []byte(`"subscription"]`),
|
|
},
|
|
{
|
|
name: "empty label",
|
|
input: []byte(`["",data]`),
|
|
expectedT: "",
|
|
expectedRem: []byte(`data]`),
|
|
},
|
|
{
|
|
name: "malformed - no opening bracket",
|
|
input: []byte(`"EVENT",data`),
|
|
expectedT: "",
|
|
expectedRem: []byte{},
|
|
},
|
|
{
|
|
name: "malformed - no quotes",
|
|
input: []byte(`[EVENT,data]`),
|
|
expectedT: "",
|
|
expectedRem: []byte{},
|
|
},
|
|
{
|
|
name: "malformed - no comma after label",
|
|
input: []byte(`["EVENT"data]`),
|
|
expectedT: "EVENT",
|
|
expectedRem: []byte{},
|
|
},
|
|
{
|
|
name: "empty input",
|
|
input: []byte{},
|
|
expectedT: "",
|
|
expectedRem: []byte{},
|
|
},
|
|
{
|
|
name: "incomplete envelope",
|
|
input: []byte(`["EVENT"`),
|
|
expectedT: "EVENT",
|
|
expectedRem: []byte{},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
label, rem := Identify(tt.input)
|
|
if label != tt.expectedT {
|
|
t.Errorf("Identify() label = %q, want %q", label, tt.expectedT)
|
|
}
|
|
if string(rem) != string(tt.expectedRem) {
|
|
t.Errorf("Identify() rem = %q, want %q", string(rem), string(tt.expectedRem))
|
|
}
|
|
})
|
|
}
|
|
}
|