Skip to content

Commit

Permalink
Support g (#14)
Browse files Browse the repository at this point in the history
* 支持-G; --get

* 使用-G选项
  • Loading branch information
guonaihong authored Jun 2, 2020
1 parent ffe781b commit faf2950
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
13 changes: 11 additions & 2 deletions pcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
// Curl结构体
type Curl struct {
Method string `clop:"-X; --request" usage:"Specify request command to use"`
Get bool `clop:"-G; --get" usage:"Put the post data in the URL and use GET"`
Header []string `clop:"-H; --header" usage:"Pass custom header(s) to server"`
Data string `clop:"-d; --data" usage:"HTTP POST data"`
DataRaw string `clop:"--data-raw" usage:"HTTP POST data, '@' allowed"`
Expand Down Expand Up @@ -107,14 +108,22 @@ func (c *Curl) getURL() string {
return url
}

func (c *Curl) emptySetMethod() {
func (c *Curl) setMethod() {
// 在curl里面-X的选项的优先级别比-G高,所以c.Method为空时才会看c.Get是否设置
if len(c.Method) == 0 && c.Get {
c.Method = "GET"
return
}

if len(c.Method) != 0 {
return
}

if len(c.Data) > 0 {
c.Method = "POST"
return
}

c.Method = "GET"
}

Expand All @@ -130,7 +139,7 @@ func (c *Curl) Request() (req *http.Request, err error) {
}
}()

c.emptySetMethod() //如果method为空,设置一个默认值
c.setMethod()

header := c.createHeader()

Expand Down
36 changes: 35 additions & 1 deletion pcurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,42 @@ func createGeneralForm(need H, t *testing.T) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
}

//TODO
func Test_Method(t *testing.T) {
methodServer := func() *httptest.Server {
router := func() *gin.Engine {
router := gin.New()

router.DELETE("/", func(c *gin.Context) {
c.String(200, "DELETE")
})

router.GET("/", func(c *gin.Context) {
c.String(200, "GET")
})
return router
}()

return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
}

need := []string{"DELETE", "DELETE", "GET"}

for index, curlStr := range []string{
`curl -X DELETE -G `,
`curl -G -XDELETE `,
`curl -G `,
} {
ts := methodServer()
req, err := ParseAndRequest(curlStr + ts.URL)

assert.NoError(t, err)

got := ""
err = gout.New().SetRequest(req).BindBody(&got).Do()

assert.Equal(t, got, need[index])
assert.NoError(t, err)
}
}

func Test_URL(t *testing.T) {
Expand Down

0 comments on commit faf2950

Please sign in to comment.