Skip to content

Commit 8f45d3f

Browse files
authored
Fix (#10)
* bugfix前的修改 * bugfix * change
1 parent deed7cf commit 8f45d3f

File tree

8 files changed

+147
-93
lines changed

8 files changed

+147
-93
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
go: [ '1.13', '1.12', '1.14']
13+
go: [ '1.13', '1.14']
1414
name: Go ${{ matrix.go }} sample
1515

1616
steps:

getoken_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func Test_GetArgsTokenFail(t *testing.T) {
2929
func Test_GetArgsToken(t *testing.T) {
3030
type TestArgs struct {
3131
in string
32-
got []string
3332
need []string
3433
}
3534

@@ -44,9 +43,12 @@ func Test_GetArgsToken(t *testing.T) {
4443
},
4544
TestArgs{
4645
in: `'{"s":"{\"s\":\"S\"}"}'`,
47-
got: []string{`{"s":"{\"s\":\"S\"}"}`},
4846
need: []string{`{"s":"{\"s\":\"S\"}"}`},
4947
},
48+
TestArgs{
49+
in: `curl 'http://xxx.cc/admin/index.php?act=admin' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/80.0.3987.87 Chrome/80.0.3987.87 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7' -H 'Cookie: username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13aeaa51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; timezone=8; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589004902,1589265192,1589341266,1589717172; Hm_lpvt_12d9f8f1740b76bb88c6691ea1672d8b=1589719525'`,
50+
need: []string{`curl`, `http://xxx.cc/admin/index.php?act=admin`, `-H`, `Connection: keep-alive`, `-H`, `Cache-Control: max-age=0`, `-H`, `Upgrade-Insecure-Requests: 1`, `-H`, `User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/80.0.3987.87 Chrome/80.0.3987.87 Safari/537.36`, `-H`, `Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9`, `-H`, `Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7`, `-H`, `Cookie: username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13aeaa51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; timezone=8; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589004902,1589265192,1589341266,1589717172; Hm_lpvt_12d9f8f1740b76bb88c6691ea1672d8b=1589719525`},
51+
},
5052
} {
5153

5254
got, err := GetArgsToken(v.in)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/gin-gonic/gin v1.5.0
7-
github.com/guonaihong/clop v0.0.3
8-
github.com/guonaihong/gout v0.0.11
7+
github.com/guonaihong/clop v0.0.7
8+
github.com/guonaihong/gout v0.0.12
99
github.com/stretchr/testify v1.4.0
1010
)

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
2020
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
2121
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
2222
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
23-
github.com/guonaihong/clop v0.0.3 h1:TbrXJ3tgYkuoCZXB45tjIhee6167pZJEaWwdibfJO5s=
24-
github.com/guonaihong/clop v0.0.3/go.mod h1:6r46yf9HnBFtFgblQrzvbxzeAbFob3Qud3PnuhphwgI=
25-
github.com/guonaihong/gout v0.0.10-0.20200319145506-f680e3c89b65 h1:gUPwwuesHdmpmT/648oEKVL1m5cmymi37pznF972dmo=
26-
github.com/guonaihong/gout v0.0.10-0.20200319145506-f680e3c89b65/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
27-
github.com/guonaihong/gout v0.0.11 h1:9SieeKKa83zhqvdYDTjJh9bcyW+Jc3CKmzBS/w0Z4i8=
28-
github.com/guonaihong/gout v0.0.11/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
23+
github.com/guonaihong/clop v0.0.7 h1:sFZfmlu48o1wq6KUYdig0AsScEc7KHrxiC0NYp73jbk=
24+
github.com/guonaihong/clop v0.0.7/go.mod h1:6r46yf9HnBFtFgblQrzvbxzeAbFob3Qud3PnuhphwgI=
25+
github.com/guonaihong/gout v0.0.12 h1:3wMBcVnOzAaoh1l4ddShjgZtm4GecZCsgK+e8zgvj5c=
26+
github.com/guonaihong/gout v0.0.12/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
2927
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
3028
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
3129
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=

issue_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pcurl
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_Issue9(t *testing.T) {
10+
11+
req, err := ParseAndRequest(`curl 'http://xxxx.cc/admin/index.php?act=admin' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/80.0.3987.87 Chrome/80.0.3987.87 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' -H 'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7' -H 'Cookie: username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13e51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; timezone=8; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589004902,1589265192,1589341266,1589717172; Hm_lpvt_12d9f8f1740b76bb88c6691ea1672d8b=1589719525'`)
12+
assert.NoError(t, err)
13+
assert.NotNil(t, req)
14+
assert.NotEqual(t, len(req.Header), 0)
15+
}

pcurl.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,21 @@ func (c *Curl) createForm() ([]interface{}, error) {
9494
return form, nil
9595
}
9696

97+
func (c *Curl) getURL() string {
98+
url := c.URL2
99+
if c.p.GetIndex("url") > c.p.GetIndex("url2") {
100+
url = c.URL
101+
}
102+
return url
103+
}
104+
97105
func (c *Curl) Request() (*http.Request, error) {
98-
if len(c.Method) == 0 && len(c.Data) > 0 {
99-
c.Method = "POST"
106+
if len(c.Method) == 0 {
107+
if len(c.Data) > 0 {
108+
c.Method = "POST"
109+
} else {
110+
c.Method = "GET"
111+
}
100112
}
101113

102114
var (
@@ -143,10 +155,7 @@ func (c *Curl) Request() (*http.Request, error) {
143155
g.SetForm(form) //设置formdata
144156
}
145157

146-
url := c.URL2
147-
if c.p.GetIndex("url") > c.p.GetIndex("url2") {
148-
url = c.URL
149-
}
158+
url := c.getURL()
150159

151160
return g.SetURL(url). //设置url
152161
SetBody(data). //设置http body

pcurl_header_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package pcurl
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/gin-gonic/gin"
10+
"github.com/guonaihong/gout"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func createGeneralHeader(need H, t *testing.T) *httptest.Server {
15+
router := func() *gin.Engine {
16+
router := gin.New()
17+
18+
cb := func(c *gin.Context) {
19+
gotHeader := make(H, 2)
20+
//c.ShouldBindHeader(&gotHeader)
21+
22+
for k, v := range c.Request.Header {
23+
if len(v) == 0 {
24+
continue
25+
}
26+
switch k {
27+
case "Accept-Encoding", "Content-Length", "User-Agent":
28+
continue
29+
}
30+
31+
gotHeader[k] = v[0]
32+
}
33+
34+
//c.ShouldBindHeader(&gotHeader2)
35+
if assert.Equal(t, need, gotHeader) {
36+
c.JSON(200, gotHeader)
37+
return
38+
}
39+
40+
c.String(500, "")
41+
}
42+
43+
router.POST("/", cb)
44+
router.GET("/", cb)
45+
46+
return router
47+
}()
48+
49+
return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
50+
}
51+
52+
func Test_Header(t *testing.T) {
53+
54+
type testHeader struct {
55+
curlHeader []string
56+
need H
57+
}
58+
59+
for index, headerData := range []testHeader{
60+
testHeader{
61+
curlHeader: []string{"curl", "-X", "POST", "-H", "H1:v1", "-H", "H2:v2"},
62+
need: H{
63+
"H1": "v1",
64+
"H2": "v2",
65+
},
66+
},
67+
testHeader{
68+
curlHeader: []string{"curl", "-X", "POST", "--header", "H1:v1", "--header", "H2:v2"},
69+
need: H{
70+
"H1": "v1",
71+
"H2": "v2",
72+
},
73+
},
74+
testHeader{
75+
curlHeader: []string{`curl`, `-H`, `Connection: keep-alive`, `-H`, `Cache-Control: max-age=0`, `-H`, `Upgrade-Insecure-Requests: 1`, `-H`, `Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9`, `-H`, `Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7`, `-H`, `Cookie: username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13aeaa51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; timezone=8; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589004902,1589265192,1589341266,1589717172; Hm_lpvt_12d9f8f1740b76bb88c6691ea1672d8b=1589719525`},
76+
need: H{
77+
"Connection": `keep-alive`,
78+
"Cache-Control": `max-age=0`,
79+
"Upgrade-Insecure-Requests": `1`,
80+
"Accept": `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8,application/signed-exchange;v=b3;q=0.9`,
81+
"Accept-Language": `en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7`,
82+
"Cookie": `username=admin; token=b7ea3ec643e4ea4871dfe515c559d28bc0d23b6d9d6b22daf206f1de9aff13aeaa51591323199; addinfo=%7B%22chkadmin%22%3A1%2C%22chkarticle%22%3A1%2C%22levelname%22%3A%22%5Cu7ba1%5Cu7406%5Cu5458%22%2C%22userid%22%3A%221%22%2C%22useralias%22%3A%22admin%22%7D; timezone=8; Hm_lvt_12d9f8f1740b76bb88c6691ea1672d8b=1589004902,1589265192,1589341266,1589717172; Hm_lpvt_12d9f8f1740b76bb88c6691ea1672d8b=1589719525`,
83+
},
84+
},
85+
} {
86+
87+
code := 0
88+
// 创建测试服务端
89+
ts := createGeneralHeader(headerData.need, t)
90+
91+
// 解析curl表达式
92+
req, err := ParseSlice(append(headerData.curlHeader, ts.URL)).Request()
93+
assert.NoError(t, err, fmt.Sprintf("index = %d", index))
94+
95+
var getJSON H
96+
//发送请求
97+
err = gout.New().SetRequest(req).Debug(true).Code(&code).BindJSON(&getJSON).Do()
98+
99+
assert.NoError(t, err)
100+
assert.Equal(t, headerData.need, getJSON, fmt.Sprintf("index = %d", index))
101+
assert.Equal(t, code, 200, fmt.Sprintf("index = %d", index))
102+
}
103+
}

pcurl_test.go

Lines changed: 3 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ func Test_Curl(t *testing.T) {
7070
url = url + d.path
7171
}
7272

73+
// curlSlice追加url
7374
curlSlice := append(d.curlSlice, url)
75+
7476
fmt.Printf("\nindex:%d#%s\n", index, curlSlice)
77+
7578
req, err := ParseSlice(curlSlice).Request()
7679
assert.NoError(t, err, fmt.Sprintf("test index :%d", index))
7780

@@ -146,82 +149,6 @@ func Test_Form(t *testing.T) {
146149
}
147150
}
148151

149-
func createGeneralHeader(need H, t *testing.T) *httptest.Server {
150-
router := func() *gin.Engine {
151-
router := gin.New()
152-
153-
router.POST("/", func(c *gin.Context) {
154-
gotHeader := make(H, 2)
155-
//c.ShouldBindHeader(&gotHeader)
156-
157-
for k, v := range c.Request.Header {
158-
if len(v) == 0 {
159-
continue
160-
}
161-
switch k {
162-
case "Accept-Encoding", "Content-Length", "User-Agent":
163-
continue
164-
}
165-
166-
gotHeader[k] = v[0]
167-
}
168-
169-
//c.ShouldBindHeader(&gotHeader2)
170-
if assert.Equal(t, need, gotHeader) {
171-
c.JSON(200, gotHeader)
172-
return
173-
}
174-
175-
c.String(500, "")
176-
})
177-
178-
return router
179-
}()
180-
181-
return httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
182-
}
183-
184-
func Test_Header(t *testing.T) {
185-
186-
type testHeader struct {
187-
curlHeader []string
188-
need H
189-
}
190-
191-
for _, headerData := range []testHeader{
192-
testHeader{
193-
curlHeader: []string{"curl", "-X", "POST", "-H", "H1:v1", "-H", "H2:v2"},
194-
need: H{
195-
"H1": "v1",
196-
"H2": "v2",
197-
},
198-
},
199-
testHeader{
200-
curlHeader: []string{"curl", "-X", "POST", "--header", "H1:v1", "--header", "H2:v2"},
201-
need: H{
202-
"H1": "v1",
203-
"H2": "v2",
204-
},
205-
},
206-
} {
207-
208-
code := 0
209-
// 创建测试服务端
210-
ts := createGeneralHeader(headerData.need, t)
211-
212-
// 解析curl表达式
213-
req, err := ParseSlice(append(headerData.curlHeader, ts.URL)).Request()
214-
assert.NoError(t, err)
215-
216-
var getJSON H
217-
//发送请求
218-
err = gout.New().SetRequest(req).Debug(true).Code(&code).BindJSON(&getJSON).Do()
219-
assert.NoError(t, err)
220-
assert.Equal(t, code, 200)
221-
assert.Equal(t, headerData.need, getJSON)
222-
}
223-
}
224-
225152
func createGeneralForm(need H, t *testing.T) *httptest.Server {
226153
router := func() *gin.Engine {
227154
router := gin.New()

0 commit comments

Comments
 (0)