From 3c4a80a8b6f491a8c4a739e107b608da11d276a7 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Fri, 19 Oct 2018 16:26:50 +0800 Subject: [PATCH] =?UTF-8?q?v0.0.2=20=E5=9F=BA=E6=9C=AC=E8=83=BD=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E7=AE=80=E5=8D=95=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- httpclient_method.go | 5 -- httpclient_method_test.go | 9 ---- requests_method.go | 110 ++++++++++++++++++++++++++++++++++++-- requests_method_test.go | 66 +++++++++++++++++++++++ response_test.go | 2 +- 5 files changed, 174 insertions(+), 18 deletions(-) delete mode 100644 httpclient_method.go delete mode 100644 httpclient_method_test.go diff --git a/httpclient_method.go b/httpclient_method.go deleted file mode 100644 index 1ffddfd..0000000 --- a/httpclient_method.go +++ /dev/null @@ -1,5 +0,0 @@ -package requests - -func Get(url string) { - -} diff --git a/httpclient_method_test.go b/httpclient_method_test.go deleted file mode 100644 index 806e8f5..0000000 --- a/httpclient_method_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package requests - -import ( - "testing" -) - -func TestGet(t *testing.T) { - -} diff --git a/requests_method.go b/requests_method.go index 032380e..40c02e8 100644 --- a/requests_method.go +++ b/requests_method.go @@ -1,7 +1,12 @@ package requests import ( + "crypto/tls" + "errors" "net/http" + "net/url" + "reflect" + "time" ) // Params 相关参数结构 @@ -12,10 +17,21 @@ type Params struct { ContentType string } +// BasicAuth 帐号认真结构 +type BasicAuth struct { + // User 帐号 + User string + // Password 密码 + Password string +} + // Session 的基本方法 type Session struct { - client *http.Client - params *Params + client *http.Client + transport *http.Transport + cookies http.CookieJar + params *Params + auth *BasicAuth } // TypeContent post类型参数 @@ -33,9 +49,97 @@ const ( TypeFormData = "multipart/form-data" ) +// TypeConfig 配置类型 +type TypeConfig int + +const ( + _ TypeConfig = iota + // ConfigRequestTimeout request 包括 dial request redirect 总时间超时 + ConfigRequestTimeout // 支持time.Duration 和 int(秒为单位) + + // ConfigDialTimeout 一个Connect过程的Timeout + ConfigDialTimeout // 支持time.Duration 和 int(秒为单位) + + // ConfigProxy 代理链接 + ConfigProxy // http, https, socks5 + + // ConfigInsecure InsecureSkipVerify + ConfigInsecure // true, false + + // ConfigBasicAuth 帐号认证 + ConfigBasicAuth // user pwd + + // ConfigTLS 帐号认证 + ConfigTLS // user pwd +) + // NewSession 创建Session func NewSession() *Session { - return &Session{client: &http.Client{}, params: &Params{}} + client := &http.Client{} + transport := &http.Transport{} + client.Transport = transport + + return &Session{client: client, params: &Params{}, transport: transport, auth: nil} +} + +// SetConfig 设置配置 +func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { + + switch typeConfig { + case ConfigRequestTimeout: + switch v := values.(type) { + case time.Duration: + ses.client.Timeout = v + case int: + ses.client.Timeout = time.Duration(v * int(time.Second)) + case int64: + ses.client.Timeout = time.Duration(v * int64(time.Second)) + case float32: + ses.client.Timeout = time.Duration(v * float32(time.Second)) + case float64: + ses.client.Timeout = time.Duration(v * float64(time.Second)) + default: + panic(errors.New("error type " + reflect.TypeOf(v).String())) + } + case ConfigDialTimeout: + + case ConfigProxy: + switch v := values.(type) { + case string: + purl, err := (url.Parse(v)) + if err != nil { + panic(err) + } + ses.transport.Proxy = http.ProxyURL(purl) + case *url.URL: + ses.transport.Proxy = http.ProxyURL(v) + } + case ConfigInsecure: + ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)} + case ConfigTLS: + ses.transport.TLSClientConfig = values.(*tls.Config) + case ConfigBasicAuth: + if ses.auth == nil { + ses.auth = &BasicAuth{} + } + + switch v := values.(type) { + case *BasicAuth: + ses.auth.User = v.User + ses.auth.User = v.Password + case BasicAuth: + ses.auth.User = v.User + ses.auth.User = v.Password + case []string: + ses.auth.User = v[0] + ses.auth.User = v[1] + case nil: + ses.auth = nil + } + default: + panic(errors.New("unknown typeConfig " + reflect.TypeOf(typeConfig).String())) + } + return } // Get 请求 diff --git a/requests_method_test.go b/requests_method_test.go index 99af40a..92918f0 100644 --- a/requests_method_test.go +++ b/requests_method_test.go @@ -273,3 +273,69 @@ func TestSession_Patch(t *testing.T) { }) } } + +func TestSession_SetConfig(t *testing.T) { + + type args struct { + typeConfig TypeConfig + values interface{} + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + { + name: "test timeout", + args: args{typeConfig: ConfigRequestTimeout, values: 0.01}, + wantErr: true, + }, + + { + name: "test not timeout", + args: args{typeConfig: ConfigRequestTimeout, values: 5}, + wantErr: false, + }, + + { + name: "test proxy", + args: args{typeConfig: ConfigProxy, values: "http://474420502.top:7070"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ses := NewSession() + ses.SetConfig(tt.args.typeConfig, tt.args.values) + _, err := ses.Get("https://httpbin.org/get").Execute() + + if (err != nil) != tt.wantErr { + t.Errorf("Metchod error = %v", err) + return + } + + }) + } +} + +func TestSession_SetConfigInsecure(t *testing.T) { + + ses := NewSession() + ses.SetConfig(ConfigInsecure, false) + + for _, badSSL := range []string{ + "https://self-signed.badssl.com/", + "https://expired.badssl.com/", + "https://wrong.host.badssl.com/", + } { + resp, err := ses.Get(badSSL).Execute() + if err != nil { + t.Error("Unable to make request", err) + } + if resp.GResponse.StatusCode != 200 { + t.Error("Request did not return OK, is ", resp.GResponse.StatusCode) + } + } + +} diff --git a/response_test.go b/response_test.go index 871910a..db31d76 100644 --- a/response_test.go +++ b/response_test.go @@ -4,6 +4,6 @@ import ( "testing" ) -func TestURL(t *testing.T) { +func TestTest(t *testing.T) { }