Skip to content

Commit

Permalink
Add include opt (#20)
Browse files Browse the repository at this point in the history
* 新增-i --include选项支持

* fix

* fix
  • Loading branch information
guonaihong authored Mar 13, 2023
1 parent 4932e4e commit c664cb4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pcurl是解析curl表达式的库
* 支持--compressed选项
* 支持-k, --insecure选项
* 支持-G, --get选项
* 支持-i, --include选项
* 支持--data-urlencode选项
* 支持内嵌到你的结构体里面,让你的cmd秒变curl

Expand Down
19 changes: 15 additions & 4 deletions pcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -99,15 +102,23 @@ func (c *Curl) getBodyEncodeAndObj() (string, any, error) {
return body.Unmarshal([]byte(c.Data))
}

if name == bodyEmpby {
return "", nil, nil
}

return "", nil, ErrUnknownEncode
}

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
}
Expand Down
31 changes: 31 additions & 0 deletions pcurl_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <YourAppToken>' -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 <YourAppToken> ' 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)
}
}

0 comments on commit c664cb4

Please sign in to comment.