forked from go-telegram/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_request_form_test.go
107 lines (90 loc) · 3.72 KB
/
build_request_form_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
package bot
import (
"bytes"
"mime/multipart"
"strings"
"testing"
"github.com/go-telegram/bot/models"
)
func assertFormData(t *testing.T, data, expect string) {
if !strings.Contains(expect, "\r\n") {
expect = strings.Replace(expect, "\n", "\r\n", -1)
}
if data != expect {
t.Fatalf("\nExpect:\n%s\nGot:\n%s\n", expect, data)
}
}
func Test_buildRequestForm(t *testing.T) {
params := struct {
String string `json:"string"`
InputFileUpload *models.InputFileUpload `json:"input_file_upload"`
InputFileString *models.InputFileString `json:"input_file_string"`
InputMediaSlice []models.InputMedia `json:"input_media_slice"`
InlineQueryResultSlice []models.InlineQueryResult `json:"inline_query_result_slice"`
DefaultInt int `json:"default_int"`
InputMediaInterface models.InputMedia `json:"input_media_interface"`
InlineQueryResultInterface models.InlineQueryResult `json:"inline_query_result_interface"`
NoJSONTag1 string
NoJSONTag2 string `json:"-"`
OmitEmptyString string `json:"omit_empty_string,omitempty"`
}{
String: "foo",
InputFileUpload: &models.InputFileUpload{Filename: "file.png", Data: strings.NewReader("content input file upload")},
InputFileString: &models.InputFileString{Data: "content input file string"},
InputMediaSlice: []models.InputMedia{
&models.InputMediaPhoto{Media: "attach://foo.png", Caption: "bar", MediaAttachment: strings.NewReader("media attachment")},
&models.InputMediaPhoto{Media: "foobar", Caption: "bar"},
},
InlineQueryResultSlice: []models.InlineQueryResult{
&models.InlineQueryResultArticle{Title: "foo", Description: "bar", InputMessageContent: &models.InputTextMessageContent{MessageText: "foo"}},
},
DefaultInt: 42,
InputMediaInterface: &models.InputMediaPhoto{Media: "foo", Caption: "bar", ParseMode: "baz"},
InlineQueryResultInterface: &models.InlineQueryResultArticle{Title: "foo", Description: "bar", InputMessageContent: &models.InputTextMessageContent{MessageText: "foo"}},
NoJSONTag1: "foo",
NoJSONTag2: "bar",
OmitEmptyString: "",
}
buf := bytes.NewBuffer(nil)
form := multipart.NewWriter(buf)
form.SetBoundary("XXX") //nolint
fieldsCount, errBuild := buildRequestForm(form, ¶ms)
if errBuild != nil {
t.Error(errBuild)
return
}
form.Close()
expect := `--XXX
Content-Disposition: form-data; name="string"
foo
--XXX
Content-Disposition: form-data; name="input_file_upload"; filename="file.png"
Content-Type: application/octet-stream
content input file upload
--XXX
Content-Disposition: form-data; name="input_file_string"
content input file string
--XXX
Content-Disposition: form-data; name="foo.png"; filename="foo.png"
Content-Type: application/octet-stream
media attachment
--XXX
Content-Disposition: form-data; name="input_media_slice"
[{"type":"photo","media":"attach://foo.png","caption":"bar"},{"type":"photo","media":"foobar","caption":"bar"}]
--XXX
Content-Disposition: form-data; name="inline_query_result_slice"
[{"type":"article","id":"","title":"foo","input_message_content":{"message_text":"foo"},"description":"bar"}]
--XXX
Content-Disposition: form-data; name="default_int"
42
--XXX
Content-Disposition: form-data; name="input_media_interface"
{"type":"photo","media":"foo","caption":"bar","parse_mode":"baz"}
--XXX
Content-Disposition: form-data; name="inline_query_result_interface"
{"type":"article","id":"","title":"foo","input_message_content":{"message_text":"foo"},"description":"bar"}
--XXX--
`
assertEqualInt(t, fieldsCount, 6)
assertFormData(t, buf.String(), expect)
}