MCPcopy
hub / github.com/koderover/zadig / TestConnection

Function TestConnection

pkg/microservice/aslan/core/system/service/proxy.go:109–147  ·  view source on GitHub ↗
(args *commonmodels.Proxy, log *zap.SugaredLogger)

Source from the content-addressed store, hash-verified

107}
108
109func TestConnection(args *commonmodels.Proxy, log *zap.SugaredLogger) error {
110 if args == nil {
111 log.Error("invalid args")
112 return e.ErrTestConnection.AddDesc("invalid args")
113 }
114
115 request, err := http.NewRequest("GET", "https://www.baidu.com", nil)
116 if err != nil {
117 log.Errorf("http.NewRequest failed, err:%v", err)
118 return e.ErrTestConnection.AddErr(err)
119 }
120
121 var ret *http.Response
122 client := &http.Client{
123 Timeout: 5 * time.Second,
124 }
125
126 p, err := url.Parse(args.GetProxyURL())
127 if err == nil {
128 proxy := http.ProxyURL(p)
129 trans := &http.Transport{
130 Proxy: proxy,
131 }
132 client.Transport = trans
133 }
134
135 ret, err = client.Do(request)
136 if err != nil {
137 log.Errorf("client.Do failed, err:%v", err)
138 return e.ErrTestConnection.AddErr(err)
139 }
140 defer func() { _ = ret.Body.Close() }()
141
142 if ret.StatusCode != 200 {
143 return e.ErrTestConnection
144 }
145
146 return nil
147}

Callers 1

TestConnectionFunction · 0.92

Calls 8

AddDescMethod · 0.80
AddErrMethod · 0.80
DoMethod · 0.80
ErrorMethod · 0.65
ParseMethod · 0.65
ErrorfMethod · 0.45
GetProxyURLMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected