Skip to content

Commit

Permalink
Fix (#10)
Browse files Browse the repository at this point in the history
* bugfix前的修改

* bugfix

* change
  • Loading branch information
guonaihong authored May 18, 2020
1 parent deed7cf commit 8f45d3f
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 93 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.13', '1.12', '1.14']
go: [ '1.13', '1.14']
name: Go ${{ matrix.go }} sample

steps:
Expand Down
6 changes: 4 additions & 2 deletions getoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func Test_GetArgsTokenFail(t *testing.T) {
func Test_GetArgsToken(t *testing.T) {
type TestArgs struct {
in string
got []string
need []string
}

Expand All @@ -44,9 +43,12 @@ func Test_GetArgsToken(t *testing.T) {
},
TestArgs{
in: `'{"s":"{\"s\":\"S\"}"}'`,
got: []string{`{"s":"{\"s\":\"S\"}"}`},
need: []string{`{"s":"{\"s\":\"S\"}"}`},
},
TestArgs{
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'`,
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`},
},
} {

got, err := GetArgsToken(v.in)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/gin-gonic/gin v1.5.0
github.com/guonaihong/clop v0.0.3
github.com/guonaihong/gout v0.0.11
github.com/guonaihong/clop v0.0.7
github.com/guonaihong/gout v0.0.12
github.com/stretchr/testify v1.4.0
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/guonaihong/clop v0.0.3 h1:TbrXJ3tgYkuoCZXB45tjIhee6167pZJEaWwdibfJO5s=
github.com/guonaihong/clop v0.0.3/go.mod h1:6r46yf9HnBFtFgblQrzvbxzeAbFob3Qud3PnuhphwgI=
github.com/guonaihong/gout v0.0.10-0.20200319145506-f680e3c89b65 h1:gUPwwuesHdmpmT/648oEKVL1m5cmymi37pznF972dmo=
github.com/guonaihong/gout v0.0.10-0.20200319145506-f680e3c89b65/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
github.com/guonaihong/gout v0.0.11 h1:9SieeKKa83zhqvdYDTjJh9bcyW+Jc3CKmzBS/w0Z4i8=
github.com/guonaihong/gout v0.0.11/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
github.com/guonaihong/clop v0.0.7 h1:sFZfmlu48o1wq6KUYdig0AsScEc7KHrxiC0NYp73jbk=
github.com/guonaihong/clop v0.0.7/go.mod h1:6r46yf9HnBFtFgblQrzvbxzeAbFob3Qud3PnuhphwgI=
github.com/guonaihong/gout v0.0.12 h1:3wMBcVnOzAaoh1l4ddShjgZtm4GecZCsgK+e8zgvj5c=
github.com/guonaihong/gout v0.0.12/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo=
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
Expand Down
15 changes: 15 additions & 0 deletions issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pcurl

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_Issue9(t *testing.T) {

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'`)
assert.NoError(t, err)
assert.NotNil(t, req)
assert.NotEqual(t, len(req.Header), 0)
}
21 changes: 15 additions & 6 deletions pcurl.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,21 @@ func (c *Curl) createForm() ([]interface{}, error) {
return form, nil
}

func (c *Curl) getURL() string {
url := c.URL2
if c.p.GetIndex("url") > c.p.GetIndex("url2") {
url = c.URL
}
return url
}

func (c *Curl) Request() (*http.Request, error) {
if len(c.Method) == 0 && len(c.Data) > 0 {
c.Method = "POST"
if len(c.Method) == 0 {
if len(c.Data) > 0 {
c.Method = "POST"
} else {
c.Method = "GET"
}
}

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

url := c.URL2
if c.p.GetIndex("url") > c.p.GetIndex("url2") {
url = c.URL
}
url := c.getURL()

return g.SetURL(url). //设置url
SetBody(data). //设置http body
Expand Down
103 changes: 103 additions & 0 deletions pcurl_header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package pcurl

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"github.com/stretchr/testify/assert"
)

func createGeneralHeader(need H, t *testing.T) *httptest.Server {
router := func() *gin.Engine {
router := gin.New()

cb := func(c *gin.Context) {
gotHeader := make(H, 2)
//c.ShouldBindHeader(&gotHeader)

for k, v := range c.Request.Header {
if len(v) == 0 {
continue
}
switch k {
case "Accept-Encoding", "Content-Length", "User-Agent":
continue
}

gotHeader[k] = v[0]
}

//c.ShouldBindHeader(&gotHeader2)
if assert.Equal(t, need, gotHeader) {
c.JSON(200, gotHeader)
return
}

c.String(500, "")
}

router.POST("/", cb)
router.GET("/", cb)

return router
}()

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

func Test_Header(t *testing.T) {

type testHeader struct {
curlHeader []string
need H
}

for index, headerData := range []testHeader{
testHeader{
curlHeader: []string{"curl", "-X", "POST", "-H", "H1:v1", "-H", "H2:v2"},
need: H{
"H1": "v1",
"H2": "v2",
},
},
testHeader{
curlHeader: []string{"curl", "-X", "POST", "--header", "H1:v1", "--header", "H2:v2"},
need: H{
"H1": "v1",
"H2": "v2",
},
},
testHeader{
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`},
need: H{
"Connection": `keep-alive`,
"Cache-Control": `max-age=0`,
"Upgrade-Insecure-Requests": `1`,
"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`,
"Accept-Language": `en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7`,
"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`,
},
},
} {

code := 0
// 创建测试服务端
ts := createGeneralHeader(headerData.need, t)

// 解析curl表达式
req, err := ParseSlice(append(headerData.curlHeader, ts.URL)).Request()
assert.NoError(t, err, fmt.Sprintf("index = %d", index))

var getJSON H
//发送请求
err = gout.New().SetRequest(req).Debug(true).Code(&code).BindJSON(&getJSON).Do()

assert.NoError(t, err)
assert.Equal(t, headerData.need, getJSON, fmt.Sprintf("index = %d", index))
assert.Equal(t, code, 200, fmt.Sprintf("index = %d", index))
}
}
79 changes: 3 additions & 76 deletions pcurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ func Test_Curl(t *testing.T) {
url = url + d.path
}

// curlSlice追加url
curlSlice := append(d.curlSlice, url)

fmt.Printf("\nindex:%d#%s\n", index, curlSlice)

req, err := ParseSlice(curlSlice).Request()
assert.NoError(t, err, fmt.Sprintf("test index :%d", index))

Expand Down Expand Up @@ -146,82 +149,6 @@ func Test_Form(t *testing.T) {
}
}

func createGeneralHeader(need H, t *testing.T) *httptest.Server {
router := func() *gin.Engine {
router := gin.New()

router.POST("/", func(c *gin.Context) {
gotHeader := make(H, 2)
//c.ShouldBindHeader(&gotHeader)

for k, v := range c.Request.Header {
if len(v) == 0 {
continue
}
switch k {
case "Accept-Encoding", "Content-Length", "User-Agent":
continue
}

gotHeader[k] = v[0]
}

//c.ShouldBindHeader(&gotHeader2)
if assert.Equal(t, need, gotHeader) {
c.JSON(200, gotHeader)
return
}

c.String(500, "")
})

return router
}()

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

func Test_Header(t *testing.T) {

type testHeader struct {
curlHeader []string
need H
}

for _, headerData := range []testHeader{
testHeader{
curlHeader: []string{"curl", "-X", "POST", "-H", "H1:v1", "-H", "H2:v2"},
need: H{
"H1": "v1",
"H2": "v2",
},
},
testHeader{
curlHeader: []string{"curl", "-X", "POST", "--header", "H1:v1", "--header", "H2:v2"},
need: H{
"H1": "v1",
"H2": "v2",
},
},
} {

code := 0
// 创建测试服务端
ts := createGeneralHeader(headerData.need, t)

// 解析curl表达式
req, err := ParseSlice(append(headerData.curlHeader, ts.URL)).Request()
assert.NoError(t, err)

var getJSON H
//发送请求
err = gout.New().SetRequest(req).Debug(true).Code(&code).BindJSON(&getJSON).Do()
assert.NoError(t, err)
assert.Equal(t, code, 200)
assert.Equal(t, headerData.need, getJSON)
}
}

func createGeneralForm(need H, t *testing.T) *httptest.Server {
router := func() *gin.Engine {
router := gin.New()
Expand Down

0 comments on commit 8f45d3f

Please sign in to comment.