aws-smithy-http-server 0.66.4

Server runtime for Smithy Rust Server Framework.
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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Integration tests for the serve module
//!
//! These tests verify functionality that isn't explicitly tested elsewhere

use aws_smithy_http_server::body::{to_boxed, BoxBody};
use aws_smithy_http_server::routing::IntoMakeService;
use aws_smithy_http_server::serve::{Listener, ListenerExt};
use std::convert::Infallible;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::oneshot;
use tower::service_fn;

/// Simple test service that returns OK
async fn ok_service(_request: http::Request<hyper::body::Incoming>) -> Result<http::Response<BoxBody>, Infallible> {
    Ok(http::Response::builder().status(200).body(to_boxed("OK")).unwrap())
}

/// Test service that returns custom headers for verification
async fn service_with_custom_headers(
    _request: http::Request<hyper::body::Incoming>,
) -> Result<http::Response<BoxBody>, Infallible> {
    Ok(http::Response::builder()
        .status(200)
        .header("content-type", "text/plain")
        .header("x-custom-header", "test-value")
        .header("x-another-header", "another-value")
        .body(to_boxed("OK"))
        .unwrap())
}

/// Test that `configure_hyper()` actually applies HTTP/1 settings like title-case headers at the wire level.
#[tokio::test]
async fn test_configure_hyper_http1_keep_alive() {
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Start server with custom Hyper configuration including title_case_headers
    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(service_with_custom_headers)))
            .configure_hyper(|mut builder| {
                // Configure HTTP/1 settings
                builder.http1().keep_alive(true).title_case_headers(true);
                builder
            })
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    // Give server time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Use raw TCP to read the actual HTTP response headers
    let mut stream = tokio::net::TcpStream::connect(addr).await.expect("failed to connect");

    // Send a simple HTTP/1.1 request
    stream
        .write_all(b"GET /test HTTP/1.1\r\nHost: localhost\r\n\r\n")
        .await
        .expect("failed to write request");

    // Read the response
    let mut buffer = vec![0u8; 4096];
    let n = stream.read(&mut buffer).await.expect("failed to read response");
    let response_text = String::from_utf8_lossy(&buffer[..n]);

    // Verify status
    assert!(response_text.contains("HTTP/1.1 200 OK"), "Expected 200 OK status");

    // Verify title-case headers are present in the raw response
    // With title_case_headers(true), Hyper writes headers like "Content-Type:" instead of "content-type:"
    assert!(
        response_text.contains("Content-Type:") || response_text.contains("Content-Type: "),
        "Expected Title-Case 'Content-Type' header, got:\n{response_text}"
    );
    assert!(
        response_text.contains("X-Custom-Header:") || response_text.contains("X-Custom-Header: "),
        "Expected Title-Case 'X-Custom-Header' header, got:\n{response_text}"
    );
    assert!(
        response_text.contains("X-Another-Header:") || response_text.contains("X-Another-Header: "),
        "Expected Title-Case 'X-Another-Header' header, got:\n{response_text}"
    );

    // Verify it's NOT lowercase (which would be the default)
    assert!(
        !response_text.contains("content-type:"),
        "Headers should be Title-Case, not lowercase"
    );
    assert!(
        !response_text.contains("x-custom-header:"),
        "Headers should be Title-Case, not lowercase"
    );

    // Cleanup
    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that `tap_io()` invokes the closure with access to the TCP stream for configuration.
#[tokio::test]
async fn test_tap_io_set_nodelay() {
    let called = Arc::new(AtomicBool::new(false));
    let called_clone = called.clone();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind")
        .tap_io(move |tcp_stream| {
            // Set TCP_NODELAY and mark that we were called
            let _ = tcp_stream.set_nodelay(true);
            called_clone.store(true, Ordering::SeqCst);
        });

    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Make a request to trigger connection
    let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http();

    let uri = format!("http://{addr}/test");
    let request = http::Request::builder()
        .uri(&uri)
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = client.request(request).await.expect("request failed");
    assert_eq!(response.status(), 200);

    // Verify tap_io was called
    assert!(called.load(Ordering::SeqCst), "tap_io closure was not called");

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that `tap_io()` and `limit_connections()` can be chained together.
#[tokio::test]
async fn test_tap_io_with_limit_connections() {
    let tap_count = Arc::new(AtomicUsize::new(0));
    let tap_count_clone = tap_count.clone();

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind")
        .tap_io(move |_tcp_stream| {
            tap_count_clone.fetch_add(1, Ordering::SeqCst);
        })
        .limit_connections(10);

    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Make 3 requests - each creates a new connection
    // Note: HTTP clients may reuse connections, so we use Connection: close
    let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http();

    for _ in 0..3 {
        let uri = format!("http://{addr}/test");
        let request = http::Request::builder()
            .uri(&uri)
            .header("Connection", "close") // Force new connection each time
            .body(http_body_util::Empty::<bytes::Bytes>::new())
            .unwrap();

        let response = client.request(request).await.expect("request failed");
        assert_eq!(response.status(), 200);

        // Give time for connection to close
        tokio::time::sleep(Duration::from_millis(10)).await;
    }

    // Verify tap_io was called at least once (may be 1-3 depending on connection reuse)
    let count = tap_count.load(Ordering::SeqCst);
    assert!((1..=3).contains(&count), "tap_io was called {count} times");

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that the server works with Unix domain socket listeners.
#[cfg(unix)]
#[tokio::test]
async fn test_unix_listener() {
    use tokio::net::UnixListener;

    // Create a temporary socket path
    let socket_path = format!("/tmp/smithy-test-{}.sock", std::process::id());

    // Remove socket if it exists
    let _ = std::fs::remove_file(&socket_path);

    let listener = UnixListener::bind(&socket_path).expect("failed to bind unix socket");

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Connect via Unix socket
    let stream = tokio::net::UnixStream::connect(&socket_path)
        .await
        .expect("failed to connect to unix socket");

    // Use hyper to make a request over the Unix socket
    use hyper_util::rt::TokioIo;
    let io = TokioIo::new(stream);

    let (mut sender, conn) = hyper::client::conn::http1::handshake(io)
        .await
        .expect("handshake failed");

    tokio::spawn(async move {
        if let Err(err) = conn.await {
            eprintln!("Connection error: {err:?}");
        }
    });

    let request = http::Request::builder()
        .uri("/test")
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = sender.send_request(request).await.expect("request failed");
    assert_eq!(response.status(), 200);

    // Cleanup
    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
    let _ = std::fs::remove_file(&socket_path);
}

/// Test that `local_addr()` returns the correct bound address.
#[tokio::test]
async fn test_local_addr() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");

    let expected_addr = listener.local_addr().unwrap();

    let serve = aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)));

    let actual_addr = serve.local_addr().expect("failed to get local_addr");

    assert_eq!(actual_addr, expected_addr);
}

/// Test that `local_addr()` still works after calling `with_graceful_shutdown()`.
#[tokio::test]
async fn test_local_addr_with_graceful_shutdown() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");

    let expected_addr = listener.local_addr().unwrap();

    let (_shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let serve = aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
        .with_graceful_shutdown(async {
            shutdown_rx.await.ok();
        });

    let actual_addr = serve.local_addr().expect("failed to get local_addr");

    assert_eq!(actual_addr, expected_addr);
}

/// Test HTTP/2 prior knowledge mode (cleartext HTTP/2 without ALPN)
#[tokio::test]
async fn test_http2_only_prior_knowledge() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Start server with HTTP/2 only (prior knowledge mode)
    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .configure_hyper(|builder| builder.http2_only())
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Create HTTP/2 client (prior knowledge mode - no upgrade)
    let stream = tokio::net::TcpStream::connect(addr).await.expect("failed to connect");
    let io = hyper_util::rt::TokioIo::new(stream);

    let (mut sender, conn) = hyper::client::conn::http2::handshake(hyper_util::rt::TokioExecutor::new(), io)
        .await
        .expect("http2 handshake failed");

    tokio::spawn(async move {
        if let Err(err) = conn.await {
            eprintln!("HTTP/2 connection error: {err:?}");
        }
    });

    // Send HTTP/2 request
    let request = http::Request::builder()
        .uri("/test")
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = sender.send_request(request).await.expect("request failed");
    assert_eq!(response.status(), 200);

    // Cleanup
    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test HTTP/1-only mode using `http1_only()` configuration.
#[tokio::test]
async fn test_http1_only() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .configure_hyper(|builder| builder.http1_only())
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Use HTTP/1 client
    let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http();

    let uri = format!("http://{addr}/test");
    let request = http::Request::builder()
        .uri(&uri)
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = client.request(request).await.expect("request failed");
    assert_eq!(response.status(), 200);

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that the default server configuration auto-detects and supports both HTTP/1 and HTTP/2.
#[tokio::test]
async fn test_default_server_supports_both_http1_and_http2() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Start server with DEFAULT configuration (no configure_hyper call)
    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Test 1: Make an HTTP/1.1 request
    let http1_client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http();

    let uri = format!("http://{addr}/test");
    let request = http::Request::builder()
        .uri(&uri)
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = http1_client.request(request).await.expect("HTTP/1 request failed");
    assert_eq!(response.status(), 200, "HTTP/1 request should succeed");

    // Test 2: Make an HTTP/2 request (prior knowledge mode)
    let stream = tokio::net::TcpStream::connect(addr).await.expect("failed to connect");
    let io = hyper_util::rt::TokioIo::new(stream);

    let (mut sender, conn) = hyper::client::conn::http2::handshake(hyper_util::rt::TokioExecutor::new(), io)
        .await
        .expect("http2 handshake failed");

    tokio::spawn(async move {
        if let Err(err) = conn.await {
            eprintln!("HTTP/2 connection error: {err:?}");
        }
    });

    let request = http::Request::builder()
        .uri("/test")
        .body(http_body_util::Empty::<bytes::Bytes>::new())
        .unwrap();

    let response = sender.send_request(request).await.expect("HTTP/2 request failed");
    assert_eq!(response.status(), 200, "HTTP/2 request should succeed");

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that the server handles concurrent HTTP/1 and HTTP/2 connections simultaneously using a barrier.
#[tokio::test]
async fn test_mixed_protocol_concurrent_connections() {
    use tokio::sync::Barrier;
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Use a barrier to ensure all 4 requests arrive before any respond
    // This proves they're being handled concurrently
    let barrier = Arc::new(Barrier::new(4));
    let barrier_clone = barrier.clone();

    let barrier_service = move |_request: http::Request<hyper::body::Incoming>| {
        let barrier = barrier_clone.clone();
        async move {
            // Wait for all 4 requests to arrive
            barrier.wait().await;
            // Now all respond together
            Ok::<_, Infallible>(http::Response::builder().status(200).body(to_boxed("OK")).unwrap())
        }
    };

    // Start server with default configuration (supports both protocols)
    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(barrier_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Start multiple HTTP/1 connections
    let make_http1_request = |addr: std::net::SocketAddr, path: &'static str| async move {
        let stream = tokio::net::TcpStream::connect(addr).await.unwrap();
        let io = hyper_util::rt::TokioIo::new(stream);

        let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await.unwrap();

        tokio::spawn(async move {
            if let Err(e) = conn.await {
                eprintln!("HTTP/1 connection error: {e:?}");
            }
        });

        let request = http::Request::builder()
            .uri(path)
            .body(http_body_util::Empty::<bytes::Bytes>::new())
            .unwrap();

        sender.send_request(request).await
    };

    // Start multiple HTTP/2 connections
    let make_http2_request = |addr: std::net::SocketAddr, path: &'static str| async move {
        let stream = tokio::net::TcpStream::connect(addr).await.unwrap();
        let io = hyper_util::rt::TokioIo::new(stream);

        let (mut sender, conn) = hyper::client::conn::http2::handshake(hyper_util::rt::TokioExecutor::new(), io)
            .await
            .unwrap();

        tokio::spawn(async move {
            if let Err(e) = conn.await {
                eprintln!("HTTP/2 connection error: {e:?}");
            }
        });

        let request = http::Request::builder()
            .uri(path)
            .body(http_body_util::Empty::<bytes::Bytes>::new())
            .unwrap();

        sender.send_request(request).await
    };

    // Launch 2 HTTP/1 and 2 HTTP/2 requests concurrently
    let h1_handle1 = tokio::spawn(make_http1_request(addr, "/http1-test1"));
    let h1_handle2 = tokio::spawn(make_http1_request(addr, "/http1-test2"));
    let h2_handle1 = tokio::spawn(make_http2_request(addr, "/http2-test1"));
    let h2_handle2 = tokio::spawn(make_http2_request(addr, "/http2-test2"));

    // Wait for all requests to complete with timeout
    // If they complete, it means the barrier was satisfied (all 4 arrived concurrently)
    let timeout = Duration::from_secs(60);
    let h1_result1 = tokio::time::timeout(timeout, h1_handle1)
        .await
        .expect("HTTP/1 request 1 timed out")
        .unwrap();
    let h1_result2 = tokio::time::timeout(timeout, h1_handle2)
        .await
        .expect("HTTP/1 request 2 timed out")
        .unwrap();
    let h2_result1 = tokio::time::timeout(timeout, h2_handle1)
        .await
        .expect("HTTP/2 request 1 timed out")
        .unwrap();
    let h2_result2 = tokio::time::timeout(timeout, h2_handle2)
        .await
        .expect("HTTP/2 request 2 timed out")
        .unwrap();

    // All requests should succeed
    assert!(h1_result1.is_ok(), "HTTP/1 request 1 failed");
    assert!(h1_result2.is_ok(), "HTTP/1 request 2 failed");
    assert!(h2_result1.is_ok(), "HTTP/2 request 1 failed");
    assert!(h2_result2.is_ok(), "HTTP/2 request 2 failed");

    assert_eq!(h1_result1.unwrap().status(), 200);
    assert_eq!(h1_result2.unwrap().status(), 200);
    assert_eq!(h2_result1.unwrap().status(), 200);
    assert_eq!(h2_result2.unwrap().status(), 200);

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that `limit_connections()` enforces the connection limit correctly using semaphores.
#[tokio::test]
async fn test_limit_connections_blocks_excess() {
    use tokio::sync::Semaphore;
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind")
        .limit_connections(2); // Allow only 2 concurrent connections

    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Use a semaphore to control when requests complete
    // We'll hold permits to keep connections open
    let sem = Arc::new(Semaphore::new(0));
    let sem_clone = sem.clone();

    let semaphore_service = move |_request: http::Request<hyper::body::Incoming>| {
        let sem = sem_clone.clone();
        async move {
            // Wait for a permit (blocks until we release permits in the test)
            let _permit = sem.acquire().await.unwrap();
            Ok::<_, Infallible>(http::Response::builder().status(200).body(to_boxed("OK")).unwrap())
        }
    };

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(semaphore_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Create 3 separate TCP connections and HTTP/1 clients
    let make_request = |addr: std::net::SocketAddr| async move {
        let stream = tokio::net::TcpStream::connect(addr).await.unwrap();
        let io = hyper_util::rt::TokioIo::new(stream);

        let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await.unwrap();

        tokio::spawn(async move {
            if let Err(e) = conn.await {
                eprintln!("Connection error: {e:?}");
            }
        });

        let request = http::Request::builder()
            .uri("/test")
            .body(http_body_util::Empty::<bytes::Bytes>::new())
            .unwrap();

        sender.send_request(request).await
    };

    // Start 3 requests concurrently
    let handle1 = tokio::spawn(make_request(addr));
    let handle2 = tokio::spawn(make_request(addr));
    let handle3 = tokio::spawn(make_request(addr));

    // Give them time to attempt connections
    tokio::time::sleep(Duration::from_millis(100)).await;

    // Now release 3 permits so all requests can complete
    sem.add_permits(3);

    // All requests should eventually complete (with timeout to prevent hanging)
    let timeout = Duration::from_secs(60);
    let result1 = tokio::time::timeout(timeout, handle1)
        .await
        .expect("First request timed out")
        .unwrap();
    let result2 = tokio::time::timeout(timeout, handle2)
        .await
        .expect("Second request timed out")
        .unwrap();
    let result3 = tokio::time::timeout(timeout, handle3)
        .await
        .expect("Third request timed out")
        .unwrap();

    // All should succeed - the limiter allows connections through (just limits concurrency)
    assert!(result1.is_ok(), "First request failed");
    assert!(result2.is_ok(), "Second request failed");
    assert!(result3.is_ok(), "Third request failed");

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}

/// Test that graceful shutdown completes quickly when there are no active connections.
#[tokio::test]
async fn test_immediate_graceful_shutdown() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(ok_service)))
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    // Give server time to start
    tokio::time::sleep(Duration::from_millis(50)).await;

    // Immediately trigger shutdown without any connections
    shutdown_tx.send(()).unwrap();

    // Server should shutdown quickly since there are no connections
    let result = tokio::time::timeout(Duration::from_millis(500), server_handle)
        .await
        .expect("server did not shutdown in time")
        .expect("server task panicked");

    assert!(result.is_ok(), "server should shutdown cleanly");
}

/// Test HTTP/2 stream multiplexing by sending concurrent requests over a single connection using a barrier.
#[tokio::test]
async fn test_multiple_concurrent_http2_streams() {
    use tokio::sync::Barrier;
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
        .await
        .expect("failed to bind");
    let addr = listener.local_addr().unwrap();

    let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();

    // Use a barrier to ensure all 5 requests arrive before any respond
    // This proves HTTP/2 multiplexing is working
    let barrier = Arc::new(Barrier::new(5));
    let barrier_clone = barrier.clone();

    let barrier_service = move |_request: http::Request<hyper::body::Incoming>| {
        let barrier = barrier_clone.clone();
        async move {
            // Wait for all 5 requests to arrive
            barrier.wait().await;
            // Now all respond together
            Ok::<_, Infallible>(http::Response::builder().status(200).body(to_boxed("OK")).unwrap())
        }
    };

    // Start server with HTTP/2 only and configure max concurrent streams
    let server_handle = tokio::spawn(async move {
        aws_smithy_http_server::serve::serve(listener, IntoMakeService::new(service_fn(barrier_service)))
            .configure_hyper(|mut builder| {
                builder.http2().max_concurrent_streams(5);
                builder.http2_only()
            })
            .with_graceful_shutdown(async {
                shutdown_rx.await.ok();
            })
            .await
    });

    tokio::time::sleep(Duration::from_millis(50)).await;

    // Create HTTP/2 connection
    let stream = tokio::net::TcpStream::connect(addr).await.expect("failed to connect");
    let io = hyper_util::rt::TokioIo::new(stream);

    let (sender, conn) = hyper::client::conn::http2::handshake(hyper_util::rt::TokioExecutor::new(), io)
        .await
        .expect("http2 handshake failed");

    tokio::spawn(async move {
        if let Err(err) = conn.await {
            eprintln!("HTTP/2 connection error: {err:?}");
        }
    });

    // Send multiple concurrent requests over the same HTTP/2 connection
    let mut handles = vec![];

    for i in 0..5 {
        let request = http::Request::builder()
            .uri(format!("/test{i}"))
            .body(http_body_util::Empty::<bytes::Bytes>::new())
            .unwrap();

        let mut sender_clone = sender.clone();
        let handle = tokio::spawn(async move { sender_clone.send_request(request).await });
        handles.push(handle);
    }

    // Wait for all requests to complete with timeout
    // If they complete, it means the barrier was satisfied (all 5 arrived concurrently)
    let timeout = Duration::from_secs(60);
    for (i, handle) in handles.into_iter().enumerate() {
        let response = tokio::time::timeout(timeout, handle)
            .await
            .unwrap_or_else(|_| panic!("Request {i} timed out"))
            .unwrap()
            .expect("request failed");
        assert_eq!(response.status(), 200);
    }

    shutdown_tx.send(()).unwrap();
    let _ = tokio::time::timeout(Duration::from_secs(2), server_handle).await;
}