Skip to content

Commit

Permalink
支持忽略ssl验证 (#286)
Browse files Browse the repository at this point in the history
* 支持忽略ssl验证, 还差测试代码

* 补全测试代码, 还差文档说明

* 新增文档
  • Loading branch information
guonaihong authored Jun 3, 2021
1 parent bb1cad0 commit 20fbe0a
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ gout 是go写的http 客户端,为提高工作效率而开发
- [Incoming custom * http.Client](#Incoming-custom-*http.Client)
- [Using chunked data format](#Using-chunked-data-format)
- [Global configuration](#Global-configuration)
- [Insecure skip verify](#insecure-skip-verify)
- [Null values are also serialized](#Null-values-are-also-serialized)
- [Global set timeout](#global-set-timeout)
- [Unique features](#Unique-features)
Expand Down Expand Up @@ -1614,6 +1615,25 @@ func main() {
```

# Global configuration
这里记录全局配置的方法, 后面所有的全局配置都推荐使用```gout.NewWithOpt```接口的实现
## insecure skip verify
忽略ssl验证, 使用```gout.WithInsecureSkipVerify()```接口配置该功能, 传入```gout.NewWithOpt```接口即可生效.
```go
import (
"github.com/guonaihong/gout"
)
func main() {
// globalWithOpt里面包含连接池, 这是一个全局可复用的对象
globalWithOpt := gout.NewWithOpt(gout.WithInsecureSkipVerify())
err := globalWithOpt.GET("url").Do()
if err != nil {
fmt.Printf("err = %v\n" ,err)
return
}
}
```

## Null values are also serialized
```go
query := gout.H{
Expand Down
58 changes: 58 additions & 0 deletions gout_newopt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package gout

import (
"net/http"

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

type Client struct {
options
}

func NewWithOpt(opts ...Option) *Client {
c := &Client{}
c.hc = &http.Client{}

for _, o := range opts {
o.apply(&c.options)
}

return c

}

// GET send HTTP GET method
func (c *Client) GET(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).GET(url)
}

// POST send HTTP POST method
func (c *Client) POST(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).POST(url)
}

// PUT send HTTP PUT method
func (c *Client) PUT(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).PUT(url)
}

// DELETE send HTTP DELETE method
func (c *Client) DELETE(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).DELETE(url)
}

// PATCH send HTTP PATCH method
func (c *Client) PATCH(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).PATCH(url)
}

// HEAD send HTTP HEAD method
func (c *Client) HEAD(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).HEAD(url)
}

// OPTIONS send HTTP OPTIONS method
func (c *Client) OPTIONS(url string) *dataflow.DataFlow {
return dataflow.New(c.hc).OPTIONS(url)
}
41 changes: 41 additions & 0 deletions gout_newopt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gout

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

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

func Test_NewWithOptTopMethod(t *testing.T) {
var total int32

router := setupMethod(&total)

ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP))
defer ts.Close()

err := NewWithOpt().GET(ts.URL + "/someGet").Do()
assert.NoError(t, err)

err = NewWithOpt().POST(ts.URL + "/somePost").Do()
assert.NoError(t, err)

err = NewWithOpt().PUT(ts.URL + "/somePut").Do()
assert.NoError(t, err)

err = NewWithOpt().DELETE(ts.URL + "/someDelete").Do()
assert.NoError(t, err)

err = NewWithOpt().PATCH(ts.URL + "/somePatch").Do()
assert.NoError(t, err)

err = NewWithOpt().HEAD(ts.URL + "/someHead").Do()
assert.NoError(t, err)

err = NewWithOpt().OPTIONS(ts.URL + "/someOptions").Do()
assert.NoError(t, err)

assert.Equal(t, int(total), 7)
}
51 changes: 51 additions & 0 deletions gout_newopt_with_skip_verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gout

import (
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

type chanWriter chan string

func (w chanWriter) Write(p []byte) (n int, err error) {
w <- string(p)
return len(p), nil
}

func Test_WithInsecureSkipVerify(t *testing.T) {

ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
}))

errc := make(chanWriter, 10) // but only expecting 1
ts.Config.ErrorLog = log.New(errc, "", 0)

defer ts.Close()

c := ts.Client()
for _, insecure := range []bool{true, false} {
var opts []Option
if insecure {
opts = []Option{WithClient(c), WithInsecureSkipVerify()}
}
client := NewWithOpt(opts...)
err := client.GET(ts.URL).Do()
if (err == nil) != insecure {
t.Errorf("#insecure=%v: got unexpected err=%v", insecure, err)
}
}

select {
case v := <-errc:
if !strings.Contains(v, "TLS handshake error") {
t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
}
case <-time.After(5 * time.Second):
t.Errorf("timeout waiting for logged error")
}
}
42 changes: 42 additions & 0 deletions gout_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gout

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

type options struct {
hc *http.Client
}

type Option interface {
apply(*options)
}

type insecureSkipVerifyOption bool

func (i insecureSkipVerifyOption) apply(opts *options) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}

opts.hc.Transport = tr
}

// 忽略ssl验证
func WithInsecureSkipVerify() Option {
b := true
return insecureSkipVerifyOption(b)
}

type client http.Client

func (c *client) apply(opts *options) {
opts.hc = (*http.Client)(c)
}

func WithClient(c *http.Client) Option {
return (*client)(c)
}

0 comments on commit 20fbe0a

Please sign in to comment.