rs3gw 0.2.1

High-Performance AI/HPC Object Storage Gateway powered by scirs2-io
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
//! Object operation tests for rs3gw

mod common;

use common::setup_test_server;

#[tokio::test]
async fn test_object_operations() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket first
    client
        .create_bucket()
        .bucket("objects-test")
        .send()
        .await
        .unwrap();

    // Put object
    let content = b"Hello, rs3gw!";
    let put_result = client
        .put_object()
        .bucket("objects-test")
        .key("test-file.txt")
        .content_type("text/plain")
        .body(content.to_vec().into())
        .send()
        .await;
    assert!(
        put_result.is_ok(),
        "Failed to put object: {:?}",
        put_result.err()
    );

    // Head object
    let head_result = client
        .head_object()
        .bucket("objects-test")
        .key("test-file.txt")
        .send()
        .await;
    assert!(head_result.is_ok(), "Failed to head object");
    let head = head_result.unwrap();
    assert_eq!(head.content_length(), Some(content.len() as i64));

    // Get object
    let get_result = client
        .get_object()
        .bucket("objects-test")
        .key("test-file.txt")
        .send()
        .await;
    assert!(get_result.is_ok(), "Failed to get object");
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), content);

    // Delete object
    let delete_result = client
        .delete_object()
        .bucket("objects-test")
        .key("test-file.txt")
        .send()
        .await;
    assert!(delete_result.is_ok(), "Failed to delete object");

    // Verify object is deleted
    let get_result = client
        .get_object()
        .bucket("objects-test")
        .key("test-file.txt")
        .send()
        .await;
    assert!(get_result.is_err(), "Object should have been deleted");
}

#[tokio::test]
async fn test_list_objects_v2() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("list-test")
        .send()
        .await
        .unwrap();

    // Put multiple objects
    for key in ["foo/a.txt", "foo/b.txt", "bar/c.txt", "root.txt"] {
        client
            .put_object()
            .bucket("list-test")
            .key(key)
            .body(b"test content".to_vec().into())
            .send()
            .await
            .unwrap();
    }

    // List all objects
    let list_result = client.list_objects_v2().bucket("list-test").send().await;
    assert!(list_result.is_ok(), "Failed to list objects");
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    assert_eq!(contents.len(), 4);

    // List with prefix
    let list_result = client
        .list_objects_v2()
        .bucket("list-test")
        .prefix("foo/")
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    assert_eq!(contents.len(), 2);

    // List with delimiter
    let list_result = client
        .list_objects_v2()
        .bucket("list-test")
        .delimiter("/")
        .send()
        .await;
    assert!(list_result.is_ok());
    let result = list_result.unwrap();
    let contents = result.contents();
    let prefixes = result.common_prefixes();
    assert_eq!(contents.len(), 1); // root.txt
    assert_eq!(prefixes.len(), 2); // foo/ and bar/

    // Test pagination with start_after
    let list_result = client
        .list_objects_v2()
        .bucket("list-test")
        .start_after("foo/a.txt")
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    // Should return objects after "foo/a.txt" (alphabetically: foo/b.txt, root.txt)
    assert_eq!(contents.len(), 2);

    // Test pagination with max_keys and verify is_truncated/continuation_token
    let list_result = client
        .list_objects_v2()
        .bucket("list-test")
        .max_keys(2)
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    assert_eq!(contents.len(), 2);
    assert_eq!(list_output.is_truncated(), Some(true));
    assert!(list_output.next_continuation_token().is_some());

    // Use continuation_token to get next page
    let continuation_token = list_output.next_continuation_token().unwrap().to_string();
    let list_result = client
        .list_objects_v2()
        .bucket("list-test")
        .continuation_token(&continuation_token)
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    // Should return remaining objects
    assert_eq!(contents.len(), 2);
}

#[tokio::test]
async fn test_list_objects_v1() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("list-v1-test")
        .send()
        .await
        .unwrap();

    // Put multiple objects
    for key in ["alpha/a.txt", "alpha/b.txt", "beta/c.txt", "root.txt"] {
        client
            .put_object()
            .bucket("list-v1-test")
            .key(key)
            .body(b"test content".to_vec().into())
            .send()
            .await
            .unwrap();
    }

    // List all objects using V1 API (list_objects without v2)
    #[allow(deprecated)]
    let list_result = client.list_objects().bucket("list-v1-test").send().await;
    assert!(
        list_result.is_ok(),
        "Failed to list objects V1: {:?}",
        list_result.err()
    );
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    assert_eq!(contents.len(), 4);

    // List with prefix using V1
    #[allow(deprecated)]
    let list_result = client
        .list_objects()
        .bucket("list-v1-test")
        .prefix("alpha/")
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    assert_eq!(contents.len(), 2);

    // List with delimiter using V1
    #[allow(deprecated)]
    let list_result = client
        .list_objects()
        .bucket("list-v1-test")
        .delimiter("/")
        .send()
        .await;
    assert!(list_result.is_ok());
    let result = list_result.unwrap();
    let contents = result.contents();
    let prefixes = result.common_prefixes();
    assert_eq!(contents.len(), 1); // root.txt
    assert_eq!(prefixes.len(), 2); // alpha/ and beta/

    // Test with marker (pagination)
    #[allow(deprecated)]
    let list_result = client
        .list_objects()
        .bucket("list-v1-test")
        .marker("alpha/b.txt")
        .send()
        .await;
    assert!(list_result.is_ok());
    let list_output = list_result.unwrap();
    let contents = list_output.contents();
    // Should return objects after "alpha/b.txt" (alphabetically: beta/c.txt, root.txt)
    assert!(contents.len() >= 2);
}

#[tokio::test]
async fn test_object_with_metadata() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("meta-test")
        .send()
        .await
        .unwrap();

    // Put object with custom metadata
    let put_result = client
        .put_object()
        .bucket("meta-test")
        .key("with-meta.txt")
        .content_type("text/plain")
        .metadata("custom-key", "custom-value")
        .body(b"test content".to_vec().into())
        .send()
        .await;
    assert!(put_result.is_ok());

    // Head object and check metadata
    let head_result = client
        .head_object()
        .bucket("meta-test")
        .key("with-meta.txt")
        .send()
        .await;
    assert!(head_result.is_ok());
    let head = head_result.unwrap();
    assert_eq!(head.content_type(), Some("text/plain"));
    let metadata = head.metadata();
    assert!(metadata.is_some());
    assert_eq!(
        metadata.unwrap().get("custom-key"),
        Some(&"custom-value".to_string())
    );
}

#[tokio::test]
async fn test_copy_object() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("copy-test")
        .send()
        .await
        .unwrap();

    // Put source object with metadata
    let content = b"Hello, this is the source!";
    client
        .put_object()
        .bucket("copy-test")
        .key("source.txt")
        .content_type("text/plain")
        .metadata("original", "true")
        .body(content.to_vec().into())
        .send()
        .await
        .unwrap();

    // Copy object to new key
    let copy_result = client
        .copy_object()
        .bucket("copy-test")
        .key("destination.txt")
        .copy_source("copy-test/source.txt")
        .send()
        .await;
    assert!(
        copy_result.is_ok(),
        "Failed to copy object: {:?}",
        copy_result.err()
    );

    // Verify copied object
    let get_result = client
        .get_object()
        .bucket("copy-test")
        .key("destination.txt")
        .send()
        .await;
    assert!(get_result.is_ok());
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), content);

    // Verify metadata was copied
    let head_result = client
        .head_object()
        .bucket("copy-test")
        .key("destination.txt")
        .send()
        .await;
    assert!(head_result.is_ok());
    let head = head_result.unwrap();
    let metadata = head.metadata().unwrap();
    assert_eq!(metadata.get("original"), Some(&"true".to_string()));
}

#[tokio::test]
async fn test_range_request() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("range-test")
        .send()
        .await
        .unwrap();

    // Put object with known content
    let content = b"0123456789ABCDEFGHIJ";
    client
        .put_object()
        .bucket("range-test")
        .key("range.txt")
        .body(content.to_vec().into())
        .send()
        .await
        .unwrap();

    // Get partial content (first 5 bytes)
    let get_result = client
        .get_object()
        .bucket("range-test")
        .key("range.txt")
        .range("bytes=0-4")
        .send()
        .await;
    assert!(
        get_result.is_ok(),
        "Failed range request: {:?}",
        get_result.err()
    );
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), b"01234");

    // Get bytes from middle
    let get_result = client
        .get_object()
        .bucket("range-test")
        .key("range.txt")
        .range("bytes=10-14")
        .send()
        .await;
    assert!(get_result.is_ok());
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), b"ABCDE");

    // Get last 5 bytes
    let get_result = client
        .get_object()
        .bucket("range-test")
        .key("range.txt")
        .range("bytes=-5")
        .send()
        .await;
    assert!(get_result.is_ok());
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), b"FGHIJ");
}

#[tokio::test]
async fn test_object_tagging() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket and object
    client
        .create_bucket()
        .bucket("tagging-test")
        .send()
        .await
        .unwrap();
    client
        .put_object()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .body(b"content".to_vec().into())
        .send()
        .await
        .unwrap();

    // Get tagging (should be empty initially)
    let get_result = client
        .get_object_tagging()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .send()
        .await;
    assert!(
        get_result.is_ok(),
        "Failed to get tagging: {:?}",
        get_result.err()
    );
    let tagging = get_result.unwrap();
    assert!(tagging.tag_set().is_empty());

    // Put tagging
    use aws_sdk_s3::types::{Tag, Tagging};
    let tag1 = Tag::builder()
        .key("Environment")
        .value("Production")
        .build()
        .unwrap();
    let tag2 = Tag::builder()
        .key("Project")
        .value("rs3gw")
        .build()
        .unwrap();
    let tagging = Tagging::builder()
        .tag_set(tag1)
        .tag_set(tag2)
        .build()
        .unwrap();

    let put_result = client
        .put_object_tagging()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .tagging(tagging)
        .send()
        .await;
    assert!(
        put_result.is_ok(),
        "Failed to put tagging: {:?}",
        put_result.err()
    );

    // Get tagging (should have 2 tags now)
    let get_result = client
        .get_object_tagging()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .send()
        .await;
    assert!(get_result.is_ok());
    let tags = get_result.unwrap();
    let tag_set = tags.tag_set();
    assert_eq!(tag_set.len(), 2);

    // Delete tagging
    let delete_result = client
        .delete_object_tagging()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .send()
        .await;
    assert!(delete_result.is_ok(), "Failed to delete tagging");

    // Verify tagging is deleted
    let get_result = client
        .get_object_tagging()
        .bucket("tagging-test")
        .key("tagged-file.txt")
        .send()
        .await;
    assert!(get_result.is_ok());
    let tags = get_result.unwrap();
    assert!(tags.tag_set().is_empty());
}

#[tokio::test]
async fn test_delete_objects() {
    let (client, _temp_dir, _server) = setup_test_server().await;

    // Create bucket and multiple objects
    client
        .create_bucket()
        .bucket("batch-delete-test")
        .send()
        .await
        .unwrap();

    for i in 0..5 {
        client
            .put_object()
            .bucket("batch-delete-test")
            .key(format!("file{}.txt", i))
            .body(format!("content {}", i).into_bytes().into())
            .send()
            .await
            .unwrap();
    }

    // Verify all objects exist
    let list = client
        .list_objects_v2()
        .bucket("batch-delete-test")
        .send()
        .await
        .unwrap();
    assert_eq!(list.contents().len(), 5);

    // Delete 3 objects in batch
    use aws_sdk_s3::types::{Delete, ObjectIdentifier};
    let objects: Vec<ObjectIdentifier> = (0..3)
        .map(|i| {
            ObjectIdentifier::builder()
                .key(format!("file{}.txt", i))
                .build()
                .unwrap()
        })
        .collect();

    let delete = Delete::builder()
        .set_objects(Some(objects))
        .build()
        .unwrap();

    let delete_result = client
        .delete_objects()
        .bucket("batch-delete-test")
        .delete(delete)
        .send()
        .await;
    assert!(
        delete_result.is_ok(),
        "Failed to delete objects: {:?}",
        delete_result.err()
    );

    let result = delete_result.unwrap();
    assert_eq!(result.deleted().len(), 3);

    // Verify only 2 objects remain
    let list = client
        .list_objects_v2()
        .bucket("batch-delete-test")
        .send()
        .await
        .unwrap();
    assert_eq!(list.contents().len(), 2);

    // Verify the remaining objects are file3.txt and file4.txt
    let keys: Vec<&str> = list.contents().iter().filter_map(|o| o.key()).collect();
    assert!(keys.contains(&"file3.txt"));
    assert!(keys.contains(&"file4.txt"));
}

#[tokio::test]
async fn test_conditional_copy() {
    let (client, _temp_dir, server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("cond-copy-test")
        .send()
        .await
        .unwrap();

    // Put source object
    let content = b"Conditional copy test content";
    client
        .put_object()
        .bucket("cond-copy-test")
        .key("source.txt")
        .body(content.to_vec().into())
        .send()
        .await
        .unwrap();

    // Get the ETag and last modified time from source
    let head_result = client
        .head_object()
        .bucket("cond-copy-test")
        .key("source.txt")
        .send()
        .await
        .unwrap();
    let source_etag = head_result.e_tag().unwrap().to_string();

    // Use reqwest for raw HTTP calls to test conditional headers
    let http_client = reqwest::Client::new();
    let base_url = format!("http://{}", server.addr);

    // Test 1: Copy with If-Match (matching ETag) - should succeed
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest1.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header("x-amz-copy-source-if-match", &source_etag)
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        200,
        "Copy with matching If-Match should succeed"
    );

    // Verify the copy was created
    let get_result = client
        .get_object()
        .bucket("cond-copy-test")
        .key("dest1.txt")
        .send()
        .await;
    assert!(get_result.is_ok(), "Copied object should exist");

    // Test 2: Copy with If-Match (non-matching ETag) - should fail with 412
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest2.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header("x-amz-copy-source-if-match", "\"wrong-etag\"")
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        412,
        "Copy with non-matching If-Match should fail with 412"
    );

    // Test 3: Copy with If-None-Match (non-matching ETag) - should succeed
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest3.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header("x-amz-copy-source-if-none-match", "\"different-etag\"")
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        200,
        "Copy with non-matching If-None-Match should succeed"
    );

    // Test 4: Copy with If-None-Match (matching ETag) - should fail with 412
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest4.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header("x-amz-copy-source-if-none-match", &source_etag)
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        412,
        "Copy with matching If-None-Match should fail with 412"
    );

    // Test 5: Copy with If-Modified-Since (old date) - should succeed
    // Note: Jan 1, 2020 is a Wednesday
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest5.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header(
            "x-amz-copy-source-if-modified-since",
            "Wed, 01 Jan 2020 00:00:00 GMT",
        )
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        200,
        "Copy with old If-Modified-Since should succeed"
    );

    // Test 6: Copy with If-Modified-Since (future date) - should fail with 412
    // Note: Jan 1, 2030 is a Tuesday
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest6.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header(
            "x-amz-copy-source-if-modified-since",
            "Tue, 01 Jan 2030 00:00:00 GMT",
        )
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        412,
        "Copy with future If-Modified-Since should fail with 412"
    );

    // Test 7: Copy with If-Unmodified-Since (future date) - should succeed
    // Note: Jan 1, 2030 is a Tuesday
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest7.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header(
            "x-amz-copy-source-if-unmodified-since",
            "Tue, 01 Jan 2030 00:00:00 GMT",
        )
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        200,
        "Copy with future If-Unmodified-Since should succeed"
    );

    // Test 8: Copy with If-Unmodified-Since (old date) - should fail with 412
    // Note: Jan 1, 2020 is a Wednesday
    let copy_response = http_client
        .put(format!("{}/cond-copy-test/dest8.txt", base_url))
        .header("x-amz-copy-source", "cond-copy-test/source.txt")
        .header(
            "x-amz-copy-source-if-unmodified-since",
            "Wed, 01 Jan 2020 00:00:00 GMT",
        )
        .send()
        .await
        .unwrap();
    assert_eq!(
        copy_response.status(),
        412,
        "Copy with old If-Unmodified-Since should fail with 412"
    );

    // Verify only the successful copies exist
    let list_result = client
        .list_objects_v2()
        .bucket("cond-copy-test")
        .send()
        .await
        .unwrap();

    let keys: Vec<&str> = list_result
        .contents()
        .iter()
        .filter_map(|obj| obj.key())
        .collect();

    // Should have: source.txt, dest1.txt, dest3.txt, dest5.txt, dest7.txt (5 objects)
    assert!(keys.contains(&"source.txt"));
    assert!(keys.contains(&"dest1.txt"));
    assert!(keys.contains(&"dest3.txt"));
    assert!(keys.contains(&"dest5.txt"));
    assert!(keys.contains(&"dest7.txt"));
    assert!(!keys.contains(&"dest2.txt")); // Failed copy
    assert!(!keys.contains(&"dest4.txt")); // Failed copy
    assert!(!keys.contains(&"dest6.txt")); // Failed copy
    assert!(!keys.contains(&"dest8.txt")); // Failed copy
}

#[tokio::test]
async fn test_get_object_attributes() {
    let (client, _temp_dir, server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("attrs-test")
        .send()
        .await
        .unwrap();

    // Put an object with known content
    let content = b"Test content for GetObjectAttributes";
    client
        .put_object()
        .bucket("attrs-test")
        .key("test-file.txt")
        .body(content.to_vec().into())
        .send()
        .await
        .unwrap();

    // Use reqwest to call GetObjectAttributes (GET ?attributes)
    let http_client = reqwest::Client::new();
    let base_url = format!("http://{}", server.addr);

    // Test 1: Get all default attributes
    let response = http_client
        .get(format!("{}/attrs-test/test-file.txt?attributes", base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200);
    let body = response.text().await.unwrap();
    assert!(
        body.contains("GetObjectAttributesResponse"),
        "Response should be GetObjectAttributesResponse XML"
    );
    assert!(body.contains("ETag"), "Response should contain ETag");
    assert!(
        body.contains("ObjectSize"),
        "Response should contain ObjectSize"
    );
    assert!(
        body.contains("<ObjectSize>36</ObjectSize>"),
        "ObjectSize should be 36 bytes"
    );
    assert!(
        body.contains("StorageClass"),
        "Response should contain StorageClass"
    );
    assert!(body.contains("STANDARD"), "StorageClass should be STANDARD");

    // Test 2: Request specific attributes with header
    let response = http_client
        .get(format!("{}/attrs-test/test-file.txt?attributes", base_url))
        .header("x-amz-object-attributes", "ETag, ObjectSize")
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200);
    let body = response.text().await.unwrap();
    assert!(body.contains("ETag"), "Response should contain ETag");
    assert!(
        body.contains("ObjectSize"),
        "Response should contain ObjectSize"
    );

    // Test 3: Request Checksum attribute
    let response = http_client
        .get(format!("{}/attrs-test/test-file.txt?attributes", base_url))
        .header("x-amz-object-attributes", "Checksum")
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200);
    let body = response.text().await.unwrap();
    assert!(
        body.contains("Checksum"),
        "Response should contain Checksum"
    );
    assert!(
        body.contains("ChecksumSHA256"),
        "Response should contain ChecksumSHA256"
    );

    // Test 4: Non-existent object should return 404
    let response = http_client
        .get(format!(
            "{}/attrs-test/nonexistent.txt?attributes",
            base_url
        ))
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 404);
}

#[tokio::test]
async fn test_list_object_versions() {
    let (client, _temp_dir, server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("versions-test")
        .send()
        .await
        .unwrap();

    // Put some objects
    for i in 0..3 {
        client
            .put_object()
            .bucket("versions-test")
            .key(format!("file{}.txt", i))
            .body(format!("Content {}", i).into_bytes().into())
            .send()
            .await
            .unwrap();
    }

    // Use reqwest to call ListObjectVersions (GET ?versions)
    let http_client = reqwest::Client::new();
    let base_url = format!("http://{}", server.addr);

    // Test 1: List all versions
    let response = http_client
        .get(format!("{}/versions-test?versions", base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200);
    let body = response.text().await.unwrap();
    assert!(
        body.contains("ListVersionsResult"),
        "Response should be ListVersionsResult XML"
    );
    assert!(
        body.contains("<Name>versions-test</Name>"),
        "Response should contain bucket name"
    );
    // Each object should appear as a Version with version_id "null"
    assert!(
        body.contains("<VersionId>null</VersionId>"),
        "Should have version_id null"
    );
    assert!(
        body.contains("<IsLatest>true</IsLatest>"),
        "Should be marked as latest"
    );
    assert!(body.contains("file0.txt"), "Should contain file0.txt");
    assert!(body.contains("file1.txt"), "Should contain file1.txt");
    assert!(body.contains("file2.txt"), "Should contain file2.txt");

    // Test 2: List with prefix
    let response = http_client
        .get(format!("{}/versions-test?versions&prefix=file1", base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200);
    let body = response.text().await.unwrap();
    assert!(body.contains("file1.txt"), "Should contain file1.txt");
    assert!(!body.contains("file0.txt"), "Should NOT contain file0.txt");
    assert!(!body.contains("file2.txt"), "Should NOT contain file2.txt");

    // Test 3: Non-existent bucket should return 404
    let response = http_client
        .get(format!("{}/nonexistent-bucket?versions", base_url))
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 404);
}

#[tokio::test]
async fn test_post_object() {
    let (client, _temp_dir, server) = setup_test_server().await;

    // Create bucket
    client
        .create_bucket()
        .bucket("post-test")
        .send()
        .await
        .unwrap();

    let http_client = reqwest::Client::new();
    let base_url = format!("http://{}", server.addr);

    // Test 1: Basic POST object upload with multipart/form-data
    let form = reqwest::multipart::Form::new()
        .text("key", "uploaded/file.txt")
        .text("Content-Type", "text/plain")
        .part(
            "file",
            reqwest::multipart::Part::bytes(b"Hello from POST upload!".to_vec())
                .file_name("file.txt")
                .mime_str("text/plain")
                .unwrap(),
        );

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 204, "Basic POST should return 204");
    assert!(
        response.headers().contains_key("etag"),
        "Should have ETag header"
    );

    // Verify the object was created
    let get_result = client
        .get_object()
        .bucket("post-test")
        .key("uploaded/file.txt")
        .send()
        .await;
    assert!(
        get_result.is_ok(),
        "Should be able to get the uploaded object"
    );
    let body = get_result.unwrap().body.collect().await.unwrap();
    assert_eq!(body.into_bytes().as_ref(), b"Hello from POST upload!");

    // Test 2: POST with success_action_status=201 should return XML
    let form = reqwest::multipart::Form::new()
        .text("key", "xml-response.txt")
        .text("success_action_status", "201")
        .part(
            "file",
            reqwest::multipart::Part::bytes(b"Content for 201 response".to_vec())
                .file_name("test.txt"),
        );

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 201, "Should return 201 when requested");
    let body = response.text().await.unwrap();
    assert!(
        body.contains("<PostResponse>"),
        "Should return XML response"
    );
    assert!(
        body.contains("<Bucket>post-test</Bucket>"),
        "Should contain bucket name"
    );
    assert!(
        body.contains("<Key>xml-response.txt</Key>"),
        "Should contain key"
    );

    // Test 3: POST with success_action_status=200 should return 200
    let form = reqwest::multipart::Form::new()
        .text("key", "status-200.txt")
        .text("success_action_status", "200")
        .part(
            "file",
            reqwest::multipart::Part::bytes(b"Content for 200 response".to_vec())
                .file_name("test.txt"),
        );

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 200, "Should return 200 when requested");

    // Test 4: POST with custom metadata
    let form = reqwest::multipart::Form::new()
        .text("key", "with-metadata.txt")
        .text("x-amz-meta-custom-key", "custom-value")
        .part(
            "file",
            reqwest::multipart::Part::bytes(b"Content with metadata".to_vec())
                .file_name("test.txt"),
        );

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 204);

    // Verify metadata was stored
    let head_result = client
        .head_object()
        .bucket("post-test")
        .key("with-metadata.txt")
        .send()
        .await;
    assert!(head_result.is_ok());
    let metadata = head_result.unwrap().metadata().unwrap().clone();
    assert_eq!(
        metadata.get("custom-key"),
        Some(&"custom-value".to_string())
    );

    // Test 5: POST to non-existent bucket should return 404
    let form = reqwest::multipart::Form::new()
        .text("key", "test.txt")
        .part(
            "file",
            reqwest::multipart::Part::bytes(b"test".to_vec()).file_name("test.txt"),
        );

    let response = http_client
        .post(format!("{}/nonexistent-bucket", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 404);

    // Test 6: POST without key field should return 400
    let form = reqwest::multipart::Form::new().part(
        "file",
        reqwest::multipart::Part::bytes(b"test".to_vec()).file_name("test.txt"),
    );

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 400, "Missing key should return 400");

    // Test 7: POST without file field should return 400
    let form = reqwest::multipart::Form::new().text("key", "test.txt");

    let response = http_client
        .post(format!("{}/post-test", base_url))
        .multipart(form)
        .send()
        .await
        .unwrap();
    assert_eq!(response.status(), 400, "Missing file should return 400");
}

// ============================================================
// WS-7: Object API Test Matrix
// ============================================================

// --- Keys with spaces: PUT, GET, HEAD, DELETE (4 of 8) ---

/// PUT/GET/HEAD objects with space in key
#[tokio::test]
async fn test_space_key_put_get_head() {
    use aws_sdk_s3::types::{Delete, ObjectIdentifier};
    let (client, _temp_dir, _server) = setup_test_server().await;
    let bucket = "space-key-pgh-test";
    client
        .create_bucket()
        .bucket(bucket)
        .send()
        .await
        .expect("create bucket");

    // PUT
    client
        .put_object()
        .bucket(bucket)
        .key("foo bar")
        .body(b"hello space".to_vec().into())
        .send()
        .await
        .expect("PUT space key");

    // GET - body matches
    let body_bytes: &[u8] = b"hello space";
    let get = client
        .get_object()
        .bucket(bucket)
        .key("foo bar")
        .send()
        .await
        .expect("GET space key");
    let returned = get.body.collect().await.expect("collect");
    assert_eq!(returned.into_bytes().as_ref(), body_bytes);

    // HEAD - 200
    let head = client
        .head_object()
        .bucket(bucket)
        .key("foo bar")
        .send()
        .await
        .expect("HEAD space key");
    assert_eq!(head.content_length(), Some(11_i64));

    // DELETE - 204 (no error)
    client
        .delete_object()
        .bucket(bucket)
        .key("foo bar")
        .send()
        .await
        .expect("DELETE space key");
    assert!(
        client
            .get_object()
            .bucket(bucket)
            .key("foo bar")
            .send()
            .await
            .is_err(),
        "object should be deleted"
    );

    // Batch DELETE including space key
    for k in ["foo bar2", "normal-key"] {
        client
            .put_object()
            .bucket(bucket)
            .key(k)
            .body(b"x".to_vec().into())
            .send()
            .await
            .expect("put");
    }
    let oids = vec![
        ObjectIdentifier::builder()
            .key("foo bar2")
            .build()
            .expect("oid1"),
        ObjectIdentifier::builder()
            .key("normal-key")
            .build()
            .expect("oid2"),
    ];
    let del = Delete::builder()
        .set_objects(Some(oids))
        .build()
        .expect("delete");
    let res = client
        .delete_objects()
        .bucket(bucket)
        .delete(del)
        .send()
        .await
        .expect("batch delete");
    assert_eq!(res.deleted().len(), 2);
}

/// COPY from `foo bar` to `foo-bar-copy`; LIST V1 & V2 with prefix `foo`
#[tokio::test]
async fn test_space_key_copy_and_list() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    let bucket = "space-key-cl-test";
    client
        .create_bucket()
        .bucket(bucket)
        .send()
        .await
        .expect("create bucket");

    let content = b"copy with space";
    client
        .put_object()
        .bucket(bucket)
        .key("foo bar")
        .body(content.to_vec().into())
        .send()
        .await
        .expect("put source");
    for k in ["foo-other", "bar-key"] {
        client
            .put_object()
            .bucket(bucket)
            .key(k)
            .body(b"d".to_vec().into())
            .send()
            .await
            .expect("put extra");
    }

    // COPY - percent-encoded source
    client
        .copy_object()
        .bucket(bucket)
        .key("foo-bar-copy")
        .copy_source(format!("{}/foo%20bar", bucket))
        .send()
        .await
        .expect("COPY space key");
    let got = client
        .get_object()
        .bucket(bucket)
        .key("foo-bar-copy")
        .send()
        .await
        .expect("get copy");
    let body = got.body.collect().await.expect("collect").into_bytes();
    assert_eq!(body.as_ref(), content);

    // LIST V1 - includes `foo bar`
    #[allow(deprecated)]
    let v1 = client
        .list_objects()
        .bucket(bucket)
        .prefix("foo")
        .send()
        .await
        .expect("list v1");
    let keys_v1: Vec<&str> = v1.contents().iter().filter_map(|o| o.key()).collect();
    assert!(
        keys_v1.contains(&"foo bar"),
        "list v1 should contain 'foo bar': {:?}",
        keys_v1
    );

    // LIST V2 - includes `foo bar`
    let v2 = client
        .list_objects_v2()
        .bucket(bucket)
        .prefix("foo")
        .send()
        .await
        .expect("list v2");
    let keys_v2: Vec<&str> = v2.contents().iter().filter_map(|o| o.key()).collect();
    assert!(
        keys_v2.contains(&"foo bar"),
        "list v2 should contain 'foo bar': {:?}",
        keys_v2
    );
}

// --- Deep prefixes: PUT, GET, HEAD, DELETE, batch-DELETE, COPY, LIST V1, LIST V2 ---

/// Deep prefix key `a/b/c/d/key` — PUT/GET/HEAD/DELETE/batch-DELETE
#[tokio::test]
async fn test_deep_prefix_crud() {
    use aws_sdk_s3::types::{Delete, ObjectIdentifier};
    let (client, _temp_dir, _server) = setup_test_server().await;
    let bucket = "deep-prefix-crud-test";
    client
        .create_bucket()
        .bucket(bucket)
        .send()
        .await
        .expect("create bucket");
    let key = "a/b/c/d/key";
    let data: &[u8] = b"deep data!";

    // PUT
    client
        .put_object()
        .bucket(bucket)
        .key(key)
        .body(data.to_vec().into())
        .send()
        .await
        .expect("PUT deep");

    // GET - body matches
    let got = client
        .get_object()
        .bucket(bucket)
        .key(key)
        .send()
        .await
        .expect("GET deep");
    let returned = got.body.collect().await.expect("collect").into_bytes();
    assert_eq!(returned.as_ref(), data);

    // HEAD
    let head = client
        .head_object()
        .bucket(bucket)
        .key(key)
        .send()
        .await
        .expect("HEAD deep");
    assert_eq!(head.content_length(), Some(10_i64));

    // DELETE
    client
        .delete_object()
        .bucket(bucket)
        .key(key)
        .send()
        .await
        .expect("DELETE deep");
    assert!(
        client
            .get_object()
            .bucket(bucket)
            .key(key)
            .send()
            .await
            .is_err(),
        "deep key should be deleted"
    );

    // batch DELETE
    for k in ["a/b/c/d/key2", "a/b/c/d/key3"] {
        client
            .put_object()
            .bucket(bucket)
            .key(k)
            .body(b"y".to_vec().into())
            .send()
            .await
            .expect("put");
    }
    let oids = vec![
        ObjectIdentifier::builder()
            .key("a/b/c/d/key2")
            .build()
            .expect("oid1"),
        ObjectIdentifier::builder()
            .key("a/b/c/d/key3")
            .build()
            .expect("oid2"),
    ];
    let del = Delete::builder()
        .set_objects(Some(oids))
        .build()
        .expect("delete");
    let res = client
        .delete_objects()
        .bucket(bucket)
        .delete(del)
        .send()
        .await
        .expect("batch delete");
    assert_eq!(res.deleted().len(), 2);
}

/// Deep prefix COPY and LIST V1 / V2
#[tokio::test]
async fn test_deep_prefix_copy_and_list() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    let bucket = "deep-prefix-cl-test";
    client
        .create_bucket()
        .bucket(bucket)
        .send()
        .await
        .expect("create bucket");

    let content = b"deep copy src";
    client
        .put_object()
        .bucket(bucket)
        .key("a/b/c/d/key")
        .body(content.to_vec().into())
        .send()
        .await
        .expect("put source");
    client
        .put_object()
        .bucket(bucket)
        .key("a/b/c/d/other")
        .body(b"z".to_vec().into())
        .send()
        .await
        .expect("put other");
    client
        .put_object()
        .bucket(bucket)
        .key("top-level")
        .body(b"z".to_vec().into())
        .send()
        .await
        .expect("put top");

    // COPY
    client
        .copy_object()
        .bucket(bucket)
        .key("a/b/c/d/key-copy")
        .copy_source(format!("{}/a/b/c/d/key", bucket))
        .send()
        .await
        .expect("COPY deep");
    let got = client
        .get_object()
        .bucket(bucket)
        .key("a/b/c/d/key-copy")
        .send()
        .await
        .expect("get copy");
    let body = got.body.collect().await.expect("collect").into_bytes();
    assert_eq!(body.as_ref(), content);

    // LIST V1
    #[allow(deprecated)]
    let v1 = client
        .list_objects()
        .bucket(bucket)
        .prefix("a/b/c/d/")
        .send()
        .await
        .expect("list v1");
    let keys_v1: Vec<&str> = v1.contents().iter().filter_map(|o| o.key()).collect();
    assert!(
        keys_v1.contains(&"a/b/c/d/key"),
        "v1 should include deep key: {:?}",
        keys_v1
    );
    assert_eq!(
        keys_v1.len(),
        3,
        "should have key, other, key-copy: {:?}",
        keys_v1
    );

    // LIST V2
    let v2 = client
        .list_objects_v2()
        .bucket(bucket)
        .prefix("a/b/c/d/")
        .send()
        .await
        .expect("list v2");
    let keys_v2: Vec<&str> = v2.contents().iter().filter_map(|o| o.key()).collect();
    assert!(
        keys_v2.contains(&"a/b/c/d/key"),
        "v2 should include deep key: {:?}",
        keys_v2
    );
    assert_eq!(
        keys_v2.len(),
        3,
        "should have key, other, key-copy: {:?}",
        keys_v2
    );
}

// --- Large object streaming (4 tests) ---

/// PUT 64MB synthetic body — 200, ETag returned
#[tokio::test]
async fn test_large_object_put() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    client
        .create_bucket()
        .bucket("large-obj-put-test")
        .send()
        .await
        .expect("create bucket");
    let data = vec![0u8; 64 * 1024 * 1024];
    let res = client
        .put_object()
        .bucket("large-obj-put-test")
        .key("large.bin")
        .body(data.into())
        .send()
        .await
        .expect("PUT 64MB");
    assert!(
        res.e_tag().is_some(),
        "PUT should return ETag for 64MB object"
    );
}

/// GET back 64MB object — size matches
#[tokio::test]
async fn test_large_object_get() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    client
        .create_bucket()
        .bucket("large-obj-get-test")
        .send()
        .await
        .expect("create bucket");
    let size = 64 * 1024 * 1024usize;
    client
        .put_object()
        .bucket("large-obj-get-test")
        .key("large.bin")
        .body(vec![0xABu8; size].into())
        .send()
        .await
        .expect("PUT 64MB");
    let get = client
        .get_object()
        .bucket("large-obj-get-test")
        .key("large.bin")
        .send()
        .await
        .expect("GET 64MB");
    let returned = get.body.collect().await.expect("collect").into_bytes();
    assert_eq!(returned.len(), size, "GET should return exactly 64MB");
}

/// HEAD 64MB object — Content-Length: 67108864
#[tokio::test]
async fn test_large_object_head() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    client
        .create_bucket()
        .bucket("large-obj-head-test")
        .send()
        .await
        .expect("create bucket");
    client
        .put_object()
        .bucket("large-obj-head-test")
        .key("large.bin")
        .body(vec![0u8; 64 * 1024 * 1024].into())
        .send()
        .await
        .expect("PUT 64MB");
    let head = client
        .head_object()
        .bucket("large-obj-head-test")
        .key("large.bin")
        .send()
        .await
        .expect("HEAD 64MB");
    assert_eq!(head.content_length(), Some(67_108_864_i64));
}

/// Range GET bytes=0-1023 of 64MB object — 206, 1024 bytes
#[tokio::test]
async fn test_large_object_range_get() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    client
        .create_bucket()
        .bucket("large-obj-range-test")
        .send()
        .await
        .expect("create bucket");
    client
        .put_object()
        .bucket("large-obj-range-test")
        .key("large.bin")
        .body(vec![0x42u8; 64 * 1024 * 1024].into())
        .send()
        .await
        .expect("PUT 64MB");
    let get = client
        .get_object()
        .bucket("large-obj-range-test")
        .key("large.bin")
        .range("bytes=0-1023")
        .send()
        .await
        .expect("range GET");
    let bytes = get.body.collect().await.expect("collect").into_bytes();
    assert_eq!(
        bytes.len(),
        1024,
        "range GET bytes=0-1023 should return 1024 bytes"
    );
    assert!(
        bytes.iter().all(|&b| b == 0x42),
        "range data should match fill byte"
    );
}

// --- Request IDs (6 tests) ---

/// PUT, GET, HEAD, DELETE, ListObjects return x-amz-request-id; 404 also returns it
#[tokio::test]
async fn test_request_ids() {
    let (client, _temp_dir, server) = setup_test_server().await;
    let http = reqwest::Client::new();
    let base = &server.base_url;
    let bucket = "reqid-test";

    client
        .create_bucket()
        .bucket(bucket)
        .send()
        .await
        .expect("create bucket");
    client
        .put_object()
        .bucket(bucket)
        .key("f.txt")
        .body(b"d".to_vec().into())
        .send()
        .await
        .expect("put");

    // PUT
    let resp = http
        .put(format!("{}/{}/f2.txt", base, bucket))
        .body(b"d".to_vec())
        .send()
        .await
        .expect("PUT");
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "PUT missing request-id"
    );

    // GET
    let resp = http
        .get(format!("{}/{}/f.txt", base, bucket))
        .send()
        .await
        .expect("GET");
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "GET missing request-id"
    );

    // HEAD
    let resp = http
        .head(format!("{}/{}/f.txt", base, bucket))
        .send()
        .await
        .expect("HEAD");
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "HEAD missing request-id"
    );

    // DELETE
    let resp = http
        .delete(format!("{}/{}/f.txt", base, bucket))
        .send()
        .await
        .expect("DELETE");
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "DELETE missing request-id"
    );

    // ListObjects
    let resp = http
        .get(format!("{}/{}?list-type=2", base, bucket))
        .send()
        .await
        .expect("list");
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "ListObjects missing request-id"
    );

    // 404 error
    let resp = http
        .get(format!("{}/no-such-bucket-reqid/no-key", base))
        .send()
        .await
        .expect("404");
    assert_eq!(resp.status(), 404);
    assert!(
        resp.headers().contains_key("x-amz-request-id"),
        "404 missing request-id"
    );
}

// --- URL encoding (4 tests) ---

/// PUT `foo+bar`, GET `foo+bar` — roundtrip with plus sign
#[tokio::test]
async fn test_url_encoding_plus_key() {
    let (client, _temp_dir, _server) = setup_test_server().await;
    client
        .create_bucket()
        .bucket("url-plus-test")
        .send()
        .await
        .expect("create bucket");
    let data: &[u8] = b"plus body";
    client
        .put_object()
        .bucket("url-plus-test")
        .key("foo+bar")
        .body(data.to_vec().into())
        .send()
        .await
        .expect("PUT foo+bar");
    let got = client
        .get_object()
        .bucket("url-plus-test")
        .key("foo+bar")
        .send()
        .await
        .expect("GET foo+bar");
    let returned = got.body.collect().await.expect("collect").into_bytes();
    assert_eq!(returned.as_ref(), data);
}

/// Key `foo bar` stored, retrieved via raw `foo%20bar` URL
#[tokio::test]
async fn test_url_encoding_percent20_get() {
    let (client, _temp_dir, server) = setup_test_server().await;
    let http = reqwest::Client::new();
    client
        .create_bucket()
        .bucket("url-pct-test")
        .send()
        .await
        .expect("create bucket");
    let data: &[u8] = b"pct-encoded";
    client
        .put_object()
        .bucket("url-pct-test")
        .key("foo bar")
        .body(data.to_vec().into())
        .send()
        .await
        .expect("put");
    let resp = http
        .get(format!("{}/url-pct-test/foo%20bar", server.base_url))
        .send()
        .await
        .expect("GET %20");
    assert_eq!(resp.status(), 200, "GET with %20 should succeed");
    assert_eq!(resp.bytes().await.expect("bytes").as_ref(), data);
}

/// ListObjects XML result contains the key with special chars
#[tokio::test]
async fn test_url_encoding_list_contains_special_key() {
    let (client, _temp_dir, server) = setup_test_server().await;
    let http = reqwest::Client::new();
    client
        .create_bucket()
        .bucket("url-list-enc-test")
        .send()
        .await
        .expect("create bucket");
    client
        .put_object()
        .bucket("url-list-enc-test")
        .key("foo bar")
        .body(b"x".to_vec().into())
        .send()
        .await
        .expect("put");
    let resp = http
        .get(format!("{}/url-list-enc-test?list-type=2", server.base_url))
        .send()
        .await
        .expect("list");
    assert_eq!(resp.status(), 200);
    let body = resp.text().await.expect("text");
    assert!(
        body.contains("foo") && body.contains("bar"),
        "list XML should reference space-key, got: {}",
        &body[..body.len().min(500)]
    );
}