Skip to content

Commit c664cb4

Browse files
authored
Add include opt (#20)
* 新增-i --include选项支持 * fix * fix
1 parent 4932e4e commit c664cb4

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pcurl是解析curl表达式的库
1414
* 支持--compressed选项
1515
* 支持-k, --insecure选项
1616
* 支持-G, --get选项
17+
* 支持-i, --include选项
1718
* 支持--data-urlencode选项
1819
* 支持内嵌到你的结构体里面,让你的cmd秒变curl
1920

pcurl.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ type Curl struct {
2828
DataUrlencode []string `clop:"--data-urlencode" usage:"HTTP POST data url encoded"`
2929

3030
Compressed bool `clop:"--compressed" usage:"Request compressed response"`
31-
Insecure bool `clop:"-k; --insecure" usage:"Allow insecure server connections when using SSL"`
32-
Err error
33-
p *clop.Clop
31+
// 在响应包里面打印http header, 仅做字段赋值
32+
Include bool `clop:"-i;--include" usage:"Include the HTTP response headers in the output. The HTTP response headers can include things like server name, cookies, date of the document, HTTP version and more."`
33+
Insecure bool `clop:"-k; --insecure" usage:"Allow insecure server connections when using SSL"`
34+
Err error
35+
p *clop.Clop
3436
}
3537

3638
const (
39+
bodyEmpby = "empty"
3740
bodyURLEncode = "data-urlencode"
3841
bodyForm = "form"
3942
bodyData = "data"
@@ -99,15 +102,23 @@ func (c *Curl) getBodyEncodeAndObj() (string, any, error) {
99102
return body.Unmarshal([]byte(c.Data))
100103
}
101104

105+
if name == bodyEmpby {
106+
return "", nil, nil
107+
}
108+
102109
return "", nil, ErrUnknownEncode
103110
}
104111

105112
func (c *Curl) findHighestPriority() string {
106113

107114
// 获取 --data-urlencoded,-F or --form, -d or --data, --data-raw的命令行优先级别
108115
// 绑定body和优先级的关系
109-
bodyName := bodyURLEncode
116+
bodyName := bodyEmpby
110117
max := uint64(0)
118+
if index, ok := c.p.GetIndexEx(bodyURLEncode); ok && index > max {
119+
bodyName = bodyURLEncode
120+
}
121+
111122
if index, ok := c.p.GetIndexEx(bodyForm); ok && index > max {
112123
bodyName = bodyForm
113124
}

pcurl_api_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,34 @@ func TestParseAndJSON(t *testing.T) {
2323
assert.NoError(t, err)
2424
fmt.Printf("%s\n", all)
2525
}
26+
27+
type testCaseObj struct {
28+
curl string
29+
url string
30+
body string
31+
}
32+
33+
func TestPaserAndObj(t *testing.T) {
34+
35+
tab := []testCaseObj{
36+
{
37+
curl: `curl -X POST -d '{"a":"b"}' www.qq.com/test`,
38+
url: `www.qq.com/test`,
39+
},
40+
{
41+
curl: `curl -X POST -i 'http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer <YourAppToken>' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"}}'`,
42+
url: `http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users`,
43+
},
44+
{
45+
curl: `curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer <YourAppToken> ' https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`,
46+
url: `https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`,
47+
},
48+
}
49+
50+
for _, tc := range tab {
51+
52+
all, err := ParseAndObj(tc.curl)
53+
assert.Equal(t, all.URL, tc.url)
54+
assert.NoError(t, err)
55+
}
56+
}

0 commit comments

Comments
 (0)