-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 支持忽略ssl验证, 还差测试代码 * 补全测试代码, 还差文档说明 * 新增文档
- Loading branch information
1 parent
bb1cad0
commit 20fbe0a
Showing
5 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |