Compare commits

...

3 Commits

Author SHA1 Message Date
wenzuochao 2a388c0108 update checkMaxLength 2020-03-20 15:21:05 +08:00
wenzuochao 268f040e48 modify checkMaxLength 2020-03-20 13:05:33 +08:00
wenzuochao b56effc83d modify host 2020-03-12 11:17:26 +08:00
2 changed files with 15 additions and 3 deletions
+9 -2
View File
@@ -291,8 +291,11 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
for key, value := range request.Headers {
if value == "" || key == "content-length" {
continue
} else if key == "host" {
httpRequest.Header["Host"] = []string{value}
} else {
httpRequest.Header[key] = []string{value}
}
httpRequest.Header[key] = []string{value}
debugLog("> %s: %s", key, value)
}
contentlength, _ := strconv.Atoi(request.Headers["content-length"])
@@ -845,7 +848,11 @@ func checkMaxLength(valueField reflect.Value, tag string) error {
if err != nil {
return err
}
if maxLength < valueField.Len() {
length := valueField.Len()
if valueField.Kind().String() == "string" {
length = strings.Count(valueField.String(), "") - 1
}
if maxLength < length {
errMsg := fmt.Sprintf("Length of %s is more than %d", valueField.String(), maxLength)
return errors.New(errMsg)
}
+6 -1
View File
@@ -47,6 +47,7 @@ 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"`
@@ -541,7 +542,7 @@ func Test_validate(t *testing.T) {
utils.AssertNil(t, err)
num := 1
str0, str1 := "acc", "abcddd"
str0, str1 := "abc", "abcddd"
val := &validateTest{
Num: &num,
Str: &str0,
@@ -559,6 +560,10 @@ func Test_validate(t *testing.T) {
err = validate(reflect.ValueOf(val))
utils.AssertEqual(t, "num should be setted", err.Error())
val.Name = String("最大长度")
err = validate(reflect.ValueOf(val))
utils.AssertEqual(t, "num should be setted", err.Error())
val.Num = &num
val.Str = &str0
val.List = []*string{&str1}