-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_hello_test.go
88 lines (69 loc) · 2.01 KB
/
state_hello_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
package gateway
import (
"bytes"
"github.com/discordpkg/gateway/encoding"
"github.com/discordpkg/gateway/event/opcode"
"strings"
"testing"
)
func TestHelloState(t *testing.T) {
options := append(commonOptions, []Option{}...)
t.Run("initial state", func(t *testing.T) {
client := NewClientMust(t, options...)
if _, ok := client.ctx.state.(*HelloState); !ok {
t.Fatal("not hello state")
}
})
t.Run("invalid json", func(t *testing.T) {
client := NewClientMust(t, options...)
reader := strings.NewReader("{[7]]99{{")
_, err := client.ProcessNext(reader, nil)
if err == nil {
t.Error("expected error about json issue")
}
if _, ok := client.ctx.state.(*ClosedState); !ok {
t.Error("not closed state")
}
})
t.Run("unexpected operation", func(t *testing.T) {
client := NewClientMust(t, options...)
reader := strings.NewReader(`{"op":45}`)
_, err := client.ProcessNext(reader, nil)
if err == nil {
t.Error("expected to fail due to wrong op code")
}
if _, ok := client.ctx.state.(*ClosedState); !ok {
t.Error("not closed state")
}
})
t.Run("unexpected json payload", func(t *testing.T) {
client := NewClientMust(t, options...)
reader := strings.NewReader(`{"op":10,"d":{"heartbeat_interval":true}}`)
_, err := client.ProcessNext(reader, nil)
if err == nil {
t.Error("unmarshal should have failed")
}
if _, ok := client.ctx.state.(*ClosedState); !ok {
t.Error("not closed state")
}
})
t.Run("ok", func(t *testing.T) {
client := NewClientMust(t, options...)
reader := strings.NewReader(`{"op":10,"d":{"heartbeat_interval":45}}`)
buffer := &bytes.Buffer{}
_, err := client.ProcessNext(reader, buffer)
if err != nil {
t.Error(err)
}
if _, ok := client.ctx.state.(*ReadyState); !ok {
t.Fatal("not ready state")
}
var payload *Payload
if err = encoding.Unmarshal(buffer.Bytes(), &payload); err != nil {
t.Fatal("didn't write valid content to discord")
}
if payload.Op != opcode.Identify {
t.Error("didn't write identify op code")
}
})
}