-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
209 lines (173 loc) · 5.86 KB
/
request_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package lumex
import (
"encoding/json"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTelegramError_Error(t *testing.T) {
method := "sendMessage"
params := map[string]string{"chat_id": "12345", "text": "Hello"}
code := 400
description := "Bad Request: chat not found"
responseParams := &ResponseParameters{MigrateToChatId: 67890, RetryAfter: 5}
telegramError := &TelegramError{
Method: method,
Params: params,
Code: code,
Description: description,
ResponseParams: responseParams,
}
errorMessage := telegramError.Error()
expectedMessage := "unable to sendMessage: Bad Request: chat not found"
if errorMessage != expectedMessage {
t.Errorf("unexpected error message: got %q, want %q", errorMessage, expectedMessage)
}
if telegramError.Method != method {
t.Errorf("unexpected Method: got %q, want %q", telegramError.Method, method)
}
if telegramError.Code != code {
t.Errorf("unexpected Code: got %d, want %d", telegramError.Code, code)
}
if telegramError.Description != description {
t.Errorf("unexpected Description: got %q, want %q", telegramError.Description, description)
}
if telegramError.ResponseParams.MigrateToChatId != 67890 {
t.Errorf("unexpected MigrateToChatID: got %d, want %d", telegramError.ResponseParams.MigrateToChatId, 67890)
}
if telegramError.ResponseParams.RetryAfter != 5 {
t.Errorf("unexpected RetryAfter: got %d, want %d", telegramError.ResponseParams.RetryAfter, 5)
}
}
func TestBaseBotClient_RequestWithContext(t *testing.T) {
if os.Getenv("TEST_BOT_TOKEN") == "" {
t.Skip()
}
client, err := NewBot(os.Getenv("TEST_BOT_TOKEN"), nil)
assert.NoError(t, err, "NewBot() failed")
t.Run("without parameters", func(t *testing.T) {
resp, err := client.RequestWithContext(nil, "getMe", nil, nil, nil)
assert.NoError(t, err, "RequestWithContext() failed")
assert.NotNil(t, resp, "Response is nil")
var u User
err = json.Unmarshal(resp, &u)
assert.NoError(t, err, "json.Unmarshal() failed")
assert.Equal(t, "lumex_test_bot", u.Username, "unexpected username")
})
t.Run("file by url", func(t *testing.T) {
v := map[string]string{
"chat_id": "1681111384",
}
data := map[string]FileReader{}
photo := InputFileByURL("https://telegram.org/img/t_logo.png")
v["photo"] = photo.getValue()
err := photo.Attach("photo.png", data)
assert.NoError(t, err, "Attach() failed")
resp, err := client.RequestWithContext(nil, "sendPhoto", v, data, nil)
assert.NoError(t, err, "RequestWithContext() failed")
assert.NotNil(t, resp, "Response is nil")
})
t.Run("file by reader", func(t *testing.T) {
v := map[string]string{
"chat_id": "1681111384",
}
data := map[string]FileReader{}
photo, err := http.Get("https://telegram.org/img/t_logo.png")
assert.NoError(t, err, "http.Get() failed")
photoReader := InputFileByReader("photo", photo.Body)
v["photo"] = photoReader.getValue()
err = photoReader.Attach("photo", data)
assert.NoError(t, err, "Attach() failed")
resp, err := client.RequestWithContext(nil, "sendPhoto", v, data, nil)
assert.NoError(t, err, "RequestWithContext() failed")
assert.NotNil(t, resp, "Response is nil")
})
t.Run("telegram error", func(t *testing.T) {
v := map[string]string{
"chat_id": "1",
"text": "Hello",
}
_, err := client.RequestWithContext(nil, "sendMessage", v, nil, nil)
assert.Error(t, err, "RequestWithContext() should fail")
assert.IsType(t, &TelegramError{}, err, "unexpected error type")
})
}
func TestBaseBotClient_GetAPIURL(t *testing.T) {
tests := []struct {
name string
opts *RequestOpts
defaultRequest *RequestOpts
expectedAPIURL string
}{
{
name: "RequestOpts has APIURL",
opts: &RequestOpts{APIURL: "https://custom.api.com/"},
defaultRequest: nil,
expectedAPIURL: "https://custom.api.com",
},
{
name: "DefaultRequestOpts has APIURL",
opts: nil,
defaultRequest: &RequestOpts{APIURL: "https://default.api.com/"},
expectedAPIURL: "https://default.api.com",
},
{
name: "Both RequestOpts and DefaultRequestOpts are nil",
opts: nil,
defaultRequest: nil,
expectedAPIURL: DefaultAPIURL,
},
{
name: "RequestOpts APIURL is empty, use DefaultRequestOpts",
opts: &RequestOpts{APIURL: ""},
defaultRequest: &RequestOpts{APIURL: "https://default.api.com/"},
expectedAPIURL: "https://default.api.com",
},
{
name: "Both RequestOpts and DefaultRequestOpts APIURL are empty",
opts: &RequestOpts{APIURL: ""},
defaultRequest: &RequestOpts{APIURL: ""},
expectedAPIURL: DefaultAPIURL,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bot := &BaseBotClient{
DefaultRequestOpts: tt.defaultRequest,
}
got := bot.GetAPIURL(tt.opts)
if got != tt.expectedAPIURL {
t.Errorf("GetAPIURL() = %v, want %v", got, tt.expectedAPIURL)
}
})
}
}
func TestBaseBotClient_getEnvAuth(t *testing.T) {
t.Run("test env", func(t *testing.T) {
client := BaseBotClient{
UseTestEnvironment: true,
}
auth := client.getEnvAuth("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
assert.Equal(t, "bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/test", auth, "unexpected auth")
})
t.Run("production env", func(t *testing.T) {
client := BaseBotClient{
UseTestEnvironment: false,
}
auth := client.getEnvAuth("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11")
assert.Equal(t, "bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", auth, "unexpected auth")
})
}
func TestBaseBotClient_FileURL(t *testing.T) {
client := BaseBotClient{
UseTestEnvironment: false,
}
fileURL := client.FileURL("123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", "photos/file_123.jpg", nil)
assert.Equal(
t,
"https://api.telegram.org/file/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/photos/file_123.jpg",
fileURL,
"unexpected file URL",
)
}