From 3adc4128b68e91994c6b6d9190faa88e02ca9f55 Mon Sep 17 00:00:00 2001 From: per1234 Date: Sun, 6 Jun 2021 22:35:26 -0700 Subject: [PATCH] Use most appropriate function for checking URL From the http.Get() documentation: > Caller should close resp.Body when done reading from it. This was not done previously. Since the body is not needed, http.Head() is more appropriate for this application and it does not impose the requirement to add additional code to clean up. --- internal/rule/rulefunction/library.go | 2 +- internal/rule/rulefunction/library_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/rule/rulefunction/library.go b/internal/rule/rulefunction/library.go index b0cbf248e..b80fd7628 100644 --- a/internal/rule/rulefunction/library.go +++ b/internal/rule/rulefunction/library.go @@ -1011,7 +1011,7 @@ func LibraryPropertiesUrlFieldDeadLink() (result ruleresult.Type, output string) } logrus.Tracef("Checking URL: %s", url) - httpResponse, err := http.Get(url) + httpResponse, err := http.Head(url) if err != nil { return ruleresult.Fail, err.Error() } diff --git a/internal/rule/rulefunction/library_test.go b/internal/rule/rulefunction/library_test.go index c6df14e2a..86fd4d91f 100644 --- a/internal/rule/rulefunction/library_test.go +++ b/internal/rule/rulefunction/library_test.go @@ -751,7 +751,7 @@ func TestLibraryPropertiesUrlFieldDeadLink(t *testing.T) { testTables := []libraryRuleFunctionTestTable{ {"Unable to load", "InvalidLibraryProperties", ruleresult.NotRun, ""}, {"Not defined", "MissingFields", ruleresult.NotRun, ""}, - {"Bad URL", "BadURL", ruleresult.Fail, "^Get \"http://invalid/\": dial tcp: lookup invalid:"}, + {"Bad URL", "BadURL", ruleresult.Fail, "^Head \"http://invalid/\": dial tcp: lookup invalid:"}, {"HTTP error 404", "URL404", ruleresult.Fail, "^404 Not Found$"}, {"Good URL", "Recursive", ruleresult.Pass, ""}, }