Simplify Config Enum Label Too Long

This commit is contained in:
huangsimin 2018-11-08 14:26:51 +08:00
parent 4f88f69899
commit f490fa9c04
2 changed files with 25 additions and 25 deletions

View File

@ -67,26 +67,26 @@ type TypeConfig int
const ( const (
_ TypeConfig = iota _ TypeConfig = iota
// ConfigRequestTimeout request 包括 dial request redirect 总时间超时 // CRequestTimeout request 包括 dial request redirect 总时间超时
ConfigRequestTimeout // 支持time.Duration 和 int(秒为单位) CRequestTimeout // 支持time.Duration 和 int(秒为单位)
// ConfigDialTimeout 一个Connect过程的Timeout // CDialTimeout 一个Connect过程的Timeout
ConfigDialTimeout // 支持time.Duration 和 int(秒为单位) CDialTimeout // 支持time.Duration 和 int(秒为单位)
// ConfigProxy 代理链接 // CProxy 代理链接
ConfigProxy // http, https, socks5 CProxy // http, https, socks5
// ConfigInsecure InsecureSkipVerify // CInsecure InsecureSkipVerify
ConfigInsecure // true, false CInsecure // true, false
// ConfigBasicAuth 帐号认证 // CBasicAuth 帐号认证
ConfigBasicAuth // user pwd CBasicAuth // user pwd
// ConfigTLS 帐号认证 // CTLS 帐号认证
ConfigTLS // user pwd CTLS // user pwd
// ConfigCookiejar 持久化 CookieJar // CCookiejar 持久化 CookieJar
ConfigCookiejar // true or false ; default = true CCookiejar // true or false ; default = true
) )
// NewSession 创建Session // NewSession 创建Session
@ -108,7 +108,7 @@ func NewSession() *Session {
func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) { func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
switch typeConfig { switch typeConfig {
case ConfigRequestTimeout: case CRequestTimeout:
switch v := values.(type) { switch v := values.(type) {
case time.Duration: case time.Duration:
ses.client.Timeout = v ses.client.Timeout = v
@ -123,9 +123,9 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
default: default:
panic(errors.New("error type " + reflect.TypeOf(v).String())) panic(errors.New("error type " + reflect.TypeOf(v).String()))
} }
case ConfigDialTimeout: case CDialTimeout:
// 没时间实现这些小细节 // 没时间实现这些小细节
case ConfigCookiejar: case CCookiejar:
v := values.(bool) v := values.(bool)
if v { if v {
if ses.client.Jar == nil { if ses.client.Jar == nil {
@ -134,7 +134,7 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
} else { } else {
ses.client.Jar = nil ses.client.Jar = nil
} }
case ConfigProxy: case CProxy:
switch v := values.(type) { switch v := values.(type) {
case string: case string:
purl, err := (url.Parse(v)) purl, err := (url.Parse(v))
@ -147,11 +147,11 @@ func (ses *Session) SetConfig(typeConfig TypeConfig, values interface{}) {
case nil: case nil:
ses.transport.Proxy = nil ses.transport.Proxy = nil
} }
case ConfigInsecure: case CInsecure:
ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)} ses.transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: !values.(bool)}
case ConfigTLS: case CTLS:
ses.transport.TLSClientConfig = values.(*tls.Config) ses.transport.TLSClientConfig = values.(*tls.Config)
case ConfigBasicAuth: case CBasicAuth:
if ses.auth == nil { if ses.auth == nil {
ses.auth = &BasicAuth{} ses.auth = &BasicAuth{}
} }

View File

@ -285,19 +285,19 @@ func TestSession_SetConfig(t *testing.T) {
}{ }{
{ {
name: "test timeout", name: "test timeout",
args: args{typeConfig: ConfigRequestTimeout, values: 0.01}, args: args{typeConfig: CRequestTimeout, values: 0.01},
wantErr: true, wantErr: true,
}, },
{ {
name: "test not timeout", name: "test not timeout",
args: args{typeConfig: ConfigRequestTimeout, values: 5}, args: args{typeConfig: CRequestTimeout, values: 5},
wantErr: false, wantErr: false,
}, },
{ {
name: "test proxy", name: "test proxy",
args: args{typeConfig: ConfigProxy, values: "http://474420502.top:7070"}, args: args{typeConfig: CProxy, values: "http://474420502.top:7070"},
wantErr: false, wantErr: false,
}, },
} }
@ -319,7 +319,7 @@ func TestSession_SetConfig(t *testing.T) {
func TestSession_SetConfigInsecure(t *testing.T) { func TestSession_SetConfigInsecure(t *testing.T) {
ses := NewSession() ses := NewSession()
ses.SetConfig(ConfigInsecure, false) ses.SetConfig(CInsecure, false)
for _, badSSL := range []string{ for _, badSSL := range []string{
"https://self-signed.badssl.com/", "https://self-signed.badssl.com/",