@@ -18,7 +18,9 @@ package integration
1818
1919import (
2020 "bytes"
21+ b64 "encoding/base64"
2122 "encoding/json"
23+ "errors"
2224 "fmt"
2325 "io"
2426 "io/ioutil"
@@ -41,6 +43,14 @@ import (
4143
4244var token string
4345
46+ func encodeBase64 (fileName string ) string {
47+ /*
48+ Helper function to encode in base64 the file name so we can get the path
49+ */
50+ path := b64 .StdEncoding .EncodeToString ([]byte (fileName ))
51+ return path
52+ }
53+
4454func inspectHTTPResponse (httpResponse * http.Response ) string {
4555 /*
4656 Helper function to inspect the content of a HTTP response.
@@ -359,6 +369,29 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
359369 return response , err
360370}
361371
372+ func DownloadObject (bucketName string , path string ) (* http.Response , error ) {
373+ /*
374+ Helper function to download an object from a bucket.
375+ GET: {{baseUrl}}/buckets/bucketName/objects/download?prefix=file
376+ */
377+ request , err := http .NewRequest (
378+ "GET" ,
379+ "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/download?prefix=" +
380+ path ,
381+ nil ,
382+ )
383+ if err != nil {
384+ log .Println (err )
385+ }
386+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
387+ request .Header .Add ("Content-Type" , "application/json" )
388+ client := & http.Client {
389+ Timeout : 2 * time .Second ,
390+ }
391+ response , err := client .Do (request )
392+ return response , err
393+ }
394+
362395func UploadAnObject (bucketName string , fileName string ) (* http.Response , error ) {
363396 /*
364397 Helper function to upload a file to a bucket for testing.
@@ -1023,6 +1056,79 @@ func TestBucketRetention(t *testing.T) {
10231056 assert .Equal (expected , finalResponse , finalResponse )
10241057}
10251058
1059+ func TestDownloadObject (t * testing.T ) {
1060+ /*
1061+ Test to download an object from a given bucket.
1062+ */
1063+
1064+ // Vars
1065+ assert := assert .New (t )
1066+ bucketName := "testdownloadobjbucketone"
1067+ fileName := "testdownloadobjectfilenameone"
1068+ path := encodeBase64 (fileName )
1069+ workingDirectory , getWdErr := os .Getwd ()
1070+ if getWdErr != nil {
1071+ assert .Fail ("Couldn't get the directory" )
1072+ }
1073+
1074+ // 1. Create the bucket
1075+ response , err := AddBucket (bucketName , true , true , nil , nil )
1076+ assert .Nil (err )
1077+ if err != nil {
1078+ log .Println (err )
1079+ assert .Fail ("Error creating the bucket" )
1080+ return
1081+ }
1082+ if response != nil {
1083+ assert .Equal (201 , response .StatusCode , inspectHTTPResponse (response ))
1084+ }
1085+
1086+ // 2. Upload an object to the bucket
1087+ uploadResponse , uploadError := UploadAnObject (bucketName , fileName )
1088+ assert .Nil (uploadError )
1089+ if uploadError != nil {
1090+ log .Println (uploadError )
1091+ return
1092+ }
1093+ if uploadResponse != nil {
1094+ assert .Equal (
1095+ 200 ,
1096+ uploadResponse .StatusCode ,
1097+ inspectHTTPResponse (uploadResponse ),
1098+ )
1099+ }
1100+
1101+ // 3. Download the object from the bucket
1102+ downloadResponse , downloadError := DownloadObject (bucketName , path )
1103+ assert .Nil (downloadError )
1104+ if downloadError != nil {
1105+ log .Println (downloadError )
1106+ assert .Fail ("Error downloading the object" )
1107+ return
1108+ }
1109+ finalResponse := inspectHTTPResponse (downloadResponse )
1110+ if downloadResponse != nil {
1111+ assert .Equal (
1112+ 200 ,
1113+ downloadResponse .StatusCode ,
1114+ finalResponse ,
1115+ )
1116+ }
1117+
1118+ // 4. Verify the file was downloaded
1119+ files , err := ioutil .ReadDir (workingDirectory )
1120+ if err != nil {
1121+ log .Fatal (err )
1122+ }
1123+ for _ , file := range files {
1124+ fmt .Println (file .Name (), file .IsDir ())
1125+ }
1126+ if _ , err := os .Stat (workingDirectory ); errors .Is (err , os .ErrNotExist ) {
1127+ // path/to/whatever does not exist
1128+ assert .Fail ("File wasn't downloaded" )
1129+ }
1130+ }
1131+
10261132func TestUploadObjectToBucket (t * testing.T ) {
10271133 /*
10281134 Function to test the upload of an object to a bucket.
0 commit comments