From c664cb4e23ffbefdb23327359164a9c18b5497a8 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Mon, 13 Mar 2023 23:15:42 +0800 Subject: [PATCH] Add include opt (#20) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增-i --include选项支持 * fix * fix --- README.md | 1 + pcurl.go | 19 +++++++++++++++---- pcurl_api_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1fda979..17b3c4b 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ pcurl是解析curl表达式的库 * 支持--compressed选项 * 支持-k, --insecure选项 * 支持-G, --get选项 +* 支持-i, --include选项 * 支持--data-urlencode选项 * 支持内嵌到你的结构体里面,让你的cmd秒变curl diff --git a/pcurl.go b/pcurl.go index b04ea9c..18174f8 100644 --- a/pcurl.go +++ b/pcurl.go @@ -28,12 +28,15 @@ type Curl struct { DataUrlencode []string `clop:"--data-urlencode" usage:"HTTP POST data url encoded"` Compressed bool `clop:"--compressed" usage:"Request compressed response"` - Insecure bool `clop:"-k; --insecure" usage:"Allow insecure server connections when using SSL"` - Err error - p *clop.Clop + // 在响应包里面打印http header, 仅做字段赋值 + 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."` + Insecure bool `clop:"-k; --insecure" usage:"Allow insecure server connections when using SSL"` + Err error + p *clop.Clop } const ( + bodyEmpby = "empty" bodyURLEncode = "data-urlencode" bodyForm = "form" bodyData = "data" @@ -99,6 +102,10 @@ func (c *Curl) getBodyEncodeAndObj() (string, any, error) { return body.Unmarshal([]byte(c.Data)) } + if name == bodyEmpby { + return "", nil, nil + } + return "", nil, ErrUnknownEncode } @@ -106,8 +113,12 @@ func (c *Curl) findHighestPriority() string { // 获取 --data-urlencoded,-F or --form, -d or --data, --data-raw的命令行优先级别 // 绑定body和优先级的关系 - bodyName := bodyURLEncode + bodyName := bodyEmpby max := uint64(0) + if index, ok := c.p.GetIndexEx(bodyURLEncode); ok && index > max { + bodyName = bodyURLEncode + } + if index, ok := c.p.GetIndexEx(bodyForm); ok && index > max { bodyName = bodyForm } diff --git a/pcurl_api_test.go b/pcurl_api_test.go index 5aeab88..11886bf 100644 --- a/pcurl_api_test.go +++ b/pcurl_api_test.go @@ -23,3 +23,34 @@ func TestParseAndJSON(t *testing.T) { assert.NoError(t, err) fmt.Printf("%s\n", all) } + +type testCaseObj struct { + curl string + url string + body string +} + +func TestPaserAndObj(t *testing.T) { + + tab := []testCaseObj{ + { + curl: `curl -X POST -d '{"a":"b"}' www.qq.com/test`, + url: `www.qq.com/test`, + }, + { + curl: `curl -X POST -i 'http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer ' -d '{"from": "user1","to": ["user2"],"type": "txt","body": {"msg": "testmessages"}}'`, + url: `http://{{.Host}}/{{.OrgName}}/{{.AppName}}/messages/users`, + }, + { + curl: `curl -X DELETE -H 'Accept: application/json' -H 'Authorization: Bearer ' https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`, + url: `https://{{.Host}}/{{.OrgName}}/{{.AppName}}/chatgroups/{{.GroupID}}`, + }, + } + + for _, tc := range tab { + + all, err := ParseAndObj(tc.curl) + assert.Equal(t, all.URL, tc.url) + assert.NoError(t, err) + } +}