-
Notifications
You must be signed in to change notification settings - Fork 2
/
payload_test.go
163 lines (151 loc) · 3.11 KB
/
payload_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
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
152
153
154
155
156
157
158
159
160
161
162
163
package gws
import (
"bytes"
"encoding/json"
"errors"
"testing"
)
func TestOpMessage_Unmarshal(t *testing.T) {
testCases := []struct {
Name string
JSON string
Payload payload
Err error
}{
{
Name: "NoPayload",
JSON: `
{
"type": "connection_init"
}
`,
},
{
Name: "WithRequest",
JSON: `
{
"id": "1",
"type": "start",
"payload": {
"query": "{ hello { world } }"
}
}`,
Payload: &Request{Query: "{ hello { world } }"},
},
{
Name: "WithResponse",
JSON: `
{
"id": "1",
"type": "data",
"payload": {
"data": {"hello":{"world":"this is a test"}}
}
}
`,
Payload: &Response{Data: json.RawMessage([]byte(`{"hello":{"world":"this is a test"}}`))},
},
{
Name: "Unordered",
JSON: `
{
"id": "1",
"payload": {
"query": "{ hello { world } }"
},
"type": "start"
}`,
Payload: &Request{Query: "{ hello { world } }"},
},
{
Name: "UnsupportedType",
JSON: `{"type":"asdgf", "payload": {}}`,
Err: ErrUnsupportedMsgType("asdgf"),
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(subT *testing.T) {
msg := new(operationMessage)
err := json.Unmarshal([]byte(testCase.JSON), msg)
t.Log(err)
switch {
case err != nil && testCase.Err == nil:
subT.Errorf("unexpected error when unmarshaling: %s", err)
return
case err == nil && testCase.Err != nil:
subT.Errorf("expected error: %s", testCase.Err)
return
case err != nil && !errors.Is(err, testCase.Err):
subT.Logf("expected error: %s, but got: %s", testCase.Err, err)
subT.Fail()
return
case testCase.Payload == nil:
return
case msg.Payload == nil:
subT.Logf("expected payload: %v, but got nothing", testCase.Payload)
subT.Fail()
return
}
comparePayloads(subT, testCase.Payload, msg.Payload)
})
}
}
const benchReq = `{
"id": "1",
"type": "start",
"payload": {
"query": "{ hello { world } }"
}
}`
func BenchmarkOpMessage_Unmarshal(b *testing.B) {
b.Run("Via UnmarshalJSON", func(subB *testing.B) {
for i := 0; i < subB.N; i++ {
msg := new(operationMessage)
err := msg.UnmarshalJSON([]byte(benchReq))
if err != nil {
subB.Error(err)
}
}
})
b.Run("Via json.Unmarshal", func(subB *testing.B) {
for i := 0; i < subB.N; i++ {
msg := new(operationMessage)
err := json.Unmarshal([]byte(benchReq), msg)
if err != nil {
subB.Error(err)
}
}
})
}
func comparePayloads(t *testing.T, ex, out payload) {
t.Helper()
switch u := ex.(type) {
case *Request:
v, ok := out.(*Request)
if !ok {
t.Logf("expected payload: request, but got: %#v", out)
t.Fail()
return
}
if v.Query != u.Query || v.OperationName != u.OperationName {
t.Logf("requests aren't equal: %v::%v", u, v)
t.Fail()
return
}
case *Response:
v, ok := out.(*Response)
if !ok {
t.Logf("expected payload: request, but got: %#v", out)
t.Fail()
return
}
if !bytes.Equal(u.Data, v.Data) {
t.Logf("expected data: %s, but got: %s", string(u.Data), string(v.Data))
t.Fail()
return
}
case unknown:
t.Log("expected payload is: unknown which should never happen")
t.Fail()
}
}