-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
66 lines (57 loc) · 1.45 KB
/
api_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
package crare
import (
"testing"
"github.com/3JoB/ulib/pool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExtractOk(t *testing.T) {
buf := pool.NewBuffer()
data := `{"ok": true, "result": {}}`
buf.WriteString(data)
require.NoError(t, extractOk(buf))
buf.Reset()
data = `{
"ok": false,
"error_code": 400,
"description": "Bad Request: reply message not found"
}`
buf.WriteString(data)
assert.EqualError(t, extractOk(buf), ErrNotFoundToReply.Error())
buf.Reset()
data = `{
"ok": false,
"error_code": 429,
"description": "Too Many Requests: retry after 8",
"parameters": {"retry_after": 8}
}`
buf.WriteString(data)
assert.Equal(t, FloodError{
err: NewError(429, "Too Many Requests: retry after 8"),
RetryAfter: 8,
}, extractOk(buf))
buf.Reset()
data = `{
"ok": false,
"error_code": 400,
"description": "Bad Request: group chat was upgraded to a supergroup chat",
"parameters": {"migrate_to_chat_id": -100123456789}
}`
buf.WriteString(data)
assert.Equal(t, GroupError{
err: ErrGroupMigrated,
MigratedTo: -100123456789,
}, extractOk(buf))
}
func TestExtractMessage(t *testing.T) {
buf := pool.NewBuffer()
data := `{"ok":true,"result":true}`
buf.WriteString(data)
_, err := extractMessage(buf)
assert.Equal(t, ErrTrueResult, err)
buf.Reset()
data = `{"ok":true,"result":{"foo":"bar"}}`
buf.WriteString(data)
_, err = extractMessage(buf)
require.NoError(t, err)
}