Improving loadConfigFromHTTP redundant URL parse & testing it

This commit is contained in:
arberkatellari
2022-12-09 10:09:18 -05:00
committed by Dan Christian Bogos
parent bf2f6c0927
commit c216fad65c
2 changed files with 12 additions and 27 deletions

View File

@@ -23,7 +23,6 @@ import (
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
@@ -946,14 +945,12 @@ func loadConfigFromFile(ctx *context.Context, jsonFilePath string, loadFuncs Sec
func loadConfigFromHTTP(ctx *context.Context, urlPaths string, loadFuncs Sections, cfg *CGRConfig) (err error) {
for _, urlPath := range strings.Split(urlPaths, utils.InfieldSep) {
if _, err = url.ParseRequestURI(urlPath); err != nil {
return
}
var myClient = &http.Client{
Timeout: CgrConfig().GeneralCfg().ReplyTimeout,
}
var req *http.Request
if req, err = http.NewRequestWithContext(ctx, "GET", urlPath, nil); err != nil {
if req, err = http.NewRequestWithContext(ctx, utils.EmptyString, urlPath, nil); err != nil {
return
}
var cfgReq *http.Response

View File

@@ -5670,16 +5670,6 @@ func TestCgrCfgJSONDefaultsConfigS(t *testing.T) {
}
}
func TestLoadConfigFromHTTP(t *testing.T) {
cfgCgr := NewDefaultCGRConfig()
url := "inexistentURL"
expected := "parse \"inexistentURL\": invalid URI for request"
if err := loadConfigFromHTTP(context.Background(), url, cfgCgr.sections, nil); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, received %+v", expected, err)
}
}
func TestLoadConfigFromReaderError(t *testing.T) {
expectedErrFile := "open randomfile.go: no such file or directory"
file, err := os.Open("randomfile.go")
@@ -6304,20 +6294,18 @@ func TestCGRConfigLoadConfigDBCfgError(t *testing.T) {
}
}
// unfinished
// Cant get err of http.NewRequestWithContext
// func TestLoadConfigFromHTTPErrorNewReqCtx(t *testing.T) {
// cfgCgr := NewDefaultCGRConfig()
func TestLoadConfigFromHTTPErrorNewReqCtx(t *testing.T) {
cfgCgr := NewDefaultCGRConfig()
// // ctx, cancel := context.WithTimeout(context.Background(), 10*time.Nanosecond)
// // cancel()
// url := "https://raw.githubusercontent.com/cgrates/cgrates/master/data/conf/samples/tutmongo/cgrates.json"
// expected := "parse \"inexistentURL\": invalid URI for request"
// if err := loadConfigFromHTTP(nil, url, cfgCgr.sections, nil); err == nil || err.Error() != expected {
// t.Errorf("Expected %+v, \nreceived %+v", expected, err)
// }
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Nanosecond)
cancel()
url := "inexistentURL"
expected := `path:"inexistentURL" is not reachable`
if err := loadConfigFromHTTP(ctx, url, cfgCgr.sections, nil); err == nil || err.Error() != expected {
t.Errorf("Expected %+v, \nreceived %+v", expected, err)
}
// }
}
func TestGetDftRemoteHostCfg(t *testing.T) {
cgrCfg := NewDefaultCGRConfig()