rust-s3 0.38.0

Rust library for working with AWS S3 and compatible object storage APIs
Documentation
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
extern crate base64;
extern crate md5;

use bytes::Bytes;
use futures_util::TryStreamExt;
use maybe_async::maybe_async;
use std::collections::HashMap;
use std::str::FromStr as _;
use time::OffsetDateTime;

use super::request_trait::{Request, ResponseData, ResponseDataStream};
use crate::bucket::Bucket;
use crate::command::Command;
use crate::command::HttpMethod;
use crate::error::S3Error;
use crate::utils::now_utc;

use tokio_stream::StreamExt;

#[derive(Clone, Debug, Default)]
pub(crate) struct ClientOptions {
    pub proxy: Option<reqwest::Proxy>,
    #[cfg(any(
        feature = "tokio-native-tls",
        feature = "tokio-rustls-tls",
        feature = "tokio-rustls-tls-ring"
    ))]
    pub accept_invalid_certs: bool,
    #[cfg(any(
        feature = "tokio-native-tls",
        feature = "tokio-rustls-tls",
        feature = "tokio-rustls-tls-ring"
    ))]
    pub accept_invalid_hostnames: bool,
}

#[cfg(feature = "with-tokio")]
pub(crate) fn client(options: &ClientOptions) -> Result<reqwest::Client, S3Error> {
    // Request timeouts are applied to individual requests so changing a
    // Bucket's timeout does not require rebuilding this client (or lose its
    // proxy/TLS configuration).
    let client = reqwest::Client::builder();

    let client = if let Some(ref proxy) = options.proxy {
        client.proxy(proxy.clone())
    } else {
        client
    };

    cfg_if::cfg_if! {
        if #[cfg(any(
            feature = "tokio-native-tls",
            feature = "tokio-rustls-tls",
            feature = "tokio-rustls-tls-ring"
        ))] {
            let client = client.danger_accept_invalid_certs(options.accept_invalid_certs);
        }
    }

    cfg_if::cfg_if! {
        if #[cfg(any(
            feature = "tokio-native-tls",
            feature = "tokio-rustls-tls",
            feature = "tokio-rustls-tls-ring"
        ))] {
            let client = client.danger_accept_invalid_hostnames(options.accept_invalid_hostnames);
        }
    }

    Ok(client.build()?)
}
// Temporary structure for making a request
pub struct ReqwestRequest<'a> {
    pub bucket: &'a Bucket,
    pub path: &'a str,
    pub command: Command<'a>,
    pub datetime: OffsetDateTime,
    pub sync: bool,
}

async fn request_response_attempt(
    request: &ReqwestRequest<'_>,
) -> Result<reqwest::Response, crate::request::RequestAttemptError> {
    let headers = request
        .headers()
        .await?
        .iter()
        .map(|(k, v)| {
            (
                reqwest::header::HeaderName::from_str(k.as_str()),
                reqwest::header::HeaderValue::from_str(v.to_str().unwrap_or_default()),
            )
        })
        .filter(|(k, v)| k.is_ok() && v.is_ok())
        .map(|(k, v)| (k.unwrap(), v.unwrap()))
        .collect();

    let client = request.bucket.http_client();
    let method = match request.command.http_verb() {
        HttpMethod::Delete => reqwest::Method::DELETE,
        HttpMethod::Get => reqwest::Method::GET,
        HttpMethod::Post => reqwest::Method::POST,
        HttpMethod::Put => reqwest::Method::PUT,
        HttpMethod::Head => reqwest::Method::HEAD,
    };

    let request_builder = client
        .request(method, request.url()?.as_str())
        .headers(headers)
        .body(request.request_body()?);
    let request_builder = if let Some(timeout) = request.bucket.request_timeout {
        request_builder.timeout(timeout)
    } else {
        request_builder
    };
    let response = client.execute(request_builder.build()?).await?;

    if cfg!(feature = "fail-on-err") && !response.status().is_success() {
        let status = response.status().as_u16();
        let text = response.text().await.map_err(|error| {
            crate::request::RequestAttemptError::do_not_retry(S3Error::Reqwest(error))
        })?;
        return Err(S3Error::HttpFailWithBody(status, text).into());
    }

    Ok(response)
}

#[maybe_async]
impl<'a> Request for ReqwestRequest<'a> {
    type Response = reqwest::Response;
    type HeaderMap = reqwest::header::HeaderMap;

    async fn response(&self) -> Result<Self::Response, S3Error> {
        request_response_attempt(self)
            .await
            .map_err(|error| error.error)
    }

    async fn response_status(&self) -> Result<u16, S3Error> {
        crate::request::retry_request(&self.command, || async {
            let headers = self
                .headers()
                .await?
                .iter()
                .map(|(k, v)| {
                    (
                        reqwest::header::HeaderName::from_str(k.as_str()),
                        reqwest::header::HeaderValue::from_str(v.to_str().unwrap_or_default()),
                    )
                })
                .filter(|(k, v)| k.is_ok() && v.is_ok())
                .map(|(k, v)| (k.unwrap(), v.unwrap()))
                .collect();

            let client = self.bucket.http_client();

            let method = match self.command.http_verb() {
                HttpMethod::Delete => reqwest::Method::DELETE,
                HttpMethod::Get => reqwest::Method::GET,
                HttpMethod::Post => reqwest::Method::POST,
                HttpMethod::Put => reqwest::Method::PUT,
                HttpMethod::Head => reqwest::Method::HEAD,
            };

            let request = client
                .request(method, self.url()?.as_str())
                .headers(headers)
                .body(self.request_body()?);

            let request = if let Some(timeout) = self.bucket.request_timeout {
                request.timeout(timeout)
            } else {
                request
            };

            let request = request.build()?;
            let response = client.execute(request).await.map_err(S3Error::from)?;
            let status = response.status().as_u16();

            if status == 404 {
                return Ok(status);
            }

            if cfg!(feature = "fail-on-err") && !response.status().is_success() {
                let text = response.text().await.map_err(|error| {
                    crate::request::RequestAttemptError::do_not_retry(S3Error::Reqwest(error))
                })?;
                return Err(S3Error::HttpFailWithBody(status, text).into());
            }

            Ok(status)
        })
        .await
    }

    async fn response_data(&self, etag: bool) -> Result<ResponseData, S3Error> {
        let response =
            crate::request::retry_request(&self.command, || request_response_attempt(self)).await?;
        let status_code = response.status().as_u16();
        let mut headers = response.headers().clone();
        let response_headers = headers
            .clone()
            .iter()
            .map(|(k, v)| {
                (
                    k.to_string(),
                    v.to_str()
                        .unwrap_or("could-not-decode-header-value")
                        .to_string(),
                )
            })
            .collect::<HashMap<String, String>>();
        // When etag=true, we extract the ETag header and return it as the body.
        // This is used for PUT operations (regular puts, multipart chunks) where:
        // 1. S3 returns an empty or non-useful response body
        // 2. The ETag header contains the essential information we need
        // 3. The calling code expects to get the ETag via response_data.as_str()
        //
        // Note: This approach means we discard any actual response body when etag=true,
        // but for the operations that use this (PUTs), the body is typically empty
        // or contains redundant information already available in headers.
        //
        // TODO: Refactor this to properly return the response body and access ETag
        // from headers instead of replacing the body. This would be a breaking change.
        let body_vec = if etag {
            if let Some(etag) = headers.remove("ETag") {
                Bytes::from(etag.to_str()?.to_string())
            } else {
                Bytes::from("")
            }
        } else {
            response.bytes().await?
        };
        Ok(ResponseData::new(body_vec, status_code, response_headers))
    }

    async fn response_data_to_writer<T: tokio::io::AsyncWrite + Send + Unpin + ?Sized>(
        &self,
        writer: &mut T,
    ) -> Result<u16, S3Error> {
        use tokio::io::AsyncWriteExt;
        let response =
            crate::request::retry_request(&self.command, || request_response_attempt(self)).await?;

        let status_code = response.status();
        let mut stream = response.bytes_stream();

        while let Some(item) = stream.next().await {
            writer.write_all(&item?).await?;
        }

        Ok(status_code.as_u16())
    }

    async fn response_data_to_stream(&self) -> Result<ResponseDataStream, S3Error> {
        let response =
            crate::request::retry_request(&self.command, || request_response_attempt(self)).await?;
        let status_code = response.status();
        let stream = response.bytes_stream().map_err(S3Error::Reqwest);

        Ok(ResponseDataStream {
            bytes: Box::pin(stream),
            status_code: status_code.as_u16(),
        })
    }

    async fn response_header(&self) -> Result<(Self::HeaderMap, u16), S3Error> {
        let response =
            crate::request::retry_request(&self.command, || request_response_attempt(self)).await?;
        let status_code = response.status().as_u16();
        let headers = response.headers().clone();
        Ok((headers, status_code))
    }

    fn datetime(&self) -> OffsetDateTime {
        self.datetime
    }

    fn bucket(&self) -> Bucket {
        self.bucket.clone()
    }

    fn command(&self) -> Command<'_> {
        self.command.clone()
    }

    fn path(&self) -> String {
        self.path.to_string()
    }
}

impl<'a> ReqwestRequest<'a> {
    pub async fn new(
        bucket: &'a Bucket,
        path: &'a str,
        command: Command<'a>,
    ) -> Result<ReqwestRequest<'a>, S3Error> {
        bucket.credentials_refresh().await?;
        Ok(Self {
            bucket,
            path,
            command,
            datetime: now_utc(),
            sync: false,
        })
    }
}

#[cfg(test)]
mod tests {
    use crate::bucket::Bucket;
    use crate::command::Command;
    use crate::request::Request;
    use crate::request::tokio_backend::ReqwestRequest;
    use awscreds::Credentials;
    use http::header::{HOST, RANGE};
    use std::io::{Read, Write};
    use std::net::TcpListener;
    use std::sync::mpsc;
    use std::thread;
    use std::time::Duration;
    use tokio_stream::StreamExt;

    // Fake keys - otherwise using Credentials::default will use actual user
    // credentials if they exist.
    fn fake_credentials() -> Credentials {
        let access_key = "AKIAIOSFODNN7EXAMPLE";
        let secert_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
        Credentials::new(Some(access_key), Some(secert_key), None, None, None).unwrap()
    }

    fn timeout_test_bucket(endpoint: String, timeout: Option<Duration>) -> Bucket {
        let mut bucket = *Bucket::new(
            "timeout-test",
            crate::region::Region::Custom {
                region: "us-east-1".to_owned(),
                endpoint,
            },
            fake_credentials(),
        )
        .unwrap()
        .with_path_style();
        bucket.set_request_timeout(timeout);
        bucket
    }

    fn delayed_http_response(
        response: &'static [u8],
        delay: Duration,
    ) -> (String, mpsc::Sender<()>, thread::JoinHandle<()>) {
        mock_http_response(response, None, delay)
    }

    fn stalled_http_body(
        initial_response: &'static [u8],
        trailing_body: &'static [u8],
        delay: Duration,
    ) -> (String, mpsc::Sender<()>, thread::JoinHandle<()>) {
        mock_http_response(initial_response, Some(trailing_body), delay)
    }

    fn mock_http_response(
        initial_response: &'static [u8],
        trailing_body: Option<&'static [u8]>,
        delay: Duration,
    ) -> (String, mpsc::Sender<()>, thread::JoinHandle<()>) {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let endpoint = format!("http://{}", listener.local_addr().unwrap());
        let (stop_tx, stop_rx) = mpsc::channel();
        let server = thread::spawn(move || {
            let accept_deadline = std::time::Instant::now() + Duration::from_secs(8);
            let (mut stream, _) = loop {
                if !matches!(stop_rx.try_recv(), Err(mpsc::TryRecvError::Empty)) {
                    return;
                }
                match listener.accept() {
                    Ok(connection) => break connection,
                    Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                        if std::time::Instant::now() >= accept_deadline {
                            return;
                        }
                        thread::sleep(Duration::from_millis(5));
                    }
                    Err(_) => return,
                }
            };
            stream.set_nonblocking(false).unwrap();
            stream
                .set_read_timeout(Some(Duration::from_secs(2)))
                .unwrap();
            stream
                .set_write_timeout(Some(Duration::from_secs(2)))
                .unwrap();
            let mut request = Vec::new();
            let mut byte = [0u8; 1];
            while !request.ends_with(b"\r\n\r\n") {
                if stream.read(&mut byte).unwrap_or(0) == 0 {
                    return;
                }
                request.push(byte[0]);
                assert!(
                    request.len() < 16 * 1024,
                    "request header exceeded fixture bound"
                );
            }
            if trailing_body.is_none()
                && !matches!(
                    stop_rx.recv_timeout(delay),
                    Err(mpsc::RecvTimeoutError::Timeout)
                )
            {
                return;
            }
            if stream.write_all(initial_response).is_err() {
                return;
            }
            if let Some(trailing_body) = trailing_body {
                if !matches!(
                    stop_rx.recv_timeout(delay),
                    Err(mpsc::RecvTimeoutError::Timeout)
                ) {
                    return;
                }
                let _ = stream.write_all(trailing_body);
            }
            let _ = stream.flush();
        });
        (endpoint, stop_tx, server)
    }

    fn timeout_then_success_status_server() -> (String, mpsc::Sender<()>, thread::JoinHandle<usize>)
    {
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        listener.set_nonblocking(true).unwrap();
        let endpoint = format!("http://{}", listener.local_addr().unwrap());
        let (stop_tx, stop_rx) = mpsc::channel();
        let server = thread::spawn(move || {
            let mut attempts = 0;
            let deadline = std::time::Instant::now() + Duration::from_secs(8);
            let Some(mut first) = accept_mock_request(&listener, &stop_rx, deadline) else {
                return attempts;
            };
            attempts += 1;
            if !read_mock_request(&mut first) {
                return attempts;
            }
            let response = b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
            let first_response_deadline = std::time::Instant::now() + Duration::from_secs(3);
            loop {
                if !matches!(stop_rx.try_recv(), Err(mpsc::TryRecvError::Empty)) {
                    return attempts;
                }
                match listener.accept() {
                    Ok((mut second, _)) => {
                        let _ = second.set_nonblocking(false);
                        let _ = second.set_read_timeout(Some(Duration::from_secs(2)));
                        let _ = second.set_write_timeout(Some(Duration::from_secs(2)));
                        attempts += 1;
                        if read_mock_request(&mut second) {
                            let _ = second.write_all(response);
                            let _ = second.flush();
                        }
                        return attempts;
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                        if std::time::Instant::now() >= first_response_deadline {
                            let _ = first.write_all(response);
                            let _ = first.flush();
                            return attempts;
                        }
                        thread::sleep(Duration::from_millis(5));
                    }
                    Err(_) => return attempts,
                }
            }
        });
        (endpoint, stop_tx, server)
    }

    fn accept_mock_request(
        listener: &TcpListener,
        stop: &mpsc::Receiver<()>,
        deadline: std::time::Instant,
    ) -> Option<std::net::TcpStream> {
        loop {
            if !matches!(stop.try_recv(), Err(mpsc::TryRecvError::Empty))
                || std::time::Instant::now() >= deadline
            {
                return None;
            }
            match listener.accept() {
                Ok((stream, _)) => {
                    stream.set_nonblocking(false).ok()?;
                    let _ = stream.set_read_timeout(Some(Duration::from_secs(2)));
                    let _ = stream.set_write_timeout(Some(Duration::from_secs(2)));
                    return Some(stream);
                }
                Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
                    thread::sleep(Duration::from_millis(5));
                }
                Err(_) => return None,
            }
        }
    }

    fn read_mock_request(stream: &mut std::net::TcpStream) -> bool {
        let mut request = Vec::new();
        let mut byte = [0u8; 1];
        while !request.ends_with(b"\r\n\r\n") {
            match stream.read(&mut byte) {
                Ok(0) | Err(_) => return false,
                Ok(_) => request.push(byte[0]),
            }
            if request.len() >= 16 * 1024 {
                return false;
            }
        }
        true
    }

    #[tokio::test]
    async fn request_timeout_covers_delayed_response_headers() {
        let (endpoint, stop, server) = delayed_http_response(
            b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
            Duration::from_millis(1500),
        );
        let bucket = timeout_test_bucket(endpoint, Some(Duration::from_millis(500)));
        let request = ReqwestRequest::new(&bucket, "/object", Command::GetObject)
            .await
            .unwrap();

        let result = tokio::time::timeout(Duration::from_secs(5), request.response()).await;
        let _ = stop.send(());
        server.join().unwrap();
        let result = result.expect("request should finish within the fixture bound");
        assert!(
            matches!(&result, Err(crate::error::S3Error::Reqwest(error)) if error.is_timeout()),
            "delayed headers should exceed the request timeout"
        );
    }

    #[tokio::test]
    async fn request_timeout_covers_body_stream_and_writer_consumption() {
        let (endpoint, stop, server) = stalled_http_body(
            b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\nabc",
            b"def",
            Duration::from_millis(1500),
        );
        let bucket = timeout_test_bucket(endpoint, Some(Duration::from_millis(500)));
        let mut body_stream = bucket.get_object_stream("/object").await.unwrap();
        let mut prefix = Vec::new();
        while prefix.len() < 3 {
            let chunk = tokio::time::timeout(Duration::from_secs(1), body_stream.bytes.next())
                .await
                .expect("first body bytes should arrive promptly")
                .expect("response body should contain its initial bytes")
                .unwrap();
            prefix.extend_from_slice(&chunk);
        }
        assert_eq!(&prefix[..3], b"abc");
        let next = tokio::time::timeout(Duration::from_secs(2), body_stream.bytes.next())
            .await
            .expect("body timeout should be bounded");
        assert!(matches!(
            next.expect("body should report its timeout"),
            Err(crate::error::S3Error::Reqwest(ref error)) if error.is_timeout()
        ));
        let _ = stop.send(());
        server.join().unwrap();

        let (endpoint, stop, server) = stalled_http_body(
            b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\nabc",
            b"def",
            Duration::from_millis(1500),
        );
        let bucket = timeout_test_bucket(endpoint, Some(Duration::from_millis(500)));
        let mut writer = Vec::new();
        let result = tokio::time::timeout(
            Duration::from_secs(2),
            bucket.get_object_to_writer("/object", &mut writer),
        )
        .await;
        let _ = stop.send(());
        server.join().unwrap();
        let result = result.expect("writer timeout should be bounded");
        assert!(matches!(
            result,
            Err(crate::error::S3Error::Reqwest(ref error)) if error.is_timeout()
        ));
        assert_eq!(writer, b"abc", "the complete prefix should be written once");
    }

    #[tokio::test]
    async fn request_timeout_can_be_removed_after_bucket_creation() {
        let (endpoint, stop, server) = delayed_http_response(
            b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n",
            Duration::from_millis(1500),
        );
        let mut bucket = *timeout_test_bucket(endpoint, None)
            .with_request_timeout(Duration::from_millis(500))
            .unwrap();
        bucket.set_request_timeout(None);
        let request = ReqwestRequest::new(&bucket, "/object", Command::GetObject)
            .await
            .unwrap();

        let response = tokio::time::timeout(Duration::from_secs(5), request.response()).await;
        let _ = stop.send(());
        server.join().unwrap();
        let response = response
            .expect("request should finish within the fixture bound")
            .expect("None should remove the prior per-request timeout");
        assert_eq!(response.status(), reqwest::StatusCode::OK);
    }

    #[tokio::test]
    async fn response_status_applies_timeout_and_retries_the_request() {
        let (endpoint, stop, server) = timeout_then_success_status_server();
        let bucket = timeout_test_bucket(endpoint, Some(Duration::from_millis(500)));
        let request = ReqwestRequest::new(&bucket, "/object", Command::GetObject)
            .await
            .unwrap();

        let status = tokio::time::timeout(Duration::from_secs(5), request.response_status()).await;
        let _ = stop.send(());
        let attempts = server.join().unwrap();
        let status = status
            .expect("status retry should remain bounded")
            .expect("the second response should succeed");
        assert_eq!(status, 200);
        assert_eq!(attempts, 2);
    }

    #[tokio::test]
    async fn url_uses_https_by_default() {
        let region = "custom-region".parse().unwrap();
        let bucket = Bucket::new("my-first-bucket", region, fake_credentials()).unwrap();
        let path = "/my-first/path";
        let request = ReqwestRequest::new(&bucket, path, Command::GetObject)
            .await
            .unwrap();

        assert_eq!(request.url().unwrap().scheme(), "https");

        let headers = request.headers().await.unwrap();
        let host = headers.get(HOST).unwrap();

        assert_eq!(*host, "my-first-bucket.custom-region".to_string());
    }

    #[tokio::test]
    async fn url_uses_https_by_default_path_style() {
        let region = "custom-region".parse().unwrap();
        let bucket = Bucket::new("my-first-bucket", region, fake_credentials())
            .unwrap()
            .with_path_style();
        let path = "/my-first/path";
        let request = ReqwestRequest::new(&bucket, path, Command::GetObject)
            .await
            .unwrap();

        assert_eq!(request.url().unwrap().scheme(), "https");

        let headers = request.headers().await.unwrap();
        let host = headers.get(HOST).unwrap();

        assert_eq!(*host, "custom-region".to_string());
    }

    #[tokio::test]
    async fn url_uses_scheme_from_custom_region_if_defined() {
        let region = "http://custom-region".parse().unwrap();
        let bucket = Bucket::new("my-second-bucket", region, fake_credentials()).unwrap();
        let path = "/my-second/path";
        let request = ReqwestRequest::new(&bucket, path, Command::GetObject)
            .await
            .unwrap();

        assert_eq!(request.url().unwrap().scheme(), "http");

        let headers = request.headers().await.unwrap();
        let host = headers.get(HOST).unwrap();
        assert_eq!(*host, "my-second-bucket.custom-region".to_string());
    }

    #[tokio::test]
    async fn url_uses_scheme_from_custom_region_if_defined_with_path_style() {
        let region = "http://custom-region".parse().unwrap();
        let bucket = Bucket::new("my-second-bucket", region, fake_credentials())
            .unwrap()
            .with_path_style();
        let path = "/my-second/path";
        let request = ReqwestRequest::new(&bucket, path, Command::GetObject)
            .await
            .unwrap();

        assert_eq!(request.url().unwrap().scheme(), "http");

        let headers = request.headers().await.unwrap();
        let host = headers.get(HOST).unwrap();
        assert_eq!(*host, "custom-region".to_string());
    }

    #[tokio::test]
    async fn test_get_object_range_header() {
        let region = "http://custom-region".parse().unwrap();
        let bucket = Bucket::new("my-second-bucket", region, fake_credentials())
            .unwrap()
            .with_path_style();
        let path = "/my-second/path";

        let request = ReqwestRequest::new(
            &bucket,
            path,
            Command::GetObjectRange {
                start: 0,
                end: None,
            },
        )
        .await
        .unwrap();
        let headers = request.headers().await.unwrap();
        let range = headers.get(RANGE).unwrap();
        assert_eq!(range, "bytes=0-");
        assert!(!headers.contains_key("Content-Length"));
        assert!(!headers.contains_key("Content-Type"));
        assert_eq!(headers.get("Accept").unwrap(), "application/octet-stream");
        let authorization = headers.get("Authorization").unwrap().to_str().unwrap();
        let signed_headers = authorization
            .split("SignedHeaders=")
            .nth(1)
            .and_then(|value| value.split(',').next())
            .unwrap();
        assert!(signed_headers.contains("range"));
        assert!(!signed_headers.contains("content-length"));
        assert!(!signed_headers.contains("content-type"));

        let request = ReqwestRequest::new(
            &bucket,
            path,
            Command::GetObjectRange {
                start: 0,
                end: Some(1),
            },
        )
        .await
        .unwrap();
        let headers = request.headers().await.unwrap();
        let range = headers.get(RANGE).unwrap();
        assert_eq!(range, "bytes=0-1");
        assert!(!headers.contains_key("Content-Length"));
        assert!(!headers.contains_key("Content-Type"));
        let authorization = headers.get("Authorization").unwrap().to_str().unwrap();
        let signed_headers = authorization
            .split("SignedHeaders=")
            .nth(1)
            .and_then(|value| value.split(',').next())
            .unwrap();
        assert!(signed_headers.contains("range"));
        assert!(!signed_headers.contains("content-length"));
        assert!(!signed_headers.contains("content-type"));

        let get = ReqwestRequest::new(&bucket, path, Command::GetObject)
            .await
            .unwrap();
        let headers = get.headers().await.unwrap();
        assert!(!headers.contains_key("Content-Length"));
        assert!(!headers.contains_key("Content-Type"));

        let put = ReqwestRequest::new(
            &bucket,
            path,
            Command::PutObject {
                content: b"abc",
                content_type: "application/test",
                custom_headers: None,
                multipart: None,
            },
        )
        .await
        .unwrap();
        let headers = put.headers().await.unwrap();
        assert_eq!(headers.get("Content-Length").unwrap(), "3");
        assert_eq!(headers.get("Content-Type").unwrap(), "application/test");
        let authorization = headers.get("Authorization").unwrap().to_str().unwrap();
        let signed_headers = authorization
            .split("SignedHeaders=")
            .nth(1)
            .and_then(|value| value.split(',').next())
            .unwrap();
        assert!(signed_headers.contains("content-length"));
        assert!(signed_headers.contains("content-type"));
    }
}