-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcurl_x_www_form_urlencoded_test.go
105 lines (82 loc) · 2.4 KB
/
pcurl_x_www_form_urlencoded_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
package pcurl
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
)
type testWWWForm struct {
Hello string `form:"hello" www-form:"hello"`
Pcurl string `form:"pcurl" www-form:"pcurl"`
}
func createWWWForm(t *testing.T, need testWWWForm) *httptest.Server {
r := gin.New()
r.POST("/", func(c *gin.Context) {
wf := testWWWForm{}
var buf bytes.Buffer
_, err := io.Copy(&buf, c.Request.Body)
assert.NoError(t, err)
ioutil.NopCloser(&buf)
c.Request.Body = ioutil.NopCloser(bytes.NewReader(buf.Bytes()))
err = c.ShouldBind(&wf)
assert.NoError(t, err)
//err := c.ShouldBind(&wf)
assert.Equal(t, need, wf)
io.Copy(c.Writer, &buf)
})
return httptest.NewServer(http.HandlerFunc(r.ServeHTTP))
}
// --data-urlencode
func Test_DataURLEncode_Option(t *testing.T) {
type testData struct {
sendNeed testWWWForm
need string
curlSlice []string
curlString string
path string
}
sendRequest := func(d testData, index int, req *http.Request) {
got := ""
err := gout.New().SetRequest(req).Debug(true).BindBody(&got).Do()
assert.NoError(t, err, fmt.Sprintf("test index :%d", index))
assert.Equal(t, d.need, got, fmt.Sprintf("test index:%d", index))
}
for index, d := range []testData{
{
sendNeed: testWWWForm{Hello: "world", Pcurl: "pcurl"},
need: "hello=world&pcurl=pcurl",
curlSlice: []string{"curl", "-X", "POST", "--data-urlencode", "hello=world", "--data-urlencode", "pcurl=pcurl"},
curlString: `curl -X POST --data-urlencode hello=world --data-urlencode pcurl=pcurl`,
path: "/",
},
} {
// 创建测试服务
ts := createWWWForm(t, d.sendNeed)
// 生成curl slice
url := ts.URL
if len(d.path) > 0 {
url = url + d.path
}
// curlSlice追加url
curlSlice := append(d.curlSlice, url)
fmt.Printf("\nindex:%d#%s\n", index, curlSlice)
req, err := ParseSlice(curlSlice).Request()
assert.NoError(t, err, fmt.Sprintf("test index :%d", index))
// 发送请求
sendRequest(d, index, req)
// =========分隔符================
// 生成curl字符串
curlString := d.curlString + " " + url
fmt.Printf("\nindex:%d#%s\n", index, curlString)
req, err = ParseString(curlString).Request()
assert.NoError(t, err, fmt.Sprintf("test index :%d", index))
// 发送请求
sendRequest(d, index, req)
}
}