-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcurl_data_option_test.go
91 lines (75 loc) · 3.43 KB
/
pcurl_data_option_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
package pcurl
import (
"fmt"
"testing"
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
)
// curl -d
// curl --data
// curl --data-raw
func Test_Data_Option(t *testing.T) {
type testData struct {
need string
curlSlice []string
curlString string
path string
}
for index, d := range []testData{
{
need: `{"key":"val"}`,
curlSlice: []string{"curl", "-X", "POST", "-d", `{"key":"val"}`},
curlString: `curl -X POST -d '{"key":"val"}'`,
path: "/",
},
{ //测试-k选项
need: `{"key":"val"}`,
curlSlice: []string{"curl", "-k", "-X", "POST", "-d", `{"key":"val"}`},
curlString: `curl -k -X POST -d '{"key":"val"}'`,
path: "/",
},
{ //测试--insecure选项
need: `{"key":"val"}`,
curlSlice: []string{"curl", "--insecure", "-X", "POST", "-d", `{"key":"val"}`},
curlString: `curl --insecure -X POST -d '{"key":"val"}'`,
path: "/",
},
{
need: `{"type":"region","region":"bj","business":"asr","protocol":"private","connect":416"}`,
curlSlice: []string{"curl", "--location", "--request", "DELETE", "--header", "Content-Type: text/plain", "--data-raw", `{"type":"region","region":"bj","business":"asr","protocol":"private","connect":416"}`},
curlString: `curl --location --request DELETE --header 'Content-Type: text/plain' --data-raw '{"type":"region","region":"bj","business":"asr","protocol":"private","connect":416"}'`,
path: "/appkey/admin/v1/delete/connect/rule?appkey=xx",
},
{
need: `{"type":"region","list":[{"region":"sh","business":"asr","protocol":"http","connect":56},{"region":"bj","business":"asr","protocol":"websocket","connect":52},{"region":"bj","business":"asr","protocol":"private","connect":51}]}`,
curlSlice: []string{"curl", "--location", "--request", "POST", "--header", "Content-Type: text/plain", "--data-raw", `{"type":"region","list":[{"region":"sh","business":"asr","protocol":"http","connect":56},{"region":"bj","business":"asr","protocol":"websocket","connect":52},{"region":"bj","business":"asr","protocol":"private","connect":51}]}`},
curlString: `curl --location --request POST --header 'Content-Type: text/plain' --data-raw '{"type":"region","list":[{"region":"sh","business":"asr","protocol":"http","connect":56},{"region":"bj","business":"asr","protocol":"websocket","connect":52},{"region":"bj","business":"asr","protocol":"private","connect":51}]}'`,
path: "/appkey/admin/v1/add/connect/rule?appkey=xx",
},
} {
// 创建测试服务
ts := createGeneral2()
got := ""
// 生成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))
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)
// 生成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))
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))
}
}