aws-smithy-http-client 1.4.0

HTTP client abstractions for generated smithy clients
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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! HTTP/2 connection behavior contracts.
//!
//! Each contract is implementation-neutral and has an explicit runner for every
//! HTTP client backend expected to preserve that behavior.

#![cfg(all(feature = "wire-mock", feature = "rustls-aws-lc"))]

mod common;

use aws_smithy_http_client::test_util::wire::connection::{ConnectionCloseReason, ManualGate};
use aws_smithy_http_client::tls;
use aws_smithy_http_client::Builder;
use aws_smithy_runtime_api::client::connection::{
    CaptureSmithyConnection, ConnectionMetadata as SmithyConnectionMetadata,
};
use aws_smithy_runtime_api::client::http::{SharedHttpClient, SharedHttpConnector};
use aws_smithy_runtime_api::client::orchestrator::HttpRequest;
use bytes::Bytes;
use common::client as test_client;
use common::client::{BackendConfig, HyperUtilLegacyPool};
use common::h2::{
    H2BodyPlan, H2ConnectionId, H2ConnectionPlan, H2ConnectionScript, H2Event, H2Response,
    H2StreamScript, H2TestServer,
};
use common::tls as test_tls;
use h2::Reason;
use http_body_util::BodyExt;
use std::error::Error;

trait HttpsClientBackend {
    fn build_https(
        &self,
        config: BackendConfig,
        provider: tls::Provider,
        tls_context: tls::TlsContext,
    ) -> SharedHttpClient;
}

impl HttpsClientBackend for HyperUtilLegacyPool {
    fn build_https(
        &self,
        config: BackendConfig,
        provider: tls::Provider,
        tls_context: tls::TlsContext,
    ) -> SharedHttpClient {
        let mut builder = Builder::new();
        if let Some(pool_idle_timeout) = config.pool_idle_timeout {
            builder = builder.pool_idle_timeout(pool_idle_timeout);
        }
        builder
            .tls_provider(provider)
            .tls_context(tls_context)
            .build_https()
    }
}

fn rustls_aws_lc() -> tls::Provider {
    tls::Provider::Rustls(tls::rustls_provider::CryptoMode::AwsLc)
}

fn h2_client_with_provider(
    backend: &dyn HttpsClientBackend,
    provider: tls::Provider,
) -> SharedHttpClient {
    backend.build_https(
        BackendConfig::default(),
        provider,
        test_tls::server_tls_context(),
    )
}

fn h2_client(backend: &dyn HttpsClientBackend) -> SharedHttpClient {
    h2_client_with_provider(backend, rustls_aws_lc())
}

fn stream_connection_ids(server: &H2TestServer, path: &str) -> Vec<H2ConnectionId> {
    server
        .events()
        .into_iter()
        .filter_map(|event| match event {
            H2Event::StreamAccepted {
                connection_id,
                path: event_path,
                ..
            } if event_path == path => Some(connection_id),
            _ => None,
        })
        .collect()
}

fn single_stream_connection(server: &H2TestServer, path: &str) -> H2ConnectionId {
    let connection_ids = stream_connection_ids(server, path);
    assert_eq!(connection_ids.len(), 1, "expected one stream for {path}");
    connection_ids[0]
}

async fn get_and_collect_with_capture(
    connector: &SharedHttpConnector,
    url: &str,
) -> (u16, Vec<u8>, SmithyConnectionMetadata) {
    let capture = CaptureSmithyConnection::new();
    let mut request = HttpRequest::get(url).expect("valid HTTP request");
    request.add_extension(capture.clone());
    let (status, body) = test_client::send_and_collect(connector, request).await;
    let metadata = capture
        .get()
        .expect("CaptureSmithyConnection should contain connection metadata");
    (status, body, metadata)
}

fn h2_error_reason(error: &(dyn Error + 'static)) -> Option<Reason> {
    let mut current = Some(error);
    while let Some(error) = current {
        if let Some(error) = error.downcast_ref::<h2::Error>() {
            return error.reason();
        }
        current = error.source();
    }
    None
}

mod reuse_and_multiplexing {
    use super::*;

    /// Sequential streams for one origin reuse an established H2 connection.
    async fn sequential_requests_reuse_connection(backend: &dyn HttpsClientBackend) {
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([H2ConnectionScript::new()
                .fallback(H2StreamScript::respond(H2Response::ok("hello")))]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        for request_number in 1..=5 {
            let (status, body) = test_client::get_and_collect(&connector, &server.url("/")).await;
            assert_eq!(status, 200, "request {request_number}");
            assert_eq!(body, b"hello", "request {request_number}");
        }

        assert_eq!(server.connection_count(), 1);
        assert_eq!(server.stream_count(), 5);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_sequential_requests_reuse_connection_with_hyper_util_legacy_pool() {
        sequential_requests_reuse_connection(&HyperUtilLegacyPool).await;
    }

    /// Concurrent requests multiplex as independent streams on a warmed H2 connection.
    async fn concurrent_requests_multiplex_on_warmed_connection(backend: &dyn HttpsClientBackend) {
        let body_gate = ManualGate::new();
        let script = H2ConnectionScript::new()
            .route("/warm", H2StreamScript::respond(H2Response::ok("warm")))
            .route(
                "/concurrent",
                H2StreamScript::respond(H2Response::new(http_1x::StatusCode::OK).body(
                    H2BodyPlan::gated(Bytes::new(), body_gate.waiter(), "multiplexed"),
                )),
            );
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/warm")).await;
        assert_eq!((status, body.as_slice()), (200, b"warm".as_slice()));

        let mut requests = tokio::task::JoinSet::new();
        for _ in 0..4 {
            let connector = connector.clone();
            let url = server.url("/concurrent");
            requests.spawn(async move { test_client::get_and_collect(&connector, &url).await });
        }

        body_gate
            .wait_for_arrivals(4, test_client::WAIT)
            .await
            .expect("all concurrent H2 streams should reach their body gate");
        assert_eq!(server.connection_count(), 1);
        assert_eq!(server.stream_count(), 5);
        body_gate.release();

        while let Some(result) = requests.join_next().await {
            let (status, body) = result.expect("request task should not panic");
            assert_eq!((status, body.as_slice()), (200, b"multiplexed".as_slice()));
        }

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_concurrent_requests_multiplex_on_warmed_connection_with_hyper_util_legacy_pool() {
        concurrent_requests_multiplex_on_warmed_connection(&HyperUtilLegacyPool).await;
    }

    /// Concurrent cold-start requests converge on one established H2 connection even when
    /// connection attempts race.
    async fn concurrent_cold_start_converges_on_one_h2_connection(
        backend: &dyn HttpsClientBackend,
    ) {
        let body_gate = ManualGate::new();
        let script = H2ConnectionScript::new()
            .allow_handshake_abandonment()
            .fallback(H2StreamScript::respond(
                H2Response::new(http_1x::StatusCode::OK).body(H2BodyPlan::gated(
                    Bytes::new(),
                    body_gate.waiter(),
                    "cold",
                )),
            ));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::unbounded(script))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);
        let mut requests = tokio::task::JoinSet::new();

        for _ in 0..4 {
            let connector = connector.clone();
            let url = server.url("/cold");
            requests.spawn(async move { test_client::get_and_collect(&connector, &url).await });
        }

        body_gate
            .wait_for_arrivals(4, test_client::WAIT)
            .await
            .expect("all cold-start H2 streams should reach their body gate");
        let ready_connections = server
            .events()
            .into_iter()
            .filter_map(|event| match event {
                H2Event::H2Ready { connection_id } => Some(connection_id),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(ready_connections.len(), 1);
        let stream_connections = stream_connection_ids(&server, "/cold");
        assert_eq!(stream_connections.len(), 4);
        assert!(
            stream_connections
                .iter()
                .all(|connection_id| *connection_id == ready_connections[0]),
            "all cold-start requests should converge on one established H2 connection"
        );
        body_gate.release();

        while let Some(result) = requests.join_next().await {
            let (status, body) = result.expect("request task should not panic");
            assert_eq!((status, body.as_slice()), (200, b"cold".as_slice()));
        }
        assert_eq!(server.stream_count(), 4);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_concurrent_cold_start_converges_on_one_h2_connection_with_hyper_util_legacy_pool()
    {
        concurrent_cold_start_converges_on_one_h2_connection(&HyperUtilLegacyPool).await;
    }
}

mod connection_metadata {
    use super::*;

    /// Poisoning captured H2 connection metadata moves later streams to a new connection.
    async fn poisoned_connection_is_not_reused(backend: &dyn HttpsClientBackend) {
        let script =
            H2ConnectionScript::new().fallback(H2StreamScript::respond(H2Response::ok("ok")));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script.clone(), script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        let (status, body, metadata) =
            get_and_collect_with_capture(&connector, &server.url("/first")).await;
        assert_eq!((status, body.as_slice()), (200, b"ok".as_slice()));
        metadata.poison();

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/second")).await;
        assert_eq!((status, body.as_slice()), (200, b"ok".as_slice()));

        let first_connection = single_stream_connection(&server, "/first");
        let second_connection = single_stream_connection(&server, "/second");
        assert_ne!(first_connection, second_connection);
        assert_eq!(server.connection_count(), 2);

        drop(metadata);
        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_poisoned_connection_is_not_reused_with_hyper_util_legacy_pool() {
        poisoned_connection_is_not_reused(&HyperUtilLegacyPool).await;
    }
}

mod stream_failures {
    use super::*;

    /// A peer reset fails only its H2 stream and leaves the connection available for reuse.
    async fn stream_reset_does_not_retire_connection(backend: &dyn HttpsClientBackend) {
        let reset_gate = ManualGate::new();
        let script = H2ConnectionScript::new()
            .route(
                "/reset",
                H2StreamScript::respond(H2Response::new(http_1x::StatusCode::OK).body(
                    H2BodyPlan::reset_after("partial", reset_gate.waiter(), Reason::CANCEL),
                )),
            )
            .route("/ok", H2StreamScript::respond(H2Response::ok("reused")));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        let response = test_client::send_request(
            &connector,
            HttpRequest::get(server.url("/reset")).expect("valid HTTP request"),
        )
        .await
        .expect("response headers should succeed before the stream reset");
        assert_eq!(response.status().as_u16(), 200);
        let mut body = response.into_body();
        let frame = tokio::time::timeout(test_client::WAIT, body.frame())
            .await
            .expect("response body frame should arrive within the outer deadline")
            .expect("response body should contain a frame")
            .expect("response body frame should be readable");
        let data = frame
            .into_data()
            .expect("the first body frame should be data");
        assert_eq!(data, b"partial".as_slice());
        reset_gate.release();
        let error = body
            .collect()
            .await
            .expect_err("the reset response body should fail");
        assert_eq!(h2_error_reason(error.as_ref()), Some(Reason::CANCEL));

        let reset_event = server
            .wait_for_event(test_client::WAIT, |event| {
                matches!(
                    event,
                    H2Event::ResetSent {
                        reason: Reason::CANCEL,
                        ..
                    }
                )
            })
            .await
            .expect("server should send the scripted stream reset");
        assert!(matches!(reset_event, H2Event::ResetSent { .. }));

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/ok")).await;
        assert_eq!((status, body.as_slice()), (200, b"reused".as_slice()));
        assert_eq!(
            single_stream_connection(&server, "/reset"),
            single_stream_connection(&server, "/ok")
        );
        assert_eq!(server.connection_count(), 1);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_stream_reset_does_not_retire_connection_with_hyper_util_legacy_pool() {
        stream_reset_does_not_retire_connection(&HyperUtilLegacyPool).await;
    }

    /// Dropping an incomplete response body cancels only that stream and permits reuse.
    async fn dropping_response_body_cancels_only_stream(backend: &dyn HttpsClientBackend) {
        let script = H2ConnectionScript::new()
            .route(
                "/drop",
                H2StreamScript::respond(
                    H2Response::new(http_1x::StatusCode::OK)
                        .body(H2BodyPlan::await_client_reset("partial", Reason::CANCEL)),
                ),
            )
            .route("/ok", H2StreamScript::respond(H2Response::ok("reused")));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        let response = test_client::send_request(
            &connector,
            HttpRequest::get(server.url("/drop")).expect("valid HTTP request"),
        )
        .await
        .expect("response headers should succeed");
        assert_eq!(response.status().as_u16(), 200);
        let mut body = response.into_body();
        let frame = tokio::time::timeout(test_client::WAIT, body.frame())
            .await
            .expect("response body frame should arrive within the outer deadline")
            .expect("response body should contain a frame")
            .expect("response body frame should be readable");
        let data = frame
            .into_data()
            .expect("the first body frame should be data");
        assert_eq!(data, b"partial".as_slice());
        drop(body);

        server
            .wait_for_event(test_client::WAIT, |event| {
                matches!(
                    event,
                    H2Event::ClientResetObserved {
                        reason: Reason::CANCEL,
                        ..
                    }
                )
            })
            .await
            .expect("dropping the body should reset only its H2 stream");

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/ok")).await;
        assert_eq!((status, body.as_slice()), (200, b"reused".as_slice()));
        assert_eq!(
            single_stream_connection(&server, "/drop"),
            single_stream_connection(&server, "/ok")
        );
        assert_eq!(server.connection_count(), 1);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_dropping_response_body_cancels_only_stream_with_hyper_util_legacy_pool() {
        dropping_response_body_cancels_only_stream(&HyperUtilLegacyPool).await;
    }
}

mod goaway_and_replacement {
    use super::*;

    /// Graceful GOAWAY drains an eligible in-flight stream while later streams use a
    /// replacement connection.
    async fn graceful_goaway_preserves_in_flight_stream_and_replaces_connection(
        backend: &dyn HttpsClientBackend,
    ) {
        let held_body_gate = ManualGate::new();
        let script = H2ConnectionScript::new()
            .route(
                "/held",
                H2StreamScript::respond(H2Response::new(http_1x::StatusCode::OK).body(
                    H2BodyPlan::gated("held-", held_body_gate.waiter(), "complete"),
                )),
            )
            .route(
                "/after",
                H2StreamScript::respond(H2Response::ok("replacement")),
            );
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script.clone(), script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client(backend);
        let connector = test_client::connector(&client);

        let held_response = test_client::send_request(
            &connector,
            HttpRequest::get(server.url("/held")).expect("valid HTTP request"),
        )
        .await
        .expect("held response headers should succeed");

        held_body_gate
            .wait_until_reached(test_client::WAIT)
            .await
            .expect("held stream should reach its body gate");
        let original_connection = single_stream_connection(&server, "/held");
        let held_stream_id = server
            .events()
            .iter()
            .find_map(|event| match event {
                H2Event::StreamAccepted {
                    connection_id,
                    stream_id,
                    path,
                    ..
                } if *connection_id == original_connection && path == "/held" => Some(*stream_id),
                _ => None,
            })
            .expect("the /held stream should have been accepted");

        server
            .send_graceful_goaway(original_connection)
            .await
            .expect("graceful GOAWAY should start");
        server
            .wait_for_event(test_client::WAIT, |event| {
                matches!(
                    event,
                    H2Event::GoAwaySent {
                        connection_id,
                        last_stream_id,
                        reason: Reason::NO_ERROR,
                    } if *connection_id == original_connection
                        && *last_stream_id == held_stream_id
                )
            })
            .await
            .expect("the final graceful GOAWAY should be flushed");

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/after")).await;
        assert_eq!((status, body.as_slice()), (200, b"replacement".as_slice()));
        let replacement_connection = single_stream_connection(&server, "/after");
        assert_ne!(original_connection, replacement_connection);
        // Match any close reason: the assertion is "no close happened yet", not "no specific
        // close happened." A more-specific pattern would weaken the negative check.
        assert!(!server.events().iter().any(|event| {
            matches!(
                event,
                H2Event::ConnectionClosed { connection_id, .. }
                    if *connection_id == original_connection
            )
        }));

        held_body_gate.release();
        let (status, body) = test_client::collect_response(held_response).await;
        assert_eq!(
            (status, body.as_slice()),
            (200, b"held-complete".as_slice())
        );
        server
            .wait_for_event(test_client::WAIT, |event| {
                matches!(
                    event,
                    H2Event::ConnectionClosed { connection_id, reason: ConnectionCloseReason::ClientClosed }
                        if *connection_id == original_connection
                )
            })
            .await
            .expect("the original connection should close after its held stream completes");

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_graceful_goaway_preserves_in_flight_stream_and_replaces_connection_with_hyper_util_legacy_pool(
    ) {
        graceful_goaway_preserves_in_flight_stream_and_replaces_connection(&HyperUtilLegacyPool)
            .await;
    }
}

#[cfg(feature = "s2n-tls")]
mod protocol_negotiation {
    use super::*;

    /// The s2n-tls provider negotiates `h2` with ALPN and reuses that connection.
    async fn s2n_negotiates_h2_and_reuses_connection(backend: &dyn HttpsClientBackend) {
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([H2ConnectionScript::new()
                .fallback(H2StreamScript::respond(H2Response::ok("s2n-h2")))]))
            .start()
            .await
            .expect("H2 server should start");
        let client = h2_client_with_provider(backend, tls::Provider::S2nTls);
        let connector = test_client::connector(&client);

        for request_number in 1..=3 {
            let (status, body) = test_client::get_and_collect(&connector, &server.url("/")).await;
            assert_eq!(status, 200, "request {request_number}");
            assert_eq!(body, b"s2n-h2", "request {request_number}");
        }

        let negotiated = server
            .events()
            .into_iter()
            .filter_map(|event| match event {
                H2Event::TlsNegotiated { alpn, .. } => Some(alpn),
                _ => None,
            })
            .collect::<Vec<_>>();
        assert_eq!(negotiated, vec![Some(b"h2".to_vec())]);
        assert_eq!(server.connection_count(), 1);
        assert_eq!(server.stream_count(), 3);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_s2n_negotiates_h2_and_reuses_connection_with_hyper_util_legacy_pool() {
        s2n_negotiates_h2_and_reuses_connection(&HyperUtilLegacyPool).await;
    }
}

mod idle_timeout {
    use super::*;
    use std::time::Duration;

    const IDLE_TIMEOUT: Duration = Duration::from_millis(100);

    fn client_with_idle_timeout(backend: &dyn HttpsClientBackend) -> SharedHttpClient {
        backend.build_https(
            BackendConfig {
                pool_idle_timeout: Some(IDLE_TIMEOUT),
            },
            rustls_aws_lc(),
            test_tls::server_tls_context(),
        )
    }

    /// An idle H2 connection is closed after its configured timeout and then replaced.
    async fn idle_connection_is_evicted_after_timeout(backend: &dyn HttpsClientBackend) {
        let script =
            H2ConnectionScript::new().fallback(H2StreamScript::respond(H2Response::ok("ok")));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script.clone(), script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = client_with_idle_timeout(backend);
        let connector = test_client::connector(&client);

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/first")).await;
        assert_eq!((status, body.as_slice()), (200, b"ok".as_slice()));
        let first_connection = single_stream_connection(&server, "/first");
        server
            .wait_for_event(test_client::WAIT, |event| {
                matches!(
                    event,
                    H2Event::ConnectionClosed { connection_id, reason: ConnectionCloseReason::ClientClosed }
                        if *connection_id == first_connection
                )
            })
            .await
            .expect("the idle H2 connection should close after its pool timeout");

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/second")).await;
        assert_eq!((status, body.as_slice()), (200, b"ok".as_slice()));
        assert_ne!(
            first_connection,
            single_stream_connection(&server, "/second")
        );

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_idle_connection_is_evicted_after_timeout_with_hyper_util_legacy_pool() {
        idle_connection_is_evicted_after_timeout(&HyperUtilLegacyPool).await;
    }

    /// An active stream survives the idle deadline, but the connection is replaced after the
    /// stream completes.
    async fn active_stream_survives_idle_timeout_but_later_request_uses_replacement(
        backend: &dyn HttpsClientBackend,
    ) {
        let held_body_gate = ManualGate::new();
        let script = H2ConnectionScript::new()
            .route(
                "/held",
                H2StreamScript::respond(H2Response::new(http_1x::StatusCode::OK).body(
                    H2BodyPlan::gated("held-", held_body_gate.waiter(), "complete"),
                )),
            )
            .route("/second", H2StreamScript::respond(H2Response::ok("second")));
        let server = H2TestServer::builder()
            .connections(H2ConnectionPlan::queue([script.clone(), script]))
            .start()
            .await
            .expect("H2 server should start");
        let client = client_with_idle_timeout(backend);
        let connector = test_client::connector(&client);

        let held_response = test_client::send_request(
            &connector,
            HttpRequest::get(server.url("/held")).expect("valid HTTP request"),
        )
        .await
        .expect("held response headers should succeed");

        held_body_gate
            .wait_until_reached(test_client::WAIT)
            .await
            .expect("held stream should reach its body gate");
        let first_connection = single_stream_connection(&server, "/held");
        tokio::time::sleep(IDLE_TIMEOUT * 2).await;
        // Match any close reason: the assertion is "no close happened yet", not "no specific
        // close happened." A more-specific pattern would weaken the negative check.
        assert!(!server.events().iter().any(|event| {
            matches!(
                event,
                H2Event::ConnectionClosed { connection_id, .. }
                    if *connection_id == first_connection
            )
        }));

        held_body_gate.release();
        let (status, body) = test_client::collect_response(held_response).await;
        assert_eq!(
            (status, body.as_slice()),
            (200, b"held-complete".as_slice())
        );

        let (status, body) = test_client::get_and_collect(&connector, &server.url("/second")).await;
        assert_eq!((status, body.as_slice()), (200, b"second".as_slice()));
        let second_connection = single_stream_connection(&server, "/second");
        assert_ne!(first_connection, second_connection);

        drop(connector);
        drop(client);
        server.shutdown().await.expect("clean H2 server shutdown");
    }

    #[tokio::test]
    async fn test_active_stream_survives_idle_timeout_but_later_request_uses_replacement_with_hyper_util_legacy_pool(
    ) {
        active_stream_survives_idle_timeout_but_later_request_uses_replacement(
            &HyperUtilLegacyPool,
        )
        .await;
    }
}