-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e11e99
commit 63ea544
Showing
3 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright © 2023 Patrick Hermann [email protected] | ||
*/ | ||
|
||
package internal | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func DownloadFile(url string) (string, error) { | ||
|
||
// HTTP GET REQUEST | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
log.Error("ERROR DOWNLOADING THE FILE: ", err) | ||
os.Exit(3) | ||
} | ||
defer resp.Body.Close() | ||
|
||
// CHECK HTTP STATUS | ||
if resp.StatusCode != http.StatusOK { | ||
log.Error("ERROR DOWNLOADING THE FILE: STATUS ", resp.StatusCode) | ||
os.Exit(3) | ||
} | ||
|
||
// READ ANSWER | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Error("ERROR DOWNLOADING THE FILE: ", err) | ||
os.Exit(3) | ||
} | ||
|
||
return string(body), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package internal | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestDownloadFile(t *testing.T) { | ||
// Create a test HTTP server | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Simulate a successful response | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte("Test content of the file")) | ||
if err != nil { | ||
fmt.Println("ERROR BY WRITING RESPONSE: ", err) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
// Call the DownloadFile function with the test URL | ||
content, err := DownloadFile(ts.URL) | ||
if err != nil { | ||
t.Fatalf("DownloadFile returned an error: %v", err) | ||
} | ||
|
||
// Verify the content of the downloaded file | ||
expected := "Test content of the file" | ||
if content != expected { | ||
t.Errorf("Expected '%s', but got '%s'", expected, content) | ||
} | ||
} |