-
Notifications
You must be signed in to change notification settings - Fork 115
/
import_rawtext_test.go
189 lines (145 loc) · 4.14 KB
/
import_rawtext_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
package gout
import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
type testRawText struct {
A string `form:"a" json:"a"`
B string `form:"b" json:"b"`
}
func setup_router(t *testing.T) *gin.Engine {
router := gin.New()
router.POST("/", func(c *gin.Context) {
rt := testRawText{}
err := c.Bind(&rt)
assert.NoError(t, err)
rt2 := testRawText{"a", "b"}
assert.Equal(t, rt, rt2)
})
return router
}
func Test_RawText(t *testing.T) {
all, err := ioutil.ReadFile("./testdata/raw-http-post-formdata.txt")
assert.NoError(t, err)
router := setup_router(t)
code := 0
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
err = NewImport().RawText(all).Debug(true).SetURL(ts.URL).Code(&code).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
}
// 支持导入json
func Test_RawText_JSON(t *testing.T) {
all, err := ioutil.ReadFile("./testdata/raw-http-post-json.txt")
assert.NoError(t, err)
type obj struct {
A int `json:"a"`
B int `json:"b"`
}
type result struct {
Result string `json:"result"`
}
type testRawTextJSON struct {
Array []string `json:"array"`
Num int `json:"num"`
Str string `json:"str"`
Obj obj `json:"obj"`
}
router := func() *gin.Engine {
router := gin.New()
router.POST("/colorjson", func(c *gin.Context) {
rt := testRawTextJSON{}
err := c.ShouldBindJSON(&rt)
assert.NoError(t, err)
rt2 := testRawTextJSON{Array: []string{"foo", "bar", "baz"}, Str: "foo", Num: 100, Obj: obj{A: 1, B: 2}}
assert.Equal(t, rt, rt2)
c.JSON(200, gin.H{"result": "ok"})
})
return router
}()
code := 0
var r, r1 result
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
for _, testURL := range []string{ts.URL + "/colorjson"} {
err = NewImport().RawText(all).Debug(true).SetURL(testURL).Code(&code).BindJSON(&r).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
r1 = result{"ok"}
assert.Equal(t, r, r1)
}
for _, testURL := range []string{ts.URL} {
err = NewImport().RawText(all).Debug(true).SetHost(testURL).Code(&code).BindJSON(&r).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
r1 = result{"ok"}
assert.Equal(t, r, r1)
}
}
// 修改URL
func Test_RawText_URL(t *testing.T) {
all, err := ioutil.ReadFile("./testdata/raw-http-post-formdata.txt")
assert.NoError(t, err)
type testRawText struct {
A string `form:"a"`
B string `form:"b"`
}
router := func() *gin.Engine {
router := gin.New()
router.POST("/", func(c *gin.Context) {
rt := testRawText{}
err := c.Bind(&rt)
assert.NoError(t, err)
rt2 := testRawText{"a", "b"}
assert.Equal(t, rt, rt2)
rt = testRawText{}
rt2 = testRawText{"q1", "q2"}
err = c.ShouldBindQuery(&rt)
assert.NoError(t, err)
assert.Equal(t, rt, rt2)
})
return router
}()
code := 0
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
err = NewImport().RawText(all).Debug(true).SetURL(ts.URL).
SetQuery(H{"a": "q1", "b": "q2"}).
Code(&code).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
}
// 测试错误情况
func Test_RawText_FAILED(t *testing.T) {
router := setup_router(t)
code := 0
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
for _, v := range []interface{}{new(int), "xxxx"} {
err := NewImport().RawText(v).Debug(true).SetURL(ts.URL).Code(&code).Do()
assert.Error(t, err)
}
for _, v := range []interface{}{new(int), "xxxx"} {
err := NewImport().RawText(v).Debug(true).SetHost(ts.URL).Code(&code).Do()
assert.Error(t, err)
}
for _, v := range []interface{}{new(int), ""} {
req, err := NewImport().RawText(v).Debug(true).SetURL(ts.URL).Request()
assert.Error(t, err)
assert.Nil(t, req)
}
}
// 测试支持的类型
func Test_RawText_MoreType(t *testing.T) {
all, err := ioutil.ReadFile("./testdata/raw-http-post-formdata.txt")
assert.NoError(t, err)
router := setup_router(t)
code := 0
ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
for _, v := range []interface{}{all, string(all)} {
err = NewImport().RawText(v).Debug(true).SetURL(ts.URL).Code(&code).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
}
}