Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6355725204 | |||
| b6aa048c4a | |||
| 5da4c33a57 | |||
| 2a316b984d |
@@ -4,5 +4,7 @@ go 1.14
|
||||
|
||||
require (
|
||||
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
|
||||
github.com/json-iterator/go v1.1.10
|
||||
github.com/modern-go/reflect2 v1.0.1
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+19
-22
@@ -209,7 +209,7 @@ func (err *CastError) Error() string {
|
||||
// Convert is use convert map[string]interface object to struct
|
||||
func Convert(in interface{}, out interface{}) error {
|
||||
byt, _ := json.Marshal(in)
|
||||
err := json.Unmarshal(byt, out)
|
||||
err := jsonParser.Unmarshal(byt, out)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -271,20 +271,9 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
request.Protocol = String(strings.ToLower(StringValue(request.Protocol)))
|
||||
}
|
||||
|
||||
if StringValue(request.Protocol) == "http" {
|
||||
request.Port = Int(80)
|
||||
} else if StringValue(request.Protocol) == "https" {
|
||||
request.Port = Int(443)
|
||||
}
|
||||
|
||||
requestURL := ""
|
||||
request.Domain = request.Headers["host"]
|
||||
matched, _ := regexp.MatchString(":", StringValue(request.Domain))
|
||||
if matched {
|
||||
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
|
||||
} else {
|
||||
requestURL = fmt.Sprintf("%s://%s:%d%s", StringValue(request.Protocol), StringValue(request.Domain), IntValue(request.Port), StringValue(request.Pathname))
|
||||
}
|
||||
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
|
||||
queryParams := request.Query
|
||||
// sort QueryParams by key
|
||||
q := url.Values{}
|
||||
@@ -425,7 +414,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
|
||||
DualStack: true,
|
||||
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr), IntValue(req.Port)),
|
||||
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr)),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -433,7 +422,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
trans.Dial = dialer.Dial
|
||||
}
|
||||
} else {
|
||||
trans.DialContext = setDialContext(runtime, IntValue(req.Port))
|
||||
trans.DialContext = setDialContext(runtime)
|
||||
}
|
||||
return trans, nil
|
||||
}
|
||||
@@ -521,22 +510,20 @@ func getSocks5Proxy(runtime *RuntimeObject) (proxy *url.URL, err error) {
|
||||
return proxy, err
|
||||
}
|
||||
|
||||
func getLocalAddr(localAddr string, port int) (addr *net.TCPAddr) {
|
||||
func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
|
||||
if localAddr != "" {
|
||||
addr = &net.TCPAddr{
|
||||
Port: port,
|
||||
IP: []byte(localAddr),
|
||||
IP: []byte(localAddr),
|
||||
}
|
||||
}
|
||||
return addr
|
||||
}
|
||||
|
||||
func setDialContext(runtime *RuntimeObject, port int) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
|
||||
func setDialContext(runtime *RuntimeObject) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
|
||||
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
if runtime.LocalAddr != nil && StringValue(runtime.LocalAddr) != "" {
|
||||
netAddr := &net.TCPAddr{
|
||||
Port: port,
|
||||
IP: []byte(StringValue(runtime.LocalAddr)),
|
||||
IP: []byte(StringValue(runtime.LocalAddr)),
|
||||
}
|
||||
return (&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
@@ -726,7 +713,9 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
|
||||
if !fieldValue.IsValid() || fieldValue.IsNil() {
|
||||
continue
|
||||
}
|
||||
if field.Type.Kind().String() == "struct" {
|
||||
if field.Type.String() == "io.Reader" || field.Type.String() == "io.Writer" {
|
||||
continue
|
||||
} else if field.Type.Kind().String() == "struct" {
|
||||
out[name] = structToMap(fieldValue)
|
||||
} else if field.Type.Kind().String() == "ptr" &&
|
||||
field.Type.Elem().Kind().String() == "struct" {
|
||||
@@ -1110,3 +1099,11 @@ func Prettify(i interface{}) string {
|
||||
resp, _ := json.MarshalIndent(i, "", " ")
|
||||
return string(resp)
|
||||
}
|
||||
|
||||
func ToInt(a *int32) *int {
|
||||
return Int(int(Int32Value(a)))
|
||||
}
|
||||
|
||||
func ToInt32(a *int) *int32 {
|
||||
return Int32(int32(IntValue(a)))
|
||||
}
|
||||
|
||||
+16
-13
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -138,16 +139,6 @@ func TestConvert(t *testing.T) {
|
||||
utils.AssertEqual(t, "test", string(out.Body))
|
||||
}
|
||||
|
||||
func TestConvertType(t *testing.T) {
|
||||
in := map[string]interface{}{
|
||||
"key": 123,
|
||||
}
|
||||
out := new(test)
|
||||
err := Convert(in, &out)
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "json: cannot unmarshal number into Go struct field test.key of type string", err.Error())
|
||||
}
|
||||
|
||||
func TestRuntimeObject(t *testing.T) {
|
||||
runtimeobject := NewRuntimeObject(nil)
|
||||
utils.AssertNil(t, runtimeobject.IgnoreSSL)
|
||||
@@ -240,6 +231,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) {
|
||||
@@ -273,9 +266,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"])
|
||||
@@ -399,7 +395,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)
|
||||
@@ -456,7 +451,7 @@ func Test_DoRequest(t *testing.T) {
|
||||
runtimeObj["cert"] = cert
|
||||
runtimeObj["ca"] = "private ca"
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyun.com"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
_, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNotNil(t, err)
|
||||
|
||||
runtimeObj["ca"] = ca
|
||||
@@ -558,7 +553,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")
|
||||
@@ -780,3 +775,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