Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa780870a7 | |||
| 9144ca7f27 | |||
| 174725cf27 | |||
| 45df60e197 | |||
| ecd18128a4 | |||
| f16f5b0116 |
+103
-25
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -35,11 +36,11 @@ var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *
|
||||
}
|
||||
|
||||
var basicTypes = []string{
|
||||
"int", "int64", "float32", "float64", "string", "bool", "uint64",
|
||||
"int", "int16", "int64", "int32", "float32", "float64", "string", "bool", "uint64", "uint32", "uint16",
|
||||
}
|
||||
|
||||
// Verify whether the parameters meet the requirements
|
||||
var validateParams = []string{"require", "pattern", "maxLength", "minLength", "maximum", "minimum"}
|
||||
var validateParams = []string{"require", "pattern", "maxLength", "minLength", "maximum", "minimum", "maxItems", "minItems"}
|
||||
|
||||
// CastError is used for cast type fails
|
||||
type CastError struct {
|
||||
@@ -85,6 +86,9 @@ type RuntimeObject struct {
|
||||
HttpsProxy *string `json:"httpsProxy" xml:"httpsProxy"`
|
||||
NoProxy *string `json:"noProxy" xml:"noProxy"`
|
||||
MaxIdleConns *int `json:"maxIdleConns" xml:"maxIdleConns"`
|
||||
Key *string `json:"key" xml:"key"`
|
||||
Cert *string `json:"cert" xml:"cert"`
|
||||
CA *string `json:"ca" xml:"ca"`
|
||||
Socks5Proxy *string `json:"socks5Proxy" xml:"socks5Proxy"`
|
||||
Socks5NetWork *string `json:"socks5NetWork" xml:"socks5NetWork"`
|
||||
Listener utils.ProgressListener `json:"listener" xml:"listener"`
|
||||
@@ -123,6 +127,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
|
||||
MaxIdleConns: TransInterfaceToInt(runtime["maxIdleConns"]),
|
||||
Socks5Proxy: TransInterfaceToString(runtime["socks5Proxy"]),
|
||||
Socks5NetWork: TransInterfaceToString(runtime["socks5NetWork"]),
|
||||
Key: TransInterfaceToString(runtime["key"]),
|
||||
Cert: TransInterfaceToString(runtime["cert"]),
|
||||
CA: TransInterfaceToString(runtime["ca"]),
|
||||
}
|
||||
if runtime["listener"] != nil {
|
||||
runtimeObject.Listener = runtime["listener"].(utils.ProgressListener)
|
||||
@@ -317,6 +324,10 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
continue
|
||||
} else if key == "host" {
|
||||
httpRequest.Header["Host"] = []string{*value}
|
||||
delete(httpRequest.Header, "host")
|
||||
} else if key == "user-agent" {
|
||||
httpRequest.Header["User-Agent"] = []string{*value}
|
||||
delete(httpRequest.Header, "user-agent")
|
||||
} else {
|
||||
httpRequest.Header[key] = []string{*value}
|
||||
}
|
||||
@@ -363,8 +374,29 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
|
||||
if strings.ToLower(*req.Protocol) == "https" &&
|
||||
runtime.Key != nil && runtime.Cert != nil {
|
||||
cert, err := tls.X509KeyPair([]byte(StringValue(runtime.Cert)), []byte(StringValue(runtime.Key)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
Certificates: []tls.Certificate{cert},
|
||||
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
|
||||
}
|
||||
if runtime.CA != nil {
|
||||
clientCertPool := x509.NewCertPool()
|
||||
ok := clientCertPool.AppendCertsFromPEM([]byte(StringValue(runtime.CA)))
|
||||
if !ok {
|
||||
return nil, errors.New("Failed to parse root certificate")
|
||||
}
|
||||
trans.TLSClientConfig.RootCAs = clientCertPool
|
||||
}
|
||||
} else {
|
||||
trans.TLSClientConfig = &tls.Config{
|
||||
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
|
||||
}
|
||||
}
|
||||
if httpProxy != nil {
|
||||
trans.Proxy = http.ProxyURL(httpProxy)
|
||||
@@ -820,12 +852,12 @@ func validateParam(field reflect.StructField, valueField reflect.Value, tagName
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(field.Type.String(), "[]") { // Verify the parameters of the array type
|
||||
err := validateSlice(valueField, containsTag, tag, tagName)
|
||||
err := validateSlice(field, valueField, containsTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if valueField.Kind() == reflect.Ptr { // Determines whether it is a pointer object
|
||||
err := validatePtr(valueField, containsTag, tag, tagName)
|
||||
err := validatePtr(field, valueField, containsTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -833,12 +865,28 @@ func validateParam(field reflect.StructField, valueField reflect.Value, tagName
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSlice(valueField reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
func validateSlice(field reflect.StructField, valueField reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
if valueField.IsValid() && !valueField.IsNil() { // Determines whether the parameter has a value
|
||||
if containsregexpTag {
|
||||
if tagName == "maxItems" {
|
||||
err := checkMaxItems(field, valueField, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minItems" {
|
||||
err := checkMinItems(field, valueField, tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for m := 0; m < valueField.Len(); m++ {
|
||||
elementValue := valueField.Index(m)
|
||||
if elementValue.Type().Kind() == reflect.Ptr { // Determines whether the child elements of an array are of a basic type
|
||||
err := validatePtr(elementValue, containsregexpTag, tag, tagName)
|
||||
err := validatePtr(field, elementValue, containsregexpTag, tag, tagName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -848,43 +896,42 @@ func validateSlice(valueField reflect.Value, containsregexpTag bool, tag, tagNam
|
||||
return nil
|
||||
}
|
||||
|
||||
func validatePtr(elementValue reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
func validatePtr(field reflect.StructField, elementValue reflect.Value, containsregexpTag bool, tag, tagName string) error {
|
||||
if elementValue.IsNil() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if isFilterType(elementValue.Elem().Type().String(), basicTypes) {
|
||||
if containsregexpTag {
|
||||
if tagName == "pattern" {
|
||||
err := checkPattern(elementValue.Elem(), tag)
|
||||
err := checkPattern(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "maxLength" {
|
||||
err := checkMaxLength(elementValue.Elem(), tag)
|
||||
err := checkMaxLength(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minLength" {
|
||||
err := checkMinLength(elementValue.Elem(), tag)
|
||||
err := checkMinLength(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "maximum" {
|
||||
err := checkMaximum(elementValue.Elem(), tag)
|
||||
err := checkMaximum(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if tagName == "minimum" {
|
||||
err := checkMinimum(elementValue.Elem(), tag)
|
||||
err := checkMinimum(field, elementValue.Elem(), tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -909,17 +956,48 @@ func checkRequire(field reflect.StructField, valueField reflect.Value) error {
|
||||
return errors.New(name + " should be setted")
|
||||
}
|
||||
|
||||
func checkPattern(valueField reflect.Value, tag string) error {
|
||||
func checkPattern(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
value := valueField.String()
|
||||
if match, _ := regexp.MatchString(tag, value); !match { // Determines whether the parameter value satisfies the regular expression or not, and throws an error
|
||||
r, _ := regexp.Compile("^" + tag + "$")
|
||||
if match := r.MatchString(value); !match { // Determines whether the parameter value satisfies the regular expression or not, and throws an error
|
||||
return errors.New(value + " is not matched " + tag)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaxLength(valueField reflect.Value, tag string) error {
|
||||
func checkMaxItems(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maxItems, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := valueField.Len()
|
||||
if maxItems < length {
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is more than %d", field.Name, length, maxItems)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinItems(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() {
|
||||
minItems, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
length := valueField.Len()
|
||||
if minItems > length {
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is less than %d", field.Name, length, minItems)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaxLength(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maxLength, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
@@ -930,14 +1008,14 @@ func checkMaxLength(valueField reflect.Value, tag string) error {
|
||||
length = strings.Count(valueField.String(), "") - 1
|
||||
}
|
||||
if maxLength < length {
|
||||
errMsg := fmt.Sprintf("Length of %s is more than %d", valueField.String(), maxLength)
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is more than %d", field.Name, length, maxLength)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinLength(valueField reflect.Value, tag string) error {
|
||||
func checkMinLength(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() {
|
||||
minLength, err := strconv.Atoi(tag)
|
||||
if err != nil {
|
||||
@@ -948,14 +1026,14 @@ func checkMinLength(valueField reflect.Value, tag string) error {
|
||||
length = strings.Count(valueField.String(), "") - 1
|
||||
}
|
||||
if minLength > length {
|
||||
errMsg := fmt.Sprintf("Length of %s is less than %d", valueField.String(), minLength)
|
||||
errMsg := fmt.Sprintf("The length of %s is %d which is less than %d", field.Name, length, minLength)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMaximum(valueField reflect.Value, tag string) error {
|
||||
func checkMaximum(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
maximum, err := strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
@@ -967,14 +1045,14 @@ func checkMaximum(valueField reflect.Value, tag string) error {
|
||||
return err
|
||||
}
|
||||
if maximum < num {
|
||||
errMsg := fmt.Sprintf("%f is greater than %f", num, maximum)
|
||||
errMsg := fmt.Sprintf("The size of %s is %f which is greater than %f", field.Name, num, maximum)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMinimum(valueField reflect.Value, tag string) error {
|
||||
func checkMinimum(field reflect.StructField, valueField reflect.Value, tag string) error {
|
||||
if valueField.IsValid() && valueField.String() != "" {
|
||||
minimum, err := strconv.ParseFloat(tag, 64)
|
||||
if err != nil {
|
||||
@@ -987,7 +1065,7 @@ func checkMinimum(valueField reflect.Value, tag string) error {
|
||||
return err
|
||||
}
|
||||
if minimum > num {
|
||||
errMsg := fmt.Sprintf("%f is less than %f", num, minimum)
|
||||
errMsg := fmt.Sprintf("The size of %s is %f which is less than %f", field.Name, num, minimum)
|
||||
return errors.New(errMsg)
|
||||
}
|
||||
}
|
||||
|
||||
+113
-14
@@ -50,12 +50,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 +76,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 {
|
||||
}
|
||||
|
||||
@@ -346,6 +356,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 }()
|
||||
@@ -373,12 +417,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 +444,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"
|
||||
resp, 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) {
|
||||
@@ -558,6 +630,10 @@ func Test_Validate(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
@@ -584,7 +660,6 @@ func Test_validate(t *testing.T) {
|
||||
Num1: &num,
|
||||
Num2: &num,
|
||||
Str: &str0,
|
||||
List: []*string{&str0},
|
||||
}
|
||||
|
||||
err = validate(reflect.ValueOf(val))
|
||||
@@ -592,7 +667,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))
|
||||
@@ -604,18 +679,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}
|
||||
@@ -625,6 +722,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,
|
||||
@@ -632,6 +730,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,
|
||||
@@ -649,22 +748,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) {
|
||||
|
||||
Reference in New Issue
Block a user