Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 45df60e197 | |||
| ecd18128a4 | |||
| f16f5b0116 | |||
| 8291a17aca | |||
| 7ad16974b6 | |||
| 8918f7f891 | |||
| 29a23a75ba | |||
| d9c5d0857b | |||
| 852924e5df | |||
| 4c13582572 | |||
| d3a7dc916c |
@@ -1,5 +1,7 @@
|
||||
module github.com/alibabacloud-go/tea
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
|
||||
|
||||
+189
-29
File diff suppressed because it is too large
Load Diff
+150
-32
@@ -18,8 +18,8 @@ import (
|
||||
)
|
||||
|
||||
type test struct {
|
||||
Key string `json:"key"`
|
||||
Body []byte `json:"body"`
|
||||
Key string `json:"key,omitempty"`
|
||||
Body []byte `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
type PrettifyTest struct {
|
||||
@@ -46,17 +46,44 @@ var runtimeObj = map[string]interface{}{
|
||||
}
|
||||
|
||||
type validateTest struct {
|
||||
Num *int `json:"num" require:"true"`
|
||||
Name *string `json:"name" maxLength:"4"`
|
||||
Str *string `json:"str" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
Test *errLength `json:"test"`
|
||||
List []*string `json:"list" pattern:"^[a-d]*$" maxLength:"4"`
|
||||
Num1 *int `json:"num1,omitempty" require:"true" minimum:"2"`
|
||||
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"`
|
||||
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
|
||||
MinLength *errMinLength `json:"MinLength,omitempty"`
|
||||
Maximum *errMaximum `json:"Maximum,omitempty"`
|
||||
Minimum *errMinimum `json:"Minimum,omitempty"`
|
||||
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 errLength struct {
|
||||
type errMaxLength struct {
|
||||
Num *int `json:"num" maxLength:"a"`
|
||||
}
|
||||
|
||||
type errMinLength struct {
|
||||
Num *int `json:"num" minLength:"a"`
|
||||
}
|
||||
|
||||
type errMaximum struct {
|
||||
Num *int `json:"num" maximum:"a"`
|
||||
}
|
||||
|
||||
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 {
|
||||
}
|
||||
|
||||
@@ -141,6 +168,9 @@ func TestSDKError(t *testing.T) {
|
||||
})
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "SDKError:\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error())
|
||||
|
||||
err.SetErrMsg("test")
|
||||
utils.AssertEqual(t, "test", err.Error())
|
||||
}
|
||||
|
||||
func TestSDKErrorCode404(t *testing.T) {
|
||||
@@ -204,12 +234,12 @@ func TestMerge(t *testing.T) {
|
||||
}
|
||||
|
||||
type Test struct {
|
||||
Msg *string `json:"Msg"`
|
||||
Cast *CastError `json:"Cast"`
|
||||
ListPtr []*string `json:"ListPtr"`
|
||||
List []string `json:"List"`
|
||||
CastList []CastError `json:"CastList"`
|
||||
CastListPtr []*CastError `json:"CastListPtr"`
|
||||
Msg *string `json:"Msg,omitempty"`
|
||||
Cast *CastError `json:"Cast,omitempty"`
|
||||
ListPtr []*string `json:"ListPtr,omitempty"`
|
||||
List []string `json:"List,omitempty"`
|
||||
CastList []CastError `json:"CastList,omitempty"`
|
||||
CastListPtr []*CastError `json:"CastListPtr,omitempty"`
|
||||
}
|
||||
|
||||
func TestToMap(t *testing.T) {
|
||||
@@ -349,16 +379,18 @@ func Test_DoRequest(t *testing.T) {
|
||||
runtimeObj["httpsProxy"] = "# #%gfdf"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
|
||||
utils.AssertContains(t, err.Error(), `invalid URL escape "%gf"`)
|
||||
|
||||
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())
|
||||
@@ -366,7 +398,7 @@ func Test_DoRequest(t *testing.T) {
|
||||
runtimeObj["socks5Proxy"] = "# #%gfdf"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
|
||||
utils.AssertContains(t, err.Error(), ` invalid URL escape "%gf"`)
|
||||
|
||||
hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
|
||||
return func(req *http.Request) (*http.Response, error) {
|
||||
@@ -528,12 +560,33 @@ func Test_ToString(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_Validate(t *testing.T) {
|
||||
num := 1
|
||||
num := 3
|
||||
config := &validateTest{
|
||||
Num: &num,
|
||||
Num1: &num,
|
||||
}
|
||||
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) {
|
||||
err := Recover(nil)
|
||||
utils.AssertNil(t, err)
|
||||
defer func() {
|
||||
if r := Recover(recover()); r != nil {
|
||||
utils.AssertEqual(t, "test", r.Error())
|
||||
}
|
||||
}()
|
||||
panic("test")
|
||||
}
|
||||
|
||||
func Test_validate(t *testing.T) {
|
||||
@@ -541,12 +594,12 @@ func Test_validate(t *testing.T) {
|
||||
err := validate(reflect.ValueOf(test))
|
||||
utils.AssertNil(t, err)
|
||||
|
||||
num := 1
|
||||
num := 3
|
||||
str0, str1 := "abc", "abcddd"
|
||||
val := &validateTest{
|
||||
Num: &num,
|
||||
Num1: &num,
|
||||
Num2: &num,
|
||||
Str: &str0,
|
||||
List: []*string{&str0},
|
||||
}
|
||||
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -554,25 +607,47 @@ 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.Num = nil
|
||||
val.Num1 = nil
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "num should be setted", err.Error())
|
||||
utils.AssertEqual(t, "num1 should be setted", err.Error())
|
||||
|
||||
val.Name = String("最大长度")
|
||||
val.Name1 = String("最大长度")
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, "num should be setted", err.Error())
|
||||
utils.AssertEqual(t, "num1 should be setted", err.Error())
|
||||
|
||||
val.Num = &num
|
||||
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
|
||||
@@ -581,11 +656,54 @@ func Test_validate(t *testing.T) {
|
||||
|
||||
val.Str = &str0
|
||||
val.List = []*string{&str0}
|
||||
val.Test = &errLength{
|
||||
val.MaxLength = &errMaxLength{
|
||||
Num: &num,
|
||||
}
|
||||
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,
|
||||
}
|
||||
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,
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.ParseFloat: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.Maximum = nil
|
||||
val.Minimum = &errMinimum{
|
||||
Num: &num,
|
||||
}
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `strconv.ParseFloat: parsing "a": invalid syntax`, err.Error())
|
||||
|
||||
val.Minimum = nil
|
||||
val.Num2 = Int(10)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
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, `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, `The length of Name2 is 0 which is less than 2`, err.Error())
|
||||
|
||||
val.Name2 = String("tea")
|
||||
val.Num1 = Int(0)
|
||||
err = validate(reflect.ValueOf(val))
|
||||
utils.AssertEqual(t, `The size of Num1 is 0.000000 which is less than 2.000000`, err.Error())
|
||||
}
|
||||
|
||||
func Test_Prettify(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user