hyperlane-core 22.7.0

A lightweight, high-performance, and cross-platform Rust HTTP server library built on Tokio. It simplifies modern web service development by providing built-in support for middleware, WebSocket, Server-Sent Events (SSE), and raw TCP communication. With a unified and ergonomic API across Windows, Linux, and MacOS, it enables developers to build robust, scalable, and event-driven network applications with minimal overhead and maximum flexibility.
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
use super::*;

#[tokio::test]
async fn http3_rejects_a_second_control_stream_before_the_first_closes() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .unwrap();
    let (_endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let mut first: QuicSendStream = connection.open_uni().await.unwrap();
    first.write_all(&[0, 4, 0]).await.unwrap();
    let mut second: QuicSendStream = connection.open_uni().await.unwrap();
    second.write_all(&[0, 4, 0]).await.unwrap();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    control.shutdown().await;
    assert!(
        closed.is_ok(),
        "duplicate control streams must close the connection"
    );
    match closed.unwrap() {
        quinn::ConnectionError::ApplicationClosed(error) => {
            assert_eq!(error.error_code, quinn::VarInt::from_u32(0x103));
        }
        other => panic!("expected H3_STREAM_CREATION_ERROR, got {other:?}"),
    }
}

#[tokio::test]
async fn http3_rejects_eof_on_the_peer_control_stream() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .unwrap();
    let (_endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let mut stream: QuicSendStream = connection.open_uni().await.unwrap();
    stream.write_all(&[0, 4, 0]).await.unwrap();
    stream.finish().unwrap();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    control.shutdown().await;
    assert!(
        closed.is_ok(),
        "closing a critical stream must close the connection"
    );
    match closed.unwrap() {
        quinn::ConnectionError::ApplicationClosed(error) => {
            assert_eq!(error.error_code, quinn::VarInt::from_u32(0x104));
        }
        other => panic!("expected H3_CLOSED_CRITICAL_STREAM, got {other:?}"),
    }
}

#[tokio::test]
async fn http3_rejects_eof_on_qpack_critical_streams() {
    for stream_type in [2u8, 3u8] {
        let (control, port): (ServerControlHook, u16) =
            start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
                .await
                .unwrap();
        let (_endpoint, connection): (Endpoint, quinn::Connection) =
            connect_quic(port, ALPN_HTTP3).await.unwrap();
        let mut stream: QuicSendStream = connection.open_uni().await.unwrap();
        stream.write_all(&[stream_type]).await.unwrap();
        stream.finish().unwrap();
        let closed: Result<quinn::ConnectionError, Elapsed> =
            timeout(Duration::from_secs(2), connection.closed()).await;
        control.shutdown().await;
        assert!(
            closed.is_ok(),
            "closing QPACK critical stream must close the connection"
        );
        match closed.unwrap() {
            quinn::ConnectionError::ApplicationClosed(error) => {
                assert_eq!(error.error_code, quinn::VarInt::from_u32(0x104));
            }
            other => panic!("expected H3_CLOSED_CRITICAL_STREAM, got {other:?}"),
        }
    }
}

#[tokio::test]
async fn http3_rejects_missing_or_duplicate_or_invalid_settings_on_control_stream() {
    let cases: [(&[u8], u32); 4] = [
        (&[0, 7, 1, 0], 0x10a),
        (&[0, 4, 0, 4, 0], 0x105),
        (&[0, 4, 0, 0, 0], 0x105),
        (&[0, 4, 2, 2, 0], 0x109),
    ];
    for (bytes, expected_code) in cases {
        let (control, port): (ServerControlHook, u16) =
            start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
                .await
                .unwrap();
        let (_endpoint, connection): (Endpoint, quinn::Connection) =
            connect_quic(port, ALPN_HTTP3).await.unwrap();
        let mut stream: QuicSendStream = connection.open_uni().await.unwrap();
        stream.write_all(bytes).await.unwrap();
        let closed: Result<quinn::ConnectionError, Elapsed> =
            timeout(Duration::from_secs(2), connection.closed()).await;
        control.shutdown().await;
        assert!(
            closed.is_ok(),
            "invalid control stream must close connection"
        );
        match closed.unwrap() {
            quinn::ConnectionError::ApplicationClosed(error) => {
                assert_eq!(error.error_code, quinn::VarInt::from_u32(expected_code));
            }
            other => panic!("expected error {expected_code:#x}, got {other:?}"),
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn http3_multi_worker_connection_migration_preserves_session() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .unwrap();
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let (mut driver, mut send_request) = new(h3_quinn::Connection::new(connection)).await.unwrap();
    let drive: JoinHandle<()> = spawn(async move {
        let _: h3::error::ConnectionError =
            poll_fn(|cx: &mut task::Context<'_>| driver.poll_close(cx)).await;
    });
    for migrated in [false, true] {
        if migrated {
            let new_socket: StdUdpSocket = StdUdpSocket::bind((LOOPBACK_HOST, 0)).unwrap();
            endpoint.rebind(new_socket).unwrap();
        }
        let req: ProtocolRequest<()> = ProtocolRequest::builder()
            .method("GET")
            .uri(format!(
                "https://{TLS_SERVER_NAME}:{port}{VERSION_ROUTE_PATH}"
            ))
            .body(())
            .unwrap();
        let mut stream: RequestStream<BidiStream<Bytes>, Bytes> =
            send_request.send_request(req).await.unwrap();
        stream.finish().await.unwrap();
        let resp: ProtocolResponse<()> = stream.recv_response().await.unwrap();
        assert_eq!(resp.status(), 200);
        let mut body: Vec<u8> = Vec::new();
        while let Some(chunk) = stream.recv_data().await.unwrap() {
            body.extend_from_slice(chunk.chunk());
        }
        assert_eq!(
            String::from_utf8_lossy(&body),
            format!("{:?}", HttpVersion::Http3)
        );
    }
    drop(send_request);
    drive.abort();
    control.shutdown().await;
}

#[tokio::test]
async fn http3_cancelled_backpressured_write_can_be_followed_by_a_smaller_write() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 server must start");
    let (_endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let (mut driver, mut client) = new(h3_quinn::Connection::new(connection.clone()))
        .await
        .unwrap();
    let driving: JoinHandle<()> = spawn(async move {
        let _: h3::error::ConnectionError = driver.wait_idle().await;
    });
    let target: String = format!("https://{TLS_SERVER_NAME}:{port}{CANCELLED_WRITE_ROUTE_PATH}");
    let request: http::Request<()> = http::Request::builder().uri(target).body(()).unwrap();
    let mut stream: RequestStream<BidiStream<Bytes>, Bytes> =
        client.send_request(request).await.unwrap();
    stream.finish().await.unwrap();
    let response: http::Response<()> = stream.recv_response().await.unwrap();
    assert_eq!(response.status().as_u16(), 200);
    sleep(Duration::from_millis(150)).await;
    let mut body: Vec<u8> = Vec::new();
    loop {
        let chunk: Option<_> = timeout(
            Duration::from_secs(REQUEST_TIMEOUT_SECS),
            stream.recv_data(),
        )
        .await
        .expect("the writer must resume when receive capacity is released")
        .expect("cancelling a write must not reset the stream");
        let Some(chunk) = chunk else { break };
        body.extend_from_slice(chunk.chunk());
    }
    assert!(!body.is_empty());
    assert!(body.len() < CANCELLED_WRITE_BYTES);
    assert_eq!(body.last(), Some(&b'!'));
    assert!(body[..body.len() - 1].iter().all(|byte: &u8| *byte == b'x'));
    driving.abort();
    connection.close(quinn::VarInt::from_u32(0), b"done");
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_reports_the_real_local_and_peer_addresses_of_the_connection() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) = connect_quic(port, ALPN_HTTP3)
        .await
        .expect("a quic client must connect");
    let client_local: SocketAddr = endpoint
        .local_addr()
        .expect("the client endpoint must report its own udp address");
    let target: String = format!("https://{TLS_SERVER_NAME}:{port}{SOCKET_METADATA_ROUTE_PATH}");
    let (status, _headers, body, _alpn): H3HeadReply = h3_exchange_on(H3Exchange {
        connection,
        alpn: None,
        method: "GET",
        target: &target,
        body: &[],
    })
    .await
    .expect("h3 socket metadata request must complete");
    assert_eq!(status, 200);
    let text: String = String::from_utf8_lossy(&body).to_string();
    let (reported_local, reported_peer): (String, String) =
        match text.split_once(SOCKET_METADATA_SEPARATOR) {
            Some(pair) => (pair.0.to_string(), pair.1.to_string()),
            None => panic!("the route must report both addresses, got: {text:?}"),
        };
    let expected_local: SocketAddr = format!("{LOOPBACK_HOST}:{port}")
        .parse()
        .expect("the bind address must parse");
    assert_eq!(
        reported_local,
        expected_local.to_string(),
        "an h3 stream must carry the connection's real local address, not a fabricated one"
    );
    assert_eq!(
        reported_peer,
        client_local.to_string(),
        "an h3 stream must carry the connection's real peer address"
    );
    control.shutdown().await;
}

#[tokio::test]
async fn http3_streams_a_body_that_never_announced_a_content_length() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let (status, headers, body, _alpn): H3HeadReply = h3_request_with_headers(
        port,
        "GET",
        &format!("https://{TLS_SERVER_NAME}:{port}{UNFRAMED_BODY_ROUTE_PATH}"),
        &[],
    )
    .await
    .expect("an unframed h3 body must be accepted");
    assert_eq!(status, 200);
    assert!(
        !headers.contains_key(CONTENT_LENGTH),
        "the head must not announce a length for a body framed by data frames"
    );
    assert_eq!(
        String::from_utf8_lossy(&body),
        UNFRAMED_BODY,
        "a body with no content-length must still reach the client in full"
    );
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_negotiates_h3_alpn_and_tls13() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let (status, version, body, alpn): H3Reply = h3_request(
        port,
        "GET",
        &format!("https://{TLS_SERVER_NAME}:{port}{VERSION_ROUTE_PATH}"),
        &[],
    )
    .await
    .expect("h3 request must complete");
    assert_eq!(status, 200);
    assert_eq!(
        version,
        http::Version::HTTP_3,
        "the response must be framed as HTTP/3, not downgraded"
    );
    assert_eq!(
        alpn,
        Some(ALPN_HTTP3.to_vec()),
        "QUIC must negotiate the h3 ALPN identifier"
    );
    assert_eq!(
        String::from_utf8_lossy(&body),
        format!("{:?}", HttpVersion::Http3)
    );
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_reads_path_query_and_header() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let target: String = format!(
        "https://{TLS_SERVER_NAME}:{port}{PROBE_PATH}?{}",
        PROBE_QUERY
    );
    let (status, version, _body, _alpn): H3Reply = h3_request(port, "GET", &target, &[])
        .await
        .expect("h3 probe request must complete");
    assert_eq!(status, 200);
    assert_eq!(version, http::Version::HTTP_3);
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_binary_post_round_trips_hostile_bytes() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let payload: Vec<u8> = hostile_binary_payload();
    let (status, version, body, _alpn): H3Reply = h3_request(
        port,
        "POST",
        &format!("https://{TLS_SERVER_NAME}:{port}{BINARY_ROUTE_PATH}"),
        &payload,
    )
    .await
    .expect("h3 binary post must complete");
    assert_eq!(status, 200);
    assert_eq!(version, http::Version::HTTP_3);
    assert_eq!(
        body, payload,
        "every hostile byte must survive the h3 body round trip"
    );
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_delivers_a_response_above_the_flow_control_window() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let expected: Vec<u8> = large_binary_payload();
    let (status, version, body, _alpn): H3Reply = h3_request(
        port,
        "GET",
        &format!("https://{TLS_SERVER_NAME}:{port}{LARGE_ROUTE_PATH}"),
        &[],
    )
    .await
    .expect("large h3 response must complete");
    assert_eq!(status, 200);
    assert_eq!(version, http::Version::HTTP_3);
    assert_eq!(body.len(), expected.len());
    assert!(
        body == expected,
        "the large response must be byte identical past the flow control window"
    );
    control.shutdown().await;
}

#[tokio::test]
async fn http3_over_quic_shutdown_releases_the_bound_udp_port() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    assert_wait_pending_then_complete(&control).await;
    let rebound: io::Result<UdpSocket> = UdpSocket::bind(format!("{LOOPBACK_HOST}:{port}"));
    assert!(
        rebound.is_ok(),
        "udp port {port} must be free again after shutdown and wait"
    );
}

#[tokio::test]
async fn http3_over_quic_rejects_a_client_offering_only_h2() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let outcome: Result<(Endpoint, quinn::Connection), String> =
        connect_quic(port, ALPN_HTTP2).await;
    assert!(
        outcome.is_err(),
        "an http3 listener must refuse an h2-only QUIC client instead of downgrading"
    );
    control.shutdown().await;
}

#[tokio::test]
async fn h3_sse_chunk_reaches_the_client_before_the_handler_finishes() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let _lease: GateLease = gate_lease(GateSlot::SseHttp3).await;
    let target: String = format!("https://{TLS_SERVER_NAME}:{port}{SSE_ROUTE_PATH_HTTP3}");
    let reading: JoinHandle<(Vec<u8>, bool)> =
        spawn(async move { h3_stream_until(port, &target, SSE_FIRST_CHUNK).await });
    route_gate(GateSlot::SseHttp3)
        .await_first_chunk_sent()
        .await;
    let (collected, matched): (Vec<u8>, bool) =
        timeout(Duration::from_secs(FAST_STREAM_BUDGET_MILLIS), reading)
            .await
            .expect("the first sse chunk must be readable while the handler is still blocked")
            .expect("the reader task must not panic");
    let text: String = String::from_utf8_lossy(&collected).to_string();
    assert!(
        matched,
        "the first sse chunk must be readable while the handler is still blocked, got: {text:?}"
    );
    assert!(
        !text.contains(SSE_SECOND_CHUNK),
        "the second chunk must wait for the handler to be released, got: {text:?}"
    );
    route_gate(GateSlot::SseHttp3).release_sse();
    control.shutdown().await;
}

#[tokio::test]
async fn http3_one_connection_answers_a_fast_stream_while_a_slow_stream_is_blocked() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("http3 quic server must start");
    let _lease: GateLease = gate_lease(GateSlot::SlowHttp3).await;
    let order: Arc<Mutex<Vec<&'static str>>> = Arc::new(Mutex::new(Vec::new()));
    let slow_order: Arc<Mutex<Vec<&'static str>>> = Arc::clone(&order);
    let slow: JoinHandle<Result<H3Reply, String>> = spawn(async move {
        let outcome: Result<H3Reply, String> = h3_request(
            port,
            "GET",
            &format!("https://{TLS_SERVER_NAME}:{port}{SLOW_ROUTE_PATH_HTTP3}"),
            &[],
        )
        .await;
        slow_order.lock().expect("order lock").push("slow");
        outcome
    });
    route_gate(GateSlot::SlowHttp3).await_slow_entered().await;
    let fast_order: Arc<Mutex<Vec<&'static str>>> = Arc::clone(&order);
    let fast: JoinHandle<Result<H3Reply, String>> = spawn(async move {
        let outcome: Result<H3Reply, String> = h3_request(
            port,
            "GET",
            &format!("https://{TLS_SERVER_NAME}:{port}{VERSION_ROUTE_PATH}"),
            &[],
        )
        .await;
        fast_order.lock().expect("order lock").push("fast");
        outcome
    });
    let budget: Duration = Duration::from_millis(FAST_STREAM_BUDGET_MILLIS);
    let fast_result: Result<H3Reply, String> = timeout(budget, fast)
        .await
        .expect("the fast stream must complete while the slow handler is still blocked")
        .expect("the fast stream task must not panic");
    assert!(
        fast_result.is_ok(),
        "the fast request must succeed while the slow handler is held"
    );
    assert_eq!(
        *order.lock().expect("order lock"),
        vec!["fast"],
        "only the fast stream may complete while the slow handler is blocked"
    );
    route_gate(GateSlot::SlowHttp3).release_slow();
    let slow_result: Result<H3Reply, String> =
        timeout(Duration::from_secs(REQUEST_TIMEOUT_SECS), slow)
            .await
            .expect("the released slow stream must finish inside its timeout")
            .expect("the slow stream task must not panic");
    assert!(slow_result.is_ok());
    assert_eq!(*order.lock().expect("order lock"), vec!["fast", "slow"]);
    control.shutdown().await;
}

#[tokio::test]
async fn http3_rejects_data_frame_before_headers_on_request_stream() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("the HTTP/3 server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let (mut send, _recv): (QuicSendStream, QuicRecvStream) = connection.open_bi().await.unwrap();
    send.write_all(&[0x00, 0x02, b'h', b'i']).await.unwrap();
    let _: Result<(), _> = send.finish();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    assert!(
        matches!(
            closed,
            Ok(quinn::ConnectionError::ApplicationClosed(ref frame))
                if frame.error_code == VarInt::from_u32(0x105)
        ),
        "DATA before HEADERS must close with H3_FRAME_UNEXPECTED (0x105), got: {closed:?}"
    );
    endpoint.wait_idle().await;
    control.shutdown().await;
}

#[tokio::test]
async fn http3_rejects_reserved_http2_frame_on_control_stream() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("the HTTP/3 server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let mut ctrl: QuicSendStream = connection.open_uni().await.unwrap();
    ctrl.write_all(&[0x00, 0x04, 0x00, 0x02, 0x00])
        .await
        .unwrap();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    assert!(
        matches!(
            closed,
            Ok(quinn::ConnectionError::ApplicationClosed(ref frame))
                if frame.error_code == VarInt::from_u32(0x105)
        ),
        "reserved HTTP/2 frame on control stream must close with H3_FRAME_UNEXPECTED (0x105), got: {closed:?}"
    );
    endpoint.wait_idle().await;
    control.shutdown().await;
}

#[tokio::test]
async fn http3_rejects_data_frame_after_trailing_headers() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("the HTTP/3 server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let (mut send, _recv): (QuicSendStream, QuicRecvStream) = connection.open_bi().await.unwrap();
    let mut qpack_get: Vec<u8> = vec![0x00, 0x00, 0xd1, 0xd7, 0xc1, 0x50, 9];
    qpack_get.extend_from_slice(b"localhost");
    let mut frame: Vec<u8> = Vec::new();
    frame.push(0x01);
    frame.push(qpack_get.len() as u8);
    frame.extend_from_slice(&qpack_get);
    frame.extend_from_slice(&[0x01, 0x02, 0x00, 0x00]);
    frame.extend_from_slice(&[0x00, 0x01, b'x']);
    send.write_all(&frame).await.unwrap();
    let _: Result<(), _> = send.finish();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    assert!(
        matches!(
            closed,
            Ok(quinn::ConnectionError::ApplicationClosed(ref frame))
                if frame.error_code == VarInt::from_u32(0x105)
        ),
        "DATA after trailers must close with H3_FRAME_UNEXPECTED (0x105), got: {closed:?}"
    );
    endpoint.wait_idle().await;
    control.shutdown().await;
}

#[tokio::test]
async fn http3_rejects_dynamic_qpack_encoder_instruction_when_capacity_is_zero() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("the HTTP/3 server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let mut encoder: QuicSendStream = connection.open_uni().await.unwrap();
    encoder.write_all(&[0x02, 0x21]).await.unwrap();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    assert!(
        matches!(
            closed,
            Ok(quinn::ConnectionError::ApplicationClosed(ref frame))
                if frame.error_code == VarInt::from_u32(0x201)
        ),
        "dynamic QPACK encoder instruction when capacity is zero must close with QPACK_ENCODER_STREAM_ERROR (0x201), got: {closed:?}"
    );
    endpoint.wait_idle().await;
    control.shutdown().await;
}

#[tokio::test]
async fn http3_ignores_an_unknown_non_grease_frame_before_request_headers() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .unwrap();
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let (mut send, mut recv): (QuicSendStream, QuicRecvStream) =
        connection.open_bi().await.unwrap();
    let mut block: Vec<u8> = vec![0, 0, 0xd1, 0xd7, 0x50, 9];
    block.extend_from_slice(b"localhost");
    block.push(0x51);
    block.push(VERSION_ROUTE_PATH.len() as u8);
    block.extend_from_slice(VERSION_ROUTE_PATH.as_bytes());
    assert!(block.len() < 64);
    let mut frames: Vec<u8> = vec![0x20, 0, 1, block.len() as u8];
    frames.extend_from_slice(&block);
    send.write_all(&frames).await.unwrap();
    send.finish().unwrap();
    let response: Vec<u8> = timeout(Duration::from_secs(2), recv.read_to_end(64 * 1024))
        .await
        .unwrap()
        .unwrap();
    let (frame_type, type_len): (u64, usize) = decode_varint(&response).unwrap();
    let (_, length_len): (u64, usize) = decode_varint(&response[type_len..]).unwrap();
    let start: usize = type_len + length_len;
    assert_eq!(frame_type, 1);
    assert_eq!(response.get(start..start + 3), Some(&[0, 0, 0xd9][..]));
    assert!(connection.close_reason().is_none());
    connection.close(VarInt::from_u32(H3_NO_ERROR), b"done");
    endpoint.wait_idle().await;
    control.shutdown().await;
}

#[tokio::test]
async fn http3_rejects_qpack_decoder_insert_count_increment_when_capacity_is_zero() {
    let (control, port): (ServerControlHook, u16) =
        start_protocol_server(ServerProtocol::Http3, Some(trusted_tls_config()))
            .await
            .expect("the HTTP/3 server must start");
    let (endpoint, connection): (Endpoint, quinn::Connection) =
        connect_quic(port, ALPN_HTTP3).await.unwrap();
    let mut decoder: QuicSendStream = connection.open_uni().await.unwrap();
    decoder.write_all(&[0x03, 0x01]).await.unwrap();
    let closed: Result<quinn::ConnectionError, Elapsed> =
        timeout(Duration::from_secs(2), connection.closed()).await;
    assert!(
        matches!(
            closed,
            Ok(quinn::ConnectionError::ApplicationClosed(ref frame))
                if frame.error_code == VarInt::from_u32(0x202)
        ),
        "QPACK decoder insert-count increment when capacity is zero must close with QPACK_DECODER_STREAM_ERROR (0x202), got: {closed:?}"
    );
    endpoint.wait_idle().await;
    control.shutdown().await;
}