Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a388c0108 | |||
| 268f040e48 | |||
| b56effc83d | |||
| f4dd763f32 |
+16
-3
@@ -246,8 +246,14 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
request.Port = 443
|
||||
}
|
||||
|
||||
requestURL := ""
|
||||
request.Domain = request.Headers["host"]
|
||||
requestURL := fmt.Sprintf("%s://%s:%d%s", request.Protocol, request.Domain, request.Port, request.Pathname)
|
||||
matched, _ := regexp.MatchString(":", request.Domain)
|
||||
if matched {
|
||||
requestURL = fmt.Sprintf("%s://%s%s", request.Protocol, request.Domain, request.Pathname)
|
||||
} else {
|
||||
requestURL = fmt.Sprintf("%s://%s:%d%s", request.Protocol, request.Domain, request.Port, request.Pathname)
|
||||
}
|
||||
queryParams := request.Query
|
||||
// sort QueryParams by key
|
||||
q := url.Values{}
|
||||
@@ -285,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"])
|
||||
@@ -839,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)
|
||||
}
|
||||
|
||||
+16
-3
@@ -18,7 +18,8 @@ import (
|
||||
)
|
||||
|
||||
type test struct {
|
||||
Key string `json:"key"`
|
||||
Key string `json:"key"`
|
||||
Body []byte `json:"body"`
|
||||
}
|
||||
|
||||
type PrettifyTest struct {
|
||||
@@ -46,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"`
|
||||
@@ -99,12 +101,14 @@ func TestResponse(t *testing.T) {
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
in := map[string]interface{}{
|
||||
"key": "value",
|
||||
"key": "value",
|
||||
"body": []byte("test"),
|
||||
}
|
||||
out := new(test)
|
||||
err := Convert(in, &out)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "value", out.Key)
|
||||
utils.AssertEqual(t, "test", string(out.Body))
|
||||
}
|
||||
|
||||
func TestConvertType(t *testing.T) {
|
||||
@@ -354,6 +358,11 @@ func Test_DoRequest(t *testing.T) {
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
|
||||
request.Headers["host"] = "tea-cn-hangzhou.aliyuncs.com:80"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
|
||||
runtimeObj["socks5Proxy"] = "# #%gfdf"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
@@ -533,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,
|
||||
@@ -551,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}
|
||||
|
||||
Reference in New Issue
Block a user