From c216fad65c3869ec726774bfecfd31dc958a92c8 Mon Sep 17 00:00:00 2001 From: arberkatellari Date: Fri, 9 Dec 2022 10:09:18 -0500 Subject: [PATCH] Improving loadConfigFromHTTP redundant URL parse & testing it --- config/config.go | 7 ++----- config/config_test.go | 32 ++++++++++---------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/config/config.go b/config/config.go index 98671fa19..0ec2aacc2 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/config/config_test.go b/config/config_test.go index b41639434..6e365ac5e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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()