support CLOSED messages.

This commit is contained in:
fiatjaf 2023-11-28 18:25:46 -03:00
parent b6ec7327ef
commit fa20f84ec7
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 45 additions and 1 deletions

View File

@ -37,6 +37,8 @@ func ParseMessage(message []byte) Envelope {
case bytes.Contains(label, []byte("CLOSE")):
x := CloseEnvelope("")
v = &x
case bytes.Contains(label, []byte("CLOSED")):
v = &ClosedEnvelope{}
default:
return nil
}
@ -255,12 +257,41 @@ func (v *CloseEnvelope) UnmarshalJSON(data []byte) error {
func (v CloseEnvelope) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
w.RawString(`["CLOSE",`)
w.RawString(`["CLOSED",`)
w.Raw(json.Marshal(string(v)))
w.RawString(`]`)
return w.BuildBytes()
}
type ClosedEnvelope struct {
SubscriptionID string
Reason string
}
func (_ ClosedEnvelope) Label() string { return "CLOSED" }
func (v *ClosedEnvelope) UnmarshalJSON(data []byte) error {
r := gjson.ParseBytes(data)
arr := r.Array()
switch len(arr) {
case 3:
*v = ClosedEnvelope{arr[1].Str, arr[2].Str}
return nil
default:
return fmt.Errorf("failed to decode CLOSED envelope")
}
}
func (v ClosedEnvelope) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
w.RawString(`["CLOSED",`)
w.Raw(json.Marshal(string(v.SubscriptionID)))
w.RawString(`,`)
w.Raw(json.Marshal(v.Reason))
w.RawString(`]`)
return w.BuildBytes()
}
type OKEnvelope struct {
EventID string
OK bool

View File

@ -88,6 +88,19 @@ func TestOKEnvelopeEncodingAndDecoding(t *testing.T) {
}
}
func TestClosedEnvelopeEncodingAndDecoding(t *testing.T) {
src := `["CLOSED","_","error: something went wrong"]`
var env ClosedEnvelope
json.Unmarshal([]byte(src), &env)
if env.SubscriptionID != "_" {
t.Error("failed to decode CLOSED")
}
if res, _ := json.Marshal(env); string(res) != src {
t.Errorf("failed to encode CLOSED: expected '%s', got '%s'", src, string(res))
}
}
func TestAuthEnvelopeEncodingAndDecoding(t *testing.T) {
authEnvelopes := []string{
`["AUTH","kjsabdlasb aslkd kasndkad \"as.kdnbskadb"]`,