Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b6aa048c4a | |||
| 5da4c33a57 | |||
| 2a316b984d | |||
| fa780870a7 | |||
| 9144ca7f27 | |||
| 174725cf27 | |||
| 45df60e197 | |||
| ecd18128a4 | |||
| f16f5b0116 | |||
| 8291a17aca | |||
| 7ad16974b6 | |||
| 8918f7f891 |
+131
-50
File diff suppressed because it is too large
Load Diff
+134
-16
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -50,12 +51,14 @@ type validateTest struct {
|
||||
Num2 *int `json:"num2,omitempty" maximum:"6"`
|
||||
Name1 *string `json:"name1,omitempty" maxLength:"4"`
|
||||
Name2 *string `json:"name2,omitempty" minLength:"2"`
|
||||
Str *string `json:"str,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
Str *string `json:"str,omitempty" pattern:"[a-d]*" maxLength:"4"`
|
||||
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
|
||||
MinLength *errMinLength `json:"MinLength,omitempty"`
|
||||
Maximum *errMaximum `json:"Maximum,omitempty"`
|
||||
Minimum *errMinimum `json:"Minimum,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
MaxItems *errMaxItems `json:"MaxItems,omitempty"`
|
||||
MinItems *errMinItems `json:"MinItems,omitempty"`
|
||||
List []*string `json:"list,omitempty" pattern:"[a-d]*" minItems:"2" maxItems:"3" maxLength:"4"`
|
||||
}
|
||||
|
||||
type errMaxLength struct {
|
||||
@@ -74,6 +77,14 @@ type errMinimum struct {
|
||||
Num *int `json:"num" minimum:"a"`
|
||||
}
|
||||
|
||||
type errMaxItems struct {
|
||||
NumMax []*int `json:"num" maxItems:"a"`
|
||||
}
|
||||
|
||||
type errMinItems struct {
|
||||
NumMin []*int `json:"num" minItems:"a"`
|
||||
}
|
||||
|
||||
type Progresstest struct {
|
||||
}
|
||||
|
||||
@@ -230,6 +241,8 @@ type Test struct {
|
||||
List []string `json:"List,omitempty"`
|
||||
CastList []CastError `json:"CastList,omitempty"`
|
||||
CastListPtr []*CastError `json:"CastListPtr,omitempty"`
|
||||
Reader io.Reader
|
||||
Inter interface{}
|
||||
}
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
@@ -263,9 +276,12 @@ func TestToMap(t *testing.T) {
|
||||
Message: String("CastList"),
|
||||
},
|
||||
},
|
||||
Reader: strings.NewReader(""),
|
||||
Inter: 10,
|
||||
}
|
||||
result = ToMap(valid)
|
||||
utils.AssertEqual(t, "tea", result["Msg"])
|
||||
utils.AssertNil(t, result["Reader"])
|
||||
utils.AssertEqual(t, map[string]interface{}{"Message": "message"}, result["Cast"])
|
||||
utils.AssertEqual(t, []interface{}{"test", ""}, result["ListPtr"])
|
||||
utils.AssertEqual(t, []interface{}{"list"}, result["List"])
|
||||
@@ -346,6 +362,40 @@ func Test_GetBackoffTime(t *testing.T) {
|
||||
utils.AssertEqual(t, true, IntValue(ms) <= 3)
|
||||
}
|
||||
|
||||
var key = `-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIBPAIBAAJBAN5I1VCLYr2IlTLrFpwUGcnwl8yi6V8Mdw+myxfusNgEWiH/FQ4T
|
||||
AZsIveiLOz9Gcc8m2mZSxst2qGII00scpiECAwEAAQJBAJZEhnA8yjN28eXKJy68
|
||||
J/LsQrKEL1+h/ZsHFqTHJ6XfiA0CXjbjPsa4jEbpyilMTSgUyoKdJ512ioeco2n6
|
||||
xUECIQD/JUHaKSuxz55t3efKdppqfopb92mJ2NuPJgrJI70OCwIhAN8HZ0bzr/4a
|
||||
DLvYCDUKvOj3GzsV1dtBwWuHBaZEafQDAiEAtTnrel//7z5/U55ow4BW0gmrkQM9
|
||||
bXIhEZ59zryZzl0CIQDFmBqRCu9eshecCP7kd3n88IjopSTOV4iUypBfyXcRnwIg
|
||||
eXNxUx+BCu2We36+c0deE2+vizL1s6f5XhE6l4bqtiM=
|
||||
-----END RSA PRIVATE KEY-----`
|
||||
var cert = `-----BEGIN CERTIFICATE-----
|
||||
MIIBvDCCAWYCCQDKjNYQxar0mjANBgkqhkiG9w0BAQsFADBlMQswCQYDVQQGEwJh
|
||||
czEMMAoGA1UECAwDYXNmMQwwCgYDVQQHDANzYWQxCzAJBgNVBAoMAnNkMQ0wCwYD
|
||||
VQQLDARxd2VyMQswCQYDVQQDDAJzZjERMA8GCSqGSIb3DQEJARYCd2UwHhcNMjAx
|
||||
MDE5MDI0MDMwWhcNMzAxMDE3MDI0MDMwWjBlMQswCQYDVQQGEwJhczEMMAoGA1UE
|
||||
CAwDYXNmMQwwCgYDVQQHDANzYWQxCzAJBgNVBAoMAnNkMQ0wCwYDVQQLDARxd2Vy
|
||||
MQswCQYDVQQDDAJzZjERMA8GCSqGSIb3DQEJARYCd2UwXDANBgkqhkiG9w0BAQEF
|
||||
AANLADBIAkEA3kjVUItivYiVMusWnBQZyfCXzKLpXwx3D6bLF+6w2ARaIf8VDhMB
|
||||
mwi96Is7P0ZxzybaZlLGy3aoYgjTSxymIQIDAQABMA0GCSqGSIb3DQEBCwUAA0EA
|
||||
ZjePopbFugNK0US1MM48V1S2petIsEcxbZBEk/wGqIzrY4RCFKMtbtPSgTDUl3D9
|
||||
XePemktG22a54ItVJ5FpcQ==
|
||||
-----END CERTIFICATE-----`
|
||||
var ca = `-----BEGIN CERTIFICATE-----
|
||||
MIIBuDCCAWICCQCLw4OWpjlJCDANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJm
|
||||
ZDEMMAoGA1UECAwDYXNkMQswCQYDVQQHDAJxcjEKMAgGA1UECgwBZjEMMAoGA1UE
|
||||
CwwDc2RhMQswCQYDVQQDDAJmZDESMBAGCSqGSIb3DQEJARYDYXNkMB4XDTIwMTAx
|
||||
OTAyNDQwNFoXDTIzMDgwOTAyNDQwNFowYzELMAkGA1UEBhMCZmQxDDAKBgNVBAgM
|
||||
A2FzZDELMAkGA1UEBwwCcXIxCjAIBgNVBAoMAWYxDDAKBgNVBAsMA3NkYTELMAkG
|
||||
A1UEAwwCZmQxEjAQBgkqhkiG9w0BCQEWA2FzZDBcMA0GCSqGSIb3DQEBAQUAA0sA
|
||||
MEgCQQCxXZTl5IO61Lqd0fBBOSy7ER1gsdA0LkvflP5HEaQygjecLGfrAtD/DWu0
|
||||
/sxCcBVnQRoP9Yp0ijHJwgXvBnrNAgMBAAEwDQYJKoZIhvcNAQELBQADQQBJF+/4
|
||||
DEMilhlFY+o9mqCygFVxuvHtQVhpPS938H2h7/P6pXN65jK2Y5hHefZEELq9ulQe
|
||||
91iBwaQ4e9racCgP
|
||||
-----END CERTIFICATE-----`
|
||||
|
||||
func Test_DoRequest(t *testing.T) {
|
||||
origTestHookDo := hookDo
|
||||
defer func() { hookDo = origTestHookDo }()
|
||||
@@ -355,7 +405,6 @@ func Test_DoRequest(t *testing.T) {
|
||||
}
|
||||
}
|
||||
request := NewRequest()
|
||||
request.Port = Int(80)
|
||||
request.Method = String("TEA TEST")
|
||||
resp, err := DoRequest(request, nil)
|
||||
utils.AssertNil(t, resp)
|
||||
@@ -373,12 +422,14 @@ func Test_DoRequest(t *testing.T) {
|
||||
|
||||
request.Pathname = String("?log")
|
||||
request.Headers["tea"] = String("")
|
||||
request.Headers["content-length"] = nil
|
||||
runtimeObj["httpsProxy"] = "http://someuser:somepassword@ecs.aliyun.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
|
||||
request.Headers["host"] = String("tea-cn-hangzhou.aliyuncs.com:80")
|
||||
request.Headers["user-agent"] = String("test")
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
@@ -398,6 +449,32 @@ func Test_DoRequest(t *testing.T) {
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
runtimeObj["key"] = "private rsa key"
|
||||
runtimeObj["cert"] = "private certification"
|
||||
runtimeObj["ignoreSSL"] = true
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertNil(t, resp)
|
||||
|
||||
runtimeObj["key"] = key
|
||||
runtimeObj["cert"] = cert
|
||||
runtimeObj["ca"] = "private ca"
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyun.com"
|
||||
_, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNotNil(t, err)
|
||||
|
||||
runtimeObj["ca"] = ca
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyuncs.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
request.Protocol = String("HTTP")
|
||||
runtimeObj["ignoreSSL"] = false
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
}
|
||||
|
||||
func Test_DoRequestWithConcurrent(t *testing.T) {
|
||||
@@ -486,7 +563,7 @@ func Test_getHttpProxy(t *testing.T) {
|
||||
|
||||
func Test_SetDialContext(t *testing.T) {
|
||||
runtime := &RuntimeObject{}
|
||||
dialcontext := setDialContext(runtime, 80)
|
||||
dialcontext := setDialContext(runtime)
|
||||
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
utils.AssertNotNil(t, cancelFunc)
|
||||
c, err := dialcontext(ctx, "127.0.0.1", "127.0.0.2")
|
||||
@@ -554,6 +631,16 @@ func Test_Validate(t *testing.T) {
|
||||
}
|
||||
err := Validate(config)
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
err = Validate(new(validateTest))
|
||||
utils.AssertEqual(t, err.Error(), "num1 should be setted")
|
||||
|
||||
var tmp *validateTest
|
||||
err = Validate(tmp)
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
err = Validate(nil)
|
||||
utils.AssertNil(t, err)
|
||||
}
|
||||
|
||||
func Test_Recover(t *testing.T) {
|
||||
@@ -578,7 +665,6 @@ func Test_validate(t *testing.T) {
|
||||
Num1: &num,
|
||||
Num2: &num,
|
||||
Str: &str0,
|
||||
List: []*string{&str0},
|
||||
}
|
||||
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -586,7 +672,7 @@ func Test_validate(t *testing.T) {
|
||||
|
||||
val.Str = &str1
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of Str is 6 which is more than 4", err.Error())
|
||||
|
||||
val.Num1 = nil
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -598,18 +684,40 @@ func Test_validate(t *testing.T) {
|
||||
|
||||
val.Num1 = &num
|
||||
val.Str = &str0
|
||||
val.List = []*string{&str1}
|
||||
val.List = []*string{&str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of List is 1 which is less than 2", err.Error())
|
||||
|
||||
val.Str = nil
|
||||
val.List = []*string{&str0, &str1}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "Length of abcddd is more than 4", err.Error())
|
||||
utils.AssertEqual(t, "The length of List is 6 which is more than 4", err.Error())
|
||||
|
||||
val.List = []*string{&str0, &str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
val.MaxItems = &errMaxItems{
|
||||
NumMax: []*int{&num},
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.MaxItems = nil
|
||||
val.MinItems = &errMinItems{
|
||||
NumMin: []*int{&num},
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.MinItems = nil
|
||||
val.List = []*string{&str0, &str0, &str0, &str0}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "The length of List is 4 which is more than 3", err.Error())
|
||||
|
||||
str2 := "test"
|
||||
val.Str = &str2
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "test is not matched ^[a-d]*$", err.Error())
|
||||
utils.AssertEqual(t, "test is not matched [a-d]*", err.Error())
|
||||
|
||||
val.Str = &str0
|
||||
val.List = []*string{&str0}
|
||||
@@ -619,6 +727,7 @@ func Test_validate(t *testing.T) {
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.List = nil
|
||||
val.MaxLength = nil
|
||||
val.MinLength = &errMinLength{
|
||||
Num: &num,
|
||||
@@ -626,6 +735,7 @@ func Test_validate(t *testing.T) {
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.Atoi: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.Name2 = String("tea")
|
||||
val.MinLength = nil
|
||||
val.Maximum = &errMaximum{
|
||||
Num: &num,
|
||||
@@ -643,22 +753,22 @@ func Test_validate(t *testing.T) {
|
||||
val.Minimum = nil
|
||||
val.Num2 = Int(10)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `10.000000 is greater than 6.000000`, err.Error())
|
||||
utils.AssertEqual(t, `The size of Num2 is 10.000000 which is greater than 6.000000`, err.Error())
|
||||
|
||||
val.Num2 = nil
|
||||
val.Name1 = String("maxLengthTouch")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `Length of maxLengthTouch is more than 4`, err.Error())
|
||||
utils.AssertEqual(t, `The length of Name1 is 14 which is more than 4`, err.Error())
|
||||
|
||||
val.Name1 = nil
|
||||
val.Name2 = String("")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `Length of is less than 2`, err.Error())
|
||||
utils.AssertEqual(t, `The length of Name2 is 0 which is less than 2`, err.Error())
|
||||
|
||||
val.Name2 = nil
|
||||
val.Name2 = String("tea")
|
||||
val.Num1 = Int(0)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `0.000000 is less than 2.000000`, err.Error())
|
||||
utils.AssertEqual(t, `The size of Num1 is 0.000000 which is less than 2.000000`, err.Error())
|
||||
}
|
||||
|
||||
func Test_Prettify(t *testing.T) {
|
||||
@@ -675,3 +785,11 @@ func Test_Prettify(t *testing.T) {
|
||||
str = Prettify(nil)
|
||||
utils.AssertEqual(t, str, "null")
|
||||
}
|
||||
|
||||
func Test_TransInt32AndInt(t *testing.T) {
|
||||
a := ToInt(Int32(10))
|
||||
utils.AssertEqual(t, IntValue(a), 10)
|
||||
|
||||
b := ToInt32(a)
|
||||
utils.AssertEqual(t, Int32Value(b), int32(10))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user