Repository navigation
Expand file tree
/
Copy pathraw_request_test.go
More file actions
233 lines (208 loc) · 6.11 KB
/
Copy pathraw_request_test.go
File metadata and controls
233 lines (208 loc) · 6.11 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package bot
import (
"bytes"
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/go-telegram/bot/models"
)
type clientMock struct {
requestURI string
gotReq *http.Request
}
func (c *clientMock) Do(req *http.Request) (*http.Response, error) {
c.requestURI = req.URL.RequestURI()
c.gotReq = req
resp := http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"ok":true}`)),
}
return &resp, nil
}
func Test_rawRequest_url(t *testing.T) {
cm := &clientMock{}
b := &Bot{
token: "XXX",
client: cm,
}
err := b.rawRequest(context.Background(), "foo", nil, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cm.requestURI != "/botXXX/foo" {
t.Fatalf("unexpected requestURI: %s", cm.requestURI)
}
}
func Test_rawRequest_url_testEnv(t *testing.T) {
cm := &clientMock{}
b := &Bot{
token: "XXX",
client: cm,
testEnvironment: true,
}
err := b.rawRequest(context.Background(), "foo", nil, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cm.requestURI != "/botXXX/test/foo" {
t.Fatalf("unexpected requestURI: %s", cm.requestURI)
}
}
// The outgoing request must be replayable: a non-nil GetBody and a known
// ContentLength, with GetBody returning the same bytes as the body. That is what
// http2.Transport relies on to retry a POST after the server sends GOAWAY.
func Test_rawRequest_setsGetBody(t *testing.T) {
cm := &clientMock{}
b := &Bot{token: "XXX", client: cm}
err := b.rawRequest(context.Background(), "sendMessage", &SendMessageParams{
ChatID: 123,
Text: "hello",
}, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
req := cm.gotReq
if req == nil {
t.Fatal("clientMock didn't observe a request")
}
if req.GetBody == nil {
t.Fatal("Request.GetBody is nil — http2.Transport cannot retry on GOAWAY")
}
if req.ContentLength <= 0 {
t.Fatalf("Request.ContentLength is %d, expected > 0", req.ContentLength)
}
original, err := io.ReadAll(req.Body)
if err != nil {
t.Fatalf("read original body: %v", err)
}
if int64(len(original)) != req.ContentLength {
t.Fatalf("body length %d != ContentLength %d", len(original), req.ContentLength)
}
// Two GetBody calls must each return the full body — this is what
// http2.Transport does on retry, and it can be invoked more than
// once if a connection is dropped multiple times in quick succession.
for i := 0; i < 2; i++ {
rc, err := req.GetBody()
if err != nil {
t.Fatalf("GetBody call %d returned error: %v", i, err)
}
replay, err := io.ReadAll(rc)
if err != nil {
t.Fatalf("read replayed body call %d: %v", i, err)
}
if err := rc.Close(); err != nil {
t.Fatalf("close replayed body call %d: %v", i, err)
}
if !bytes.Equal(original, replay) {
t.Fatalf("replayed body call %d differs from original (lens %d vs %d)",
i, len(replay), len(original))
}
}
// The body should also contain the expected multipart fields,
// proving GetBody isn't just returning empty bytes that happen
// to match an empty original.
if !bytes.Contains(original, []byte(`name="chat_id"`)) {
t.Errorf("body missing chat_id field")
}
if !bytes.Contains(original, []byte(`name="text"`)) {
t.Errorf("body missing text field")
}
if !bytes.Contains(original, []byte("hello")) {
t.Errorf("body missing text value")
}
}
// A method without fields (getMe, or a params struct whose fields are all omitted)
// must send neither a body nor a multipart Content-Type. Local telegram-bot-api
// servers reject an empty multipart body with a bare 400 (#285).
func Test_rawRequest_noBodyWithoutFields(t *testing.T) {
type emptyParams struct {
A string `json:"a,omitempty"`
}
cases := []struct {
name string
params any
}{
{"nil params", nil},
{"typed nil params", (*emptyParams)(nil)},
{"all fields omitted", &emptyParams{}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cm := &clientMock{}
b := &Bot{token: "XXX", client: cm}
if requestErr := b.rawRequest(context.Background(), "getMe", c.params, nil); requestErr != nil {
t.Fatalf("unexpected error: %v", requestErr)
}
req := cm.gotReq
if got := req.Header.Get("Content-Type"); got != "" {
t.Fatalf("expected no Content-Type, got %q", got)
}
if req.ContentLength != 0 {
t.Fatalf("expected ContentLength 0, got %d", req.ContentLength)
}
if req.Body != http.NoBody {
t.Fatalf("expected http.NoBody, got %T", req.Body)
}
})
}
}
// A request whose only field is a custom-marshaled or InputMedia value is not empty:
// the field is written to the form and must be counted, or the body would be dropped.
func Test_rawRequest_customMarshalAndInputMediaFieldsCount(t *testing.T) {
cases := []struct {
name string
params any
part string
}{
{
"custom marshal field",
&struct {
Scope *models.BotCommandScopeDefault `json:"scope,omitempty"`
}{Scope: &models.BotCommandScopeDefault{}},
`name="scope"`,
},
{
"custom marshal behind interface",
&struct {
Scope models.BotCommandScope `json:"scope,omitempty"`
}{Scope: &models.BotCommandScopeDefault{}},
`name="scope"`,
},
{
"input media field",
&struct {
Media *models.InputMediaPhoto `json:"media,omitempty"`
}{Media: &models.InputMediaPhoto{Media: "file_id"}},
`name="media"`,
},
{
"input media behind interface",
&struct {
Media models.InputMedia `json:"media,omitempty"`
}{Media: &models.InputMediaPhoto{Media: "file_id"}},
`name="media"`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cm := &clientMock{}
b := &Bot{token: "XXX", client: cm}
if requestErr := b.rawRequest(context.Background(), "editMessageMedia", c.params, nil); requestErr != nil {
t.Fatalf("unexpected error: %v", requestErr)
}
req := cm.gotReq
if got := req.Header.Get("Content-Type"); !strings.HasPrefix(got, "multipart/form-data") {
t.Fatalf("expected a multipart Content-Type, got %q", got)
}
body, readErr := io.ReadAll(req.Body)
if readErr != nil {
t.Fatal(readErr)
}
if !bytes.Contains(body, []byte(c.part)) {
t.Fatalf("body must contain %s, got:\n%s", c.part, body)
}
})
}
}