File tree Expand file tree Collapse file tree 2 files changed +46
-3
lines changed
Expand file tree Collapse file tree 2 files changed +46
-3
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313// Curl结构体
1414type Curl struct {
1515 Method string `clop:"-X; --request" usage:"Specify request command to use"`
16+ Get bool `clop:"-G; --get" usage:"Put the post data in the URL and use GET"`
1617 Header []string `clop:"-H; --header" usage:"Pass custom header(s) to server"`
1718 Data string `clop:"-d; --data" usage:"HTTP POST data"`
1819 DataRaw string `clop:"--data-raw" usage:"HTTP POST data, '@' allowed"`
@@ -107,14 +108,22 @@ func (c *Curl) getURL() string {
107108 return url
108109}
109110
110- func (c * Curl ) emptySetMethod () {
111+ func (c * Curl ) setMethod () {
112+ // 在curl里面-X的选项的优先级别比-G高,所以c.Method为空时才会看c.Get是否设置
113+ if len (c .Method ) == 0 && c .Get {
114+ c .Method = "GET"
115+ return
116+ }
117+
111118 if len (c .Method ) != 0 {
112119 return
113120 }
121+
114122 if len (c .Data ) > 0 {
115123 c .Method = "POST"
116124 return
117125 }
126+
118127 c .Method = "GET"
119128}
120129
@@ -130,7 +139,7 @@ func (c *Curl) Request() (req *http.Request, err error) {
130139 }
131140 }()
132141
133- c .emptySetMethod () //如果method为空,设置一个默认值
142+ c .setMethod ()
134143
135144 header := c .createHeader ()
136145
Original file line number Diff line number Diff line change @@ -209,8 +209,42 @@ func createGeneralForm(need H, t *testing.T) *httptest.Server {
209209 return httptest .NewServer (http .HandlerFunc (router .ServeHTTP ))
210210}
211211
212- //TODO
213212func Test_Method (t * testing.T ) {
213+ methodServer := func () * httptest.Server {
214+ router := func () * gin.Engine {
215+ router := gin .New ()
216+
217+ router .DELETE ("/" , func (c * gin.Context ) {
218+ c .String (200 , "DELETE" )
219+ })
220+
221+ router .GET ("/" , func (c * gin.Context ) {
222+ c .String (200 , "GET" )
223+ })
224+ return router
225+ }()
226+
227+ return httptest .NewServer (http .HandlerFunc (router .ServeHTTP ))
228+ }
229+
230+ need := []string {"DELETE" , "DELETE" , "GET" }
231+
232+ for index , curlStr := range []string {
233+ `curl -X DELETE -G ` ,
234+ `curl -G -XDELETE ` ,
235+ `curl -G ` ,
236+ } {
237+ ts := methodServer ()
238+ req , err := ParseAndRequest (curlStr + ts .URL )
239+
240+ assert .NoError (t , err )
241+
242+ got := ""
243+ err = gout .New ().SetRequest (req ).BindBody (& got ).Do ()
244+
245+ assert .Equal (t , got , need [index ])
246+ assert .NoError (t , err )
247+ }
214248}
215249
216250func Test_URL (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments