forked from matrix-org/gomatrixserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientevent_test.go
103 lines (99 loc) · 3.2 KB
/
clientevent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* Copyright 2017 Vector Creations Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gomatrixserverlib
import (
"bytes"
"encoding/json"
"testing"
)
func TestToClientEvent(t *testing.T) { // nolint: gocyclo
ev, err := NewEventFromTrustedJSON([]byte(`{
"type": "m.room.name",
"state_key": "",
"event_id": "$test:localhost",
"room_id": "!test:localhost",
"sender": "@test:localhost",
"content": {
"name": "Hello World"
},
"origin_server_ts": 123456,
"unsigned": {
"prev_content": {
"name": "Goodbye World"
}
}
}`), false, RoomVersionV1)
if err != nil {
t.Fatalf("failed to create Event: %s", err)
}
ce := ToClientEvent(ev, FormatAll)
if ce.EventID != ev.EventID() {
t.Errorf("ClientEvent.EventID: wanted %s, got %s", ev.EventID(), ce.EventID)
}
if ce.OriginServerTS != ev.OriginServerTS() {
t.Errorf("ClientEvent.OriginServerTS: wanted %d, got %d", ev.OriginServerTS(), ce.OriginServerTS)
}
if ce.StateKey == nil || *ce.StateKey != "" {
t.Errorf("ClientEvent.StateKey: wanted '', got %v", ce.StateKey)
}
if ce.Type != ev.Type() {
t.Errorf("ClientEvent.Type: wanted %s, got %s", ev.Type(), ce.Type)
}
if !bytes.Equal(ce.Content, ev.Content()) {
t.Errorf("ClientEvent.Content: wanted %s, got %s", string(ev.Content()), string(ce.Content))
}
if !bytes.Equal(ce.Unsigned, ev.Unsigned()) {
t.Errorf("ClientEvent.Unsigned: wanted %s, got %s", string(ev.Unsigned()), string(ce.Unsigned))
}
if ce.Sender != ev.Sender() {
t.Errorf("ClientEvent.Sender: wanted %s, got %s", ev.Sender(), ce.Sender)
}
j, err := json.Marshal(ce)
if err != nil {
t.Fatalf("failed to Marshal ClientEvent: %s", err)
}
// Marshal sorts keys in structs by the order they are defined in the struct, which is alphabetical
out := `{"content":{"name":"Hello World"},"event_id":"$test:localhost","origin_server_ts":123456,` +
`"room_id":"!test:localhost","sender":"@test:localhost","state_key":"","type":"m.room.name",` +
`"unsigned":{"prev_content":{"name":"Goodbye World"}}}`
if !bytes.Equal([]byte(out), j) {
t.Errorf("ClientEvent marshalled to wrong bytes: wanted %s, got %s", out, string(j))
}
}
func TestToClientFormatSync(t *testing.T) {
ev, err := NewEventFromTrustedJSON([]byte(`{
"type": "m.room.name",
"state_key": "",
"event_id": "$test:localhost",
"room_id": "!test:localhost",
"sender": "@test:localhost",
"content": {
"name": "Hello World"
},
"origin_server_ts": 123456,
"unsigned": {
"prev_content": {
"name": "Goodbye World"
}
}
}`), false, RoomVersionV1)
if err != nil {
t.Fatalf("failed to create Event: %s", err)
}
ce := ToClientEvent(ev, FormatSync)
if ce.RoomID != "" {
t.Errorf("ClientEvent.RoomID: wanted '', got %s", ce.RoomID)
}
}