11#include " gtest/gtest.h"
22#include < s3cpp/s3.h>
33
4- TEST (S3, ListObjectsNoPrefix ) {
4+ TEST (S3, ListObjectsBucket ) {
55 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
66 try {
77 // Assuming the bucket has the 10K objects
88 // Once we implement PutObject we will do this ourselves with s3cpp
9- ListBucketResult res = client.ListObjects (" my-bucket" );
10- EXPECT_EQ (res.Contents .size (), 0 );
9+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" );
10+ if (!res)
11+ GTEST_FAIL ();
12+ EXPECT_EQ (res->Contents .size (), 0 );
13+ } catch (const std::exception& e) {
14+ const std::string emsg = e.what ();
15+ if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
16+ GTEST_SKIP_ (" Skipping MinIOBasicRequest: Server not up" );
17+ }
18+ throw ;
19+ }
20+ }
21+
22+ TEST (S3, ListObjectsBucketNotExists) {
23+ S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
24+ try {
25+ std::expected<ListBucketResult, Error> res = client.ListObjects (" Does-not-exist" );
26+ if (res.has_value ()) // We must return error
27+ GTEST_FAIL ();
28+ Error error = res.error ();
29+ // EXPECT_EQ(error.Code, "InvalidBucketName");
1130 } catch (const std::exception& e) {
1231 const std::string emsg = e.what ();
1332 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
@@ -21,8 +40,10 @@ TEST(S3, ListObjectsFilePrefix) {
2140 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
2241 try {
2342 // path/to/file_1.txt must exist
24- ListBucketResult res = client.ListObjects (" my-bucket" , " path/to/file_1.txt" );
25- EXPECT_EQ (res.Contents .size (), 1 );
43+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" , " path/to/file_1.txt" );
44+ if (!res)
45+ GTEST_FAIL ();
46+ EXPECT_EQ (res->Contents .size (), 1 );
2647 } catch (const std::exception& e) {
2748 const std::string emsg = e.what ();
2849 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
@@ -35,9 +56,11 @@ TEST(S3, ListObjectsFilePrefix) {
3556TEST (S3, ListObjectsDirPrefix) {
3657 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
3758 try {
38- // Get 100 keys
39- ListBucketResult res = client.ListObjects (" my-bucket" , " path/to/" , 100 );
40- EXPECT_EQ (res.Contents .size (), 100 );
59+ // Get 100 keys
60+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" , " path/to/" , 100 );
61+ if (!res)
62+ GTEST_FAIL ();
63+ EXPECT_EQ (res->Contents .size (), 100 );
4164 } catch (const std::exception& e) {
4265 const std::string emsg = e.what ();
4366 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
@@ -50,8 +73,10 @@ TEST(S3, ListObjectsDirPrefix) {
5073TEST (S3, ListObjectsDirPrefixMaxKeys) {
5174 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
5275 try {
53- ListBucketResult res = client.ListObjects (" my-bucket" , " path/to/" , 1 );
54- EXPECT_EQ (res.Contents .size (), 1 );
76+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" , " path/to/" , 1 );
77+ if (!res)
78+ GTEST_FAIL ();
79+ EXPECT_EQ (res->Contents .size (), 1 );
5580 } catch (const std::exception& e) {
5681 const std::string emsg = e.what ();
5782 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
@@ -64,24 +89,26 @@ TEST(S3, ListObjectsDirPrefixMaxKeys) {
6489TEST (S3, ListObjectsCheckFields) {
6590 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
6691 try {
67- ListBucketResult res = client.ListObjects (" my-bucket" , " path/to/" , 2 );
92+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" , " path/to/" , 2 );
93+ if (!res)
94+ GTEST_FAIL ();
6895
69- EXPECT_EQ (res. Name , " my-bucket" );
70- EXPECT_EQ (res. Prefix , " path/to/" );
71- EXPECT_EQ (res. MaxKeys , 2 );
72- EXPECT_EQ (res. IsTruncated , true );
73- EXPECT_FALSE (res. NextContinuationToken .empty ());
96+ EXPECT_EQ (res-> Name , " my-bucket" );
97+ EXPECT_EQ (res-> Prefix , " path/to/" );
98+ EXPECT_EQ (res-> MaxKeys , 2 );
99+ EXPECT_EQ (res-> IsTruncated , true );
100+ EXPECT_FALSE (res-> NextContinuationToken .empty ());
74101
75102 // Should have exactly 2 keys
76- EXPECT_EQ (res. Contents .size (), 2 );
103+ EXPECT_EQ (res-> Contents .size (), 2 );
77104
78- EXPECT_EQ (res. Contents [0 ].Key , " path/to/file_1.txt" );
79- EXPECT_EQ (res. Contents [0 ].Size , 26 );
80- EXPECT_EQ (res. Contents [0 ].StorageClass , " STANDARD" );
105+ EXPECT_EQ (res-> Contents [0 ].Key , " path/to/file_1.txt" );
106+ EXPECT_EQ (res-> Contents [0 ].Size , 26 );
107+ EXPECT_EQ (res-> Contents [0 ].StorageClass , " STANDARD" );
81108
82- EXPECT_EQ (res. Contents [1 ].Key , " path/to/file_10.txt" );
83- EXPECT_EQ (res. Contents [1 ].Size , 27 );
84- EXPECT_EQ (res. Contents [1 ].StorageClass , " STANDARD" );
109+ EXPECT_EQ (res-> Contents [1 ].Key , " path/to/file_10.txt" );
110+ EXPECT_EQ (res-> Contents [1 ].Size , 27 );
111+ EXPECT_EQ (res-> Contents [1 ].StorageClass , " STANDARD" );
85112
86113 } catch (const std::exception& e) {
87114 const std::string emsg = e.what ();
@@ -96,8 +123,10 @@ TEST(S3, ListObjectsCheckLenKeys) {
96123 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
97124 try {
98125 // has 10K objects - limit is 1000 keys
99- ListBucketResult response = client.ListObjects (" my-bucket" , " path/to/" );
100- EXPECT_EQ (response.Contents .size (), 1000 );
126+ std::expected<ListBucketResult, Error> res = client.ListObjects (" my-bucket" , " path/to/" );
127+ if (!res)
128+ GTEST_FAIL ();
129+ EXPECT_EQ (res->Contents .size (), 1000 );
101130 } catch (const std::exception& e) {
102131 const std::string emsg = e.what ();
103132 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
@@ -117,14 +146,17 @@ TEST(S3, ListObjectsPaginator) {
117146 int pageCount = 0 ;
118147
119148 while (paginator.HasMorePages ()) {
120- ListBucketResult page = paginator.NextPage ();
121- totalObjects += page.Contents .size ();
122- if (page.Contents .size () > 0 )
149+ std::expected<ListBucketResult, Error> page = paginator.NextPage ();
150+ if (!page) {
151+ GTEST_FAIL ();
152+ }
153+ totalObjects += page->Contents .size ();
154+ if (page->Contents .size () > 0 )
123155 pageCount++;
124156
125157 if (paginator.HasMorePages ()) {
126- EXPECT_EQ (page. Contents .size (), 100 );
127- EXPECT_TRUE (page. IsTruncated );
158+ EXPECT_EQ (page-> Contents .size (), 100 );
159+ EXPECT_TRUE (page-> IsTruncated );
128160 }
129161 }
130162
@@ -142,7 +174,10 @@ TEST(S3, ListObjectsPaginator) {
142174TEST (S3, GetObjectExists) {
143175 S3Client client (" minio_access" , " minio_secret" , " 127.0.0.1:9000" , S3AddressingStyle::PathStyle);
144176 try {
145- ListBucketResult response = client.GetObject (" my-bucket" , " path/to/file_1.txt" );
177+ auto response = client.GetObject (" my-bucket" , " path/to/file_1.txt" );
178+ if (!response) {
179+ GTEST_FAIL ();
180+ }
146181 } catch (const std::exception& e) {
147182 const std::string emsg = e.what ();
148183 if (emsg == " libcurl error: Could not connect to server" || emsg == " libcurl error: Couldn't connect to server" ) {
0 commit comments