-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathresponses_test.go
More file actions
151 lines (132 loc) · 3.44 KB
/
responses_test.go
File metadata and controls
151 lines (132 loc) · 3.44 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package rely
import (
"fmt"
"io"
"slices"
"testing"
"github.com/goccy/go-json"
"github.com/nbd-wtf/go-nostr"
)
func TestMarshalEventResponse(t *testing.T) {
tests := []struct {
response eventResponse
expected []byte
}{
{
response: eventResponse{
ID: "sub-1",
Event: &nostr.Event{},
},
expected: []byte(`["EVENT","sub-1",{"kind":0,"created_at":0,"tags":[],"content":""}]`),
},
{
response: eventResponse{
ID: `sub"123\id\n`, // unsafe ID
Event: &nostr.Event{},
},
expected: []byte(`["EVENT","sub\"123\\id\\n",{"kind":0,"created_at":0,"tags":[],"content":""}]`),
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("Case=%d", i), func(t *testing.T) {
res, err := test.response.MarshalJSON()
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
if !json.Valid(res) {
t.Fatalf("got invalid json %v", string(res))
}
if !slices.Equal(res, test.expected) {
t.Fatalf("expected %v, got %v", string(test.expected), string(res))
}
})
}
}
func TestMarshalRawEventResponse(t *testing.T) {
tests := []struct {
response rawEventResponse
expected []byte
}{
{
response: rawEventResponse{
ID: "sub-1",
Event: json.RawMessage(`{"kind":1,"content":"hello"}`),
},
expected: []byte(`["EVENT","sub-1",{"kind":1,"content":"hello"}]`),
},
{
response: rawEventResponse{
ID: `sub"123\id\n`, // unsafe ID
Event: json.RawMessage(`{"kind":1,"content":"hello"}`),
},
expected: []byte(`["EVENT","sub\"123\\id\\n",{"kind":1,"content":"hello"}]`),
},
}
for i, test := range tests {
t.Run(fmt.Sprintf("Case=%d", i), func(t *testing.T) {
res, err := test.response.MarshalJSON()
if err != nil {
t.Fatalf("expected nil, got %v", err)
}
if !json.Valid(res) {
t.Fatalf("got invalid json %v", string(res))
}
if !slices.Equal(res, test.expected) {
t.Fatalf("expected %v, got %v", string(test.expected), string(res))
}
})
}
}
var (
benchEvent = nostr.Event{
ID: "e0a28a87157353b28153e355b6e99ad9e1a097578039e079d4ade30075e5e3ce",
PubKey: "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
CreatedAt: nostr.Timestamp(1762099567),
Kind: 1,
Content: "hello from the nostr army knife",
Sig: "2ffc44c03fa03cc22134b660c2bdfe88f23a3d3076a2c8ef0d99a6e6d6615452a136fef689252e789e78e568e0fbc4cdb6bf7d8e396b73fa78b7c065c381620a",
}
benchEventResponse = eventResponse{ID: "testing", Event: &benchEvent}
)
func BenchmarkMarshalEventResponse(b *testing.B) {
for range b.N {
_, err := benchEventResponse.MarshalJSON()
if err != nil {
b.Fatalf("benchmark failed: %v", err)
}
}
}
func BenchmarkMarshalRawEventResponse(b *testing.B) {
bytes, err := benchEventResponse.Event.MarshalJSON()
if err != nil {
b.Fatalf("benchmark failed: %v", err)
}
response := rawEventResponse{
ID: benchEventResponse.ID,
Event: json.RawMessage(bytes),
}
b.ResetTimer()
for range b.N {
_, err := response.MarshalJSON()
if err != nil {
b.Fatalf("benchmark failed: %v", err)
}
}
}
func BenchmarkEncodeRawEventResponse2(b *testing.B) {
bytes, err := benchEventResponse.Event.MarshalJSON()
if err != nil {
b.Fatalf("benchmark failed: %v", err)
}
response := rawEventResponse{
ID: benchEventResponse.ID,
Event: json.RawMessage(bytes),
}
b.ResetTimer()
for range b.N {
err := json.NewEncoder(io.Discard).Encode(response)
if err != nil {
b.Fatalf("benchmark failed: %v", err)
}
}
}