@@ -359,6 +359,41 @@ func GetBucketRetention(bucketName string) (*http.Response, error) {
359359 return response , err
360360}
361361
362+ func UploadAnObject (bucketName string , fileName string ) (* http.Response , error ) {
363+ /*
364+ Helper function to upload a file to a bucket for testing.
365+ POST {{baseUrl}}/buckets/:bucket_name/objects/upload
366+ */
367+ boundary := "WebKitFormBoundaryWtayBM7t9EUQb8q3"
368+ boundaryStart := "------" + boundary + "\r \n "
369+ contentDispositionOne := "Content-Disposition: form-data; name=\" 2\" ; "
370+ contentDispositionTwo := "filename=\" " + fileName + "\" \r \n "
371+ contenType := "Content-Type: text/plain\r \n \r \n a\n \r \n "
372+ boundaryEnd := "------" + boundary + "--\r \n "
373+ file := boundaryStart + contentDispositionOne + contentDispositionTwo +
374+ contenType + boundaryEnd
375+ arrayOfBytes := []byte (file )
376+ requestDataBody := bytes .NewReader (arrayOfBytes )
377+ request , err := http .NewRequest (
378+ "POST" ,
379+ "http://localhost:9090/api/v1/buckets/" + bucketName + "/objects/upload" ,
380+ requestDataBody ,
381+ )
382+ if err != nil {
383+ log .Println (err )
384+ }
385+ request .Header .Add ("Cookie" , fmt .Sprintf ("token=%s" , token ))
386+ request .Header .Add (
387+ "Content-Type" ,
388+ "multipart/form-data; boundary=----" + boundary ,
389+ )
390+ client := & http.Client {
391+ Timeout : 2 * time .Second ,
392+ }
393+ response , err := client .Do (request )
394+ return response , err
395+ }
396+
362397func TestAddBucket (t * testing.T ) {
363398 assert := assert .New (t )
364399
@@ -941,3 +976,39 @@ func TestBucketRetention(t *testing.T) {
941976 expected := "Http Response: {\" mode\" :\" compliance\" ,\" unit\" :\" years\" ,\" validity\" :3}\n "
942977 assert .Equal (expected , finalResponse , finalResponse )
943978}
979+
980+ func TestUploadObjectToBucket (t * testing.T ) {
981+ /*
982+ Function to test the upload of an object to a bucket.
983+ */
984+
985+ // Test's variables
986+ assert := assert .New (t )
987+ bucketName := "testuploadobjecttobucket1"
988+ fileName := "sample.txt"
989+
990+ // 1. Create the bucket
991+ response , err := AddBucket (bucketName , false , false , nil , nil )
992+ assert .Nil (err )
993+ if err != nil {
994+ log .Println (err )
995+ return
996+ }
997+ if response != nil {
998+ assert .Equal (201 , response .StatusCode , "Status Code is incorrect" )
999+ }
1000+
1001+ // 2. Upload the object to the bucket
1002+ uploadResponse , uploadError := UploadAnObject (bucketName , fileName )
1003+ assert .Nil (uploadError )
1004+ if uploadError != nil {
1005+ log .Println (uploadError )
1006+ return
1007+ }
1008+
1009+ // 3. Verify the object was uploaded
1010+ finalResponse := inspectHTTPResponse (uploadResponse )
1011+ if uploadResponse != nil {
1012+ assert .Equal (200 , uploadResponse .StatusCode , finalResponse )
1013+ }
1014+ }
0 commit comments