Skip to content

Commit

Permalink
see #11 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong authored May 19, 2020
1 parent da94faf commit ffe781b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pcurl是解析curl表达式的库
* 支持--data-raw选项,curl用于设置http body
* 支持-F --form选项,用作设置formdata
* 支持--url选项,curl中设置url,一般不会设置这个选项
* 支持--compressed
* 支持--compressed选项
* 支持-k, --insecure选项

# 内容
- [json](#json)
Expand Down
12 changes: 12 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package pcurl

import (
"crypto/tls"
"net/http"
)

var defaultInsecureSkipVerify = http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
27 changes: 21 additions & 6 deletions pcurl.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package pcurl

import (
"github.com/guonaihong/clop"
"github.com/guonaihong/gout"
"net/http"
"os"
"strings"

"github.com/guonaihong/clop"
"github.com/guonaihong/gout"
"github.com/guonaihong/gout/dataflow"
)

// Curl结构体
Expand All @@ -20,6 +22,7 @@ type Curl struct {
Location bool `clop:"-L; --location" usage:"Follow redirects"` //TODO

Compressed bool `clop:"--compressed" usage:"Request compressed response"`
Insecure bool `clop:"-k; --insecure" "Allow insecure server connections when using SSL"`
Err error
p *clop.Clop
}
Expand Down Expand Up @@ -115,12 +118,18 @@ func (c *Curl) emptySetMethod() {
c.Method = "GET"
}

func (c *Curl) Request() (*http.Request, error) {
func (c *Curl) Request() (req *http.Request, err error) {

var (
data interface{}
)

defer func() {
if c.Err != nil {
err = c.Err
}
}()

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

header := c.createHeader()
Expand Down Expand Up @@ -151,9 +160,15 @@ func (c *Curl) Request() (*http.Request, error) {
data = fd
}

g := gout.New().
SetMethod(c.Method). //设置method POST or GET or DELETE
Debug(true) //打开debug模式
var g *dataflow.Gout
if c.Insecure {
g = gout.New(&defaultInsecureSkipVerify)
} else {
g = gout.New()
}

g.SetMethod(c.Method). //设置method POST or GET or DELETE
Debug(true) //打开debug模式

if c.Compressed {
header = append(header, "Accept-Encoding", "deflate, gzip")
Expand Down
12 changes: 12 additions & 0 deletions pcurl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func Test_Curl(t *testing.T) {
curlString: `curl -X POST -d '{"key":"val"}'`,
path: "/",
},
{ //测试-k选项
need: `{"key":"val"}`,
curlSlice: []string{"curl", "-k", "-X", "POST", "-d", `{"key":"val"}`},
curlString: `curl -k -X POST -d '{"key":"val"}'`,
path: "/",
},
{ //测试--insecure选项
need: `{"key":"val"}`,
curlSlice: []string{"curl", "--insecure", "-X", "POST", "-d", `{"key":"val"}`},
curlString: `curl --insecure -X POST -d '{"key":"val"}'`,
path: "/",
},
{
need: `{"type":"region","region":"bj","business":"asr","protocol":"private","connect":416"}`,
curlSlice: []string{"curl", "--location", "--request", "DELETE", "--header", "Content-Type: text/plain", "--data-raw", `{"type":"region","region":"bj","business":"asr","protocol":"private","connect":416"}`},
Expand Down
11 changes: 11 additions & 0 deletions pcurl_unsupported_option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pcurl

import (
"github.com/stretchr/testify/assert"
"testing"
)

func Test_UnsupportedOption(t *testing.T) {
_, err := ParseAndRequest(`curl --hahaha`)
assert.Error(t, err)
}

0 comments on commit ffe781b

Please sign in to comment.