v0.0.2
基本能用的简单请求
This commit is contained in:
parent
79f1bf54df
commit
3c4a80a8b6
|
@ -1,5 +0,0 @@
|
||||||
package requests
|
|
||||||
|
|
||||||
func Get(url string) {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package requests
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGet(t *testing.T) {
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,7 +1,12 @@
|
||||||
package requests
|
package requests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Params 相关参数结构
|
// Params 相关参数结构
|
||||||
|
@ -12,10 +17,21 @@ type Params struct {
|
||||||
ContentType string
|
ContentType string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BasicAuth 帐号认真结构
|
||||||
|
type BasicAuth struct {
|
||||||
|
// User 帐号
|
||||||
|
User string
|
||||||
|
// Password 密码
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
// Session 的基本方法
|
// Session 的基本方法
|
||||||
type Session struct {
|
type Session struct {
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
transport *http.Transport
|
||||||
|
cookies http.CookieJar
|
||||||
params *Params
|
params *Params
|
||||||
|
auth *BasicAuth
|
||||||
}
|
}
|
||||||
|
|
||||||
// TypeContent post类型参数
|
// TypeContent post类型参数
|
||||||
|
@ -33,9 +49,97 @@ const (
|
||||||
TypeFormData = "multipart/form-data"
|
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
|
// NewSession 创建Session
|
||||||
func 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 请求
|
// Get 请求
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestURL(t *testing.T) {
|
func TestTest(t *testing.T) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user