-
Notifications
You must be signed in to change notification settings - Fork 3
/
getting-updates_test.go
168 lines (130 loc) · 3.44 KB
/
getting-updates_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
164
165
166
167
168
package telegram_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/nasermirzaei89/telegram"
)
func TestGetUpdates(t *testing.T) {
ctx := context.Background()
response := []byte(`{
"ok": true,
"result": [
{
"update_id": 109399605,
"message": {
"message_id": 1234,
"from": {
"id": 123456789,
"is_bot": false,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"language_code": "en"
},
"chat": {
"id": 123456789,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"type": "private"
},
"date": 1234567890,
"text": "Test"
}
}
]
}`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := fmt.Sprintf("/bot%s/getUpdates", testToken)
if r.URL.String() != u {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"ok":false,"error_code":401,"description":"Unauthorized"}`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}))
defer server.Close()
// success
bot := telegram.New(testToken, telegram.SetBaseURL(server.URL))
res, err := bot.GetUpdates(ctx)
if err != nil {
t.Errorf("error on get updates: %s", err.Error())
return
}
if !res.IsOK() {
t.Error("result should be ok but is not")
return
}
if res.GetErrorCode() != 0 {
t.Errorf("result error code should be zero but is %d", res.GetErrorCode())
return
}
updates := res.GetUpdates()
if updates == nil {
t.Error("result updates should be array but are nil")
return
}
for _, u := range updates {
if u.UpdateID == 0 {
t.Error("update is should not be zero but is")
}
}
// fail
bot = telegram.New(invalidToken, telegram.SetBaseURL(server.URL))
res, err = bot.GetUpdates(ctx)
if err != nil {
t.Errorf("error on get updates: %s", err.Error())
return
}
if res.IsOK() {
t.Error("result should not be ok but is")
return
}
if res.GetErrorCode() != http.StatusUnauthorized {
t.Errorf("result error code should be %d but is %d", http.StatusUnauthorized, res.GetErrorCode())
return
}
}
func TestSetWebhook(t *testing.T) {
ctx := context.Background()
response := []byte(`{"ok":true,"result":true,"description":"Webhook was set"}`)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u := fmt.Sprintf("/bot%s/setWebhook", testToken)
if r.URL.String() != u {
w.WriteHeader(http.StatusUnauthorized)
_, _ = w.Write([]byte(`{"ok":false,"error_code":401,"description":"Unauthorized"}`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
}))
defer server.Close()
// success
bot := telegram.New(testToken, telegram.SetBaseURL(server.URL))
res, err := bot.SetWebhook(
ctx,
telegram.SetURL("https://example.com/telegram/webhook"),
telegram.SetMaxConnections(40),
telegram.SetAllowedUpdates("message"),
)
if err != nil {
t.Errorf("error on set web hook: %s", err.Error())
return
}
if !res.IsOK() {
t.Error("result should be ok but is not")
return
}
if res.GetErrorCode() != 0 {
t.Errorf("result error code should be zero but is %d", res.GetErrorCode())
return
}
expectedDesc := "Webhook was set"
if desc := res.GetDescription(); desc != expectedDesc {
t.Errorf("result description should be '%s' but is '%s'", expectedDesc, desc)
}
}