aria2-core 0.3.13

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
//! Tests for HTTP proxy module.

use std::time::Duration;

use base64::{Engine, engine::general_purpose};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};

use crate::http::header_processor::HttpHeaderProcessor;
use crate::http::proxy::auth::build_proxy_auth_header;
use crate::http::proxy::config::HttpProxyConfig;
use crate::http::proxy::forward::{HttpProxyForward, forward_get_with_auth};
use crate::http::proxy::response::ProxyResponse;
use crate::http::proxy::tunnel::HttpProxyTunnel;
use crate::http::socks_connector::ProxyUrl;

async fn read_request_head(stream: &mut TcpStream) -> Vec<u8> {
    let mut request = Vec::with_capacity(512);
    loop {
        let mut byte = [0; 1];
        stream.read_exact(&mut byte).await.unwrap();
        request.push(byte[0]);
        if request.ends_with(b"\r\n\r\n") {
            return request;
        }
        assert!(
            request.len() <= 16 * 1024,
            "request header exceeded test limit"
        );
    }
}

// ==================== HttpProxyConfig tests ====================

#[test]
fn test_proxy_config_new() {
    let config = HttpProxyConfig::new(
        "proxy.example.com".into(),
        3128,
        "target.example.com".into(),
        443,
    );
    assert_eq!(config.proxy_host, "proxy.example.com");
    assert_eq!(config.proxy_port, 3128);
    assert_eq!(config.target_host, "target.example.com");
    assert_eq!(config.target_port, 443);
    assert!(config.proxy_username.is_none());
    assert!(config.proxy_password.is_none());
    assert_eq!(config.connect_timeout, Duration::from_secs(30));
    assert_eq!(config.read_timeout, Duration::from_secs(60));
}

#[test]
fn test_proxy_config_with_credentials() {
    let config = HttpProxyConfig::new("p".into(), 8080, "t".into(), 80)
        .with_credentials("user".into(), "pass".into());
    assert_eq!(config.proxy_username.as_deref(), Some("user"));
    assert_eq!(config.proxy_password.as_deref(), Some("pass"));
}

#[test]
fn test_proxy_config_from_proxy_url_http() {
    let config = HttpProxyConfig::from_proxy_url(
        "http://admin:secret@proxy.corp.com:3128",
        "target.com".into(),
        443,
    )
    .unwrap();
    assert_eq!(config.proxy_host, "proxy.corp.com");
    assert_eq!(config.proxy_port, 3128);
    assert_eq!(config.proxy_username.as_deref(), Some("admin"));
    assert_eq!(config.proxy_password.as_deref(), Some("secret"));
    assert_eq!(config.target_host, "target.com");
    assert_eq!(config.target_port, 443);
}

#[test]
fn test_proxy_config_from_proxy_url_decodes_encoded_credentials() {
    let config = HttpProxyConfig::from_proxy_url(
        "http://user%40name:p%3A%40%25@[2001:db8::1]:3128",
        "target.com".into(),
        443,
    )
    .unwrap();

    assert_eq!(config.proxy_host, "2001:db8::1");
    assert_eq!(config.proxy_port, 3128);
    assert_eq!(config.proxy_username.as_deref(), Some("user@name"));
    assert_eq!(config.proxy_password.as_deref(), Some("p:@%"));
}

#[test]
fn test_proxy_config_from_proxy_url_https() {
    let config =
        HttpProxyConfig::from_proxy_url("https://secure.proxy.com", "target.com".into(), 443)
            .unwrap();
    assert_eq!(config.proxy_host, "secure.proxy.com");
    assert_eq!(config.proxy_port, 443); // default HTTPS port
}

#[test]
fn test_proxy_config_from_proxy_url_no_credentials() {
    let config =
        HttpProxyConfig::from_proxy_url("http://proxy.local:8080", "t".into(), 80).unwrap();
    assert!(config.proxy_username.is_none());
    assert!(config.proxy_password.is_none());
}

#[test]
fn proxy_config_ignores_at_signs_outside_proxy_authority() {
    let config = HttpProxyConfig::from_proxy_url(
        "http://proxy.local/path@marker?next=@query#fragment",
        "target.example".into(),
        443,
    )
    .expect("proxy path, query, and fragment should not affect its authority");

    assert_eq!(config.proxy_host, "proxy.local");
    assert_eq!(config.proxy_port, 80);
    assert!(config.proxy_username.is_none());
    assert!(config.proxy_password.is_none());
}

#[test]
fn test_proxy_config_from_proxy_url_socks5_accepted() {
    // SOCKS5 proxy URLs are now supported (via socks_connector)
    let result = HttpProxyConfig::from_proxy_url("socks5://proxy.local:1080", "t".into(), 80);
    assert!(
        result.is_ok(),
        "SOCKS5 proxy should be accepted: {:?}",
        result.err()
    );
    let config = result.unwrap();
    assert_eq!(config.proxy_host, "proxy.local");
    assert_eq!(config.proxy_port, 1080);
    assert!(config.is_socks());
}

#[test]
fn invalid_proxy_url_errors_do_not_expose_userinfo() {
    let error = HttpProxyConfig::from_proxy_url(
        "unknown://private-user:private-password@proxy.example:3128",
        "target.example".into(),
        80,
    )
    .expect_err("unsupported proxy schemes should fail");
    let error = error.to_string();

    assert!(!error.contains("private-user"));
    assert!(!error.contains("private-password"));
}

#[test]
fn test_proxy_config_target_host_port() {
    let config = HttpProxyConfig::new("p".into(), 3128, "t".into(), 443);
    assert_eq!(config.target_host_port(), "t:443");
}

// ==================== ProxyResponse tests ====================

#[test]
fn test_proxy_response_200_connected() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 200 Connection established\r\n\r\n");
    let head = proc.get_result().unwrap();

    let resp = ProxyResponse::from_head(head);
    match resp {
        ProxyResponse::Connected(h) => {
            assert_eq!(h.status_code, 200);
        }
        _ => panic!("Expected Connected, got {:?}", resp),
    }
}

#[test]
fn test_proxy_response_407_auth_required() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"Proxy\"\r\n\r\n");
    let head = proc.get_result().unwrap();

    let resp = ProxyResponse::from_head(head);
    match resp {
        ProxyResponse::AuthRequired { response } => {
            assert_eq!(response.status_code, 407);
            assert_eq!(
                response.header("proxy-authenticate"),
                Some("Basic realm=\"Proxy\"")
            );
        }
        _ => panic!("Expected AuthRequired, got {:?}", resp),
    }
}

#[test]
fn test_proxy_response_error_status() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 403 Forbidden\r\n\r\n");
    let head = proc.get_result().unwrap();

    let resp = ProxyResponse::from_head(head);
    match resp {
        ProxyResponse::Error {
            status_code,
            reason,
        } => {
            assert_eq!(status_code, 403);
            assert_eq!(reason, "Forbidden");
        }
        _ => panic!("Expected Error, got {:?}", resp),
    }
}

#[test]
fn test_proxy_response_500_error() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 500 Internal Server Error\r\n\r\n");
    let head = proc.get_result().unwrap();

    let resp = ProxyResponse::from_head(head);
    match resp {
        ProxyResponse::Error {
            status_code,
            reason,
        } => {
            assert_eq!(status_code, 500);
            assert_eq!(reason, "Internal Server Error");
        }
        _ => panic!("Expected Error, got {:?}", resp),
    }
}

// ==================== Auth header builder tests ====================

#[test]
fn test_proxy_basic_auth_encoding() {
    let auth = aria2_protocol::http::auth::basic_authorization("user", "pass");
    assert!(auth.starts_with("Basic "));
    // Verify Base64: "user:pass" -> "dXNlcjpwYXNz"
    assert_eq!(auth, "Basic dXNlcjpwYXNz");
}

#[test]
fn test_proxy_basic_auth_special_chars() {
    let auth = aria2_protocol::http::auth::basic_authorization("admin@corp", "p@ss:w0rd");
    assert!(auth.starts_with("Basic "));
    // Decode to verify
    let encoded = &auth["Basic ".len()..];
    let decoded = String::from_utf8(
        general_purpose::STANDARD
            .decode(encoded)
            .unwrap_or_default(),
    )
    .unwrap_or_default();
    assert_eq!(decoded, "admin@corp:p@ss:w0rd");
}

#[test]
fn test_build_proxy_auth_header_basic() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\nProxy-Authenticate: Basic realm=\"Proxy\"\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1);
    assert!(auth.is_some());
    let auth = auth.unwrap();
    assert!(auth.starts_with("Basic "));
}

#[test]
fn test_build_proxy_auth_header_digest() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\nProxy-Authenticate: Digest realm=\"Proxy\", nonce=\"abc123\", qop=\"auth\"\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1);
    assert!(auth.is_some());
    let auth = auth.unwrap();
    assert!(auth.starts_with("Digest "));
    assert!(auth.contains(r#"username="user""#));
}

#[test]
fn test_build_proxy_auth_header_digest_preferred_over_basic() {
    // When both Digest and Basic are offered, Digest should be preferred
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\nProxy-Authenticate: Basic realm=\"Proxy\"\r\nProxy-Authenticate: Digest realm=\"Proxy\", nonce=\"abc123\", qop=\"auth\"\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1);
    assert!(auth.is_some());
    let auth = auth.unwrap();
    // Digest should be preferred over Basic
    assert!(auth.starts_with("Digest "));
}

#[test]
fn test_build_proxy_auth_header_handles_mixed_case_digest_scheme() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\nProxy-Authenticate: dIgEsT realm=\"proxy, internal\", nonce=\"n1\", qop=\"auth-int\", algorithm=SHA-256\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1)
        .expect("mixed-case Digest challenge should be recognized");
    assert!(auth.starts_with("Digest "));
    assert!(auth.contains("realm=\"proxy, internal\""));
    assert!(auth.contains("qop=auth-int"));
    assert!(auth.contains("algorithm=SHA-256"));
}

#[test]
fn test_build_proxy_auth_header_no_auth_header() {
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1);
    // No Proxy-Authenticate header -> None
    assert!(auth.is_none());
}

// ==================== HttpProxyForward request building tests ====================

#[test]
fn test_forward_build_request_no_auth() {
    let config = HttpProxyConfig::new("proxy.com".into(), 3128, "target.com".into(), 80);
    let forward = HttpProxyForward::new(config);
    let req = forward.build_forward_request("GET", "http://target.com:80/path", "/path", None);

    assert!(req.starts_with("GET http://target.com:80/path HTTP/1.1\r\n"));
    assert!(req.contains("Host: target.com:80\r\n"));
    assert!(!req.contains("Proxy-Authorization"));
    assert!(req.contains(&format!("User-Agent: {}\r\n", crate::constants::USER_AGENT)));
    assert!(req.ends_with("\r\n\r\n"));
}

#[test]
fn validated_forward_request_builder_rejects_request_target_injection() {
    let config = HttpProxyConfig::new("proxy.com".into(), 3128, "target.com".into(), 80);
    let forward = HttpProxyForward::new(config);
    let result = forward.try_build_forward_request(
        "GET",
        "http://target.com/file\r\nX-Injected: yes",
        "/file",
        None,
    );

    assert!(matches!(
        result,
        Err(crate::error::Aria2Error::InvalidArgument(_))
    ));
}

#[test]
fn test_forward_build_request_with_auth() {
    let config = HttpProxyConfig::new("proxy.com".into(), 3128, "target.com".into(), 80);
    let forward = HttpProxyForward::new(config);
    let req = forward.build_forward_request(
        "GET",
        "http://target.com:80/file.zip",
        "/file.zip",
        Some("Basic dXNlcjpwYXNz"),
    );

    assert!(req.contains("Proxy-Authorization: Basic dXNlcjpwYXNz\r\n"));
}

#[test]
fn test_forward_build_head_request() {
    let config = HttpProxyConfig::new("proxy.com".into(), 3128, "target.com".into(), 80);
    let forward = HttpProxyForward::new(config);
    let req = forward.build_forward_request("HEAD", "http://target.com:80/", "/", None);

    assert!(req.starts_with("HEAD http://target.com:80/ HTTP/1.1\r\n"));
}

#[test]
fn test_forward_get_credentials() {
    let config = HttpProxyConfig::new("p".into(), 3128, "t".into(), 80)
        .with_credentials("admin".into(), "s3cret".into());
    let forward = HttpProxyForward::new(config);
    let (u, p) = forward.get_credentials().unwrap();
    assert_eq!(u, "admin");
    assert_eq!(p, "s3cret");
}

// ==================== proxy_digest_auth tests ====================

#[test]
fn test_proxy_digest_auth_produces_header() {
    let challenge = aria2_protocol::http::digest::DigestChallenge::parse(
        r#"Digest realm="Proxy", nonce="abc123", qop="auth""#,
    )
    .unwrap();
    let auth = aria2_protocol::http::digest::DigestResponse::compute(
        "user",
        "pass",
        "CONNECT",
        "target.com:443",
        &challenge,
        1,
    )
    .to_header_value();
    assert!(auth.starts_with("Digest "));
    assert!(auth.contains(r#"username="user""#));
    assert!(auth.contains(r#"realm="Proxy""#));
    assert!(auth.contains("nc=00000001"));
}

// ==================== ProxyUrl integration tests ====================

#[test]
fn test_proxy_url_http_default_port() {
    let parsed = ProxyUrl::parse("http://proxy.local").unwrap();
    assert_eq!(parsed.port, 80);
}

#[test]
fn test_proxy_url_https_default_port() {
    let parsed = ProxyUrl::parse("https://proxy.local").unwrap();
    assert_eq!(parsed.port, 443);
}

#[test]
fn test_proxy_url_with_auth() {
    let parsed = ProxyUrl::parse("http://u:p@proxy.local:3128").unwrap();
    assert_eq!(parsed.username, Some("u".to_string()));
    assert_eq!(parsed.password, Some("p".to_string()));
}

// ==================== Timeout configuration tests ====================

#[test]
fn test_custom_timeouts() {
    let mut config = HttpProxyConfig::new("p".into(), 3128, "t".into(), 443);
    config.connect_timeout = Duration::from_secs(10);
    config.read_timeout = Duration::from_secs(30);
    config.write_timeout = Duration::from_secs(30);
    assert_eq!(config.connect_timeout, Duration::from_secs(10));
    assert_eq!(config.read_timeout, Duration::from_secs(30));
    assert_eq!(config.write_timeout, Duration::from_secs(30));
}

// ==================== Edge case tests ====================

#[test]
fn test_proxy_response_non_standard_2xx() {
    // Some proxies return 200 with different reason phrases
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 200 Tunnel Connection Established\r\n\r\n");
    let head = proc.get_result().unwrap();

    let resp = ProxyResponse::from_head(head);
    assert!(matches!(resp, ProxyResponse::Connected(_)));
}

#[test]
fn test_build_proxy_auth_header_fallback_to_basic() {
    // Unknown scheme should fall back to Basic
    let mut proc = HttpHeaderProcessor::new();
    proc.feed(b"HTTP/1.1 407 Auth\r\nProxy-Authenticate: NTLM\r\n\r\n");
    let head = proc.get_result().unwrap();

    let auth = build_proxy_auth_header(&head, "user", "pass", "CONNECT", "t:443", 1);
    assert!(auth.is_some());
    let auth = auth.unwrap();
    // Should fall back to Basic
    assert!(auth.starts_with("Basic "));
}

#[tokio::test]
async fn public_connect_tunnel_uses_protocol_handshake_and_preserves_request() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let (mut stream, _) = listener.accept().await.unwrap();
        let request = read_request_head(&mut stream).await;
        assert_eq!(
            request.as_slice(),
            b"CONNECT target.example:443 HTTP/1.1\r\nHost: target.example:443\r\nProxy-Connection: keep-alive\r\n\r\n"
        );
        stream
            .write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
            .await
            .unwrap();
    });

    let mut config = HttpProxyConfig::new(
        "127.0.0.1".into(),
        address.port(),
        "target.example".into(),
        443,
    );
    config.connect_timeout = Duration::from_secs(2);
    config.read_timeout = Duration::from_secs(2);
    config.write_timeout = Duration::from_secs(2);
    let tunnel = HttpProxyTunnel::new(config);
    let stream = tunnel.connect().await.unwrap();
    drop(stream);
    server.await.unwrap();
}

#[test]
fn test_forward_request_target_port_80() {
    let config = HttpProxyConfig::new("proxy.local".into(), 8080, "example.com".into(), 80);
    let forward = HttpProxyForward::new(config);
    let req = forward.build_forward_request(
        "GET",
        "http://example.com:80/index.html",
        "/index.html",
        None,
    );

    assert!(req.starts_with("GET http://example.com:80/index.html HTTP/1.1\r\n"));
    assert!(req.contains("Host: example.com:80\r\n"));
}

#[tokio::test]
async fn forward_get_authentication_preserves_response_body_after_headers() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let (mut first, _) = listener.accept().await.unwrap();
        let request = read_request_head(&mut first).await;
        assert!(request.starts_with(b"GET http://example.com:80/file HTTP/1.1\r\n"));
        first
            .write_all(
                b"HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"proxy\"\r\nContent-Length: 0\r\n\r\n",
            )
            .await
            .unwrap();
        drop(first);

        let (mut second, _) = listener.accept().await.unwrap();
        let request = read_request_head(&mut second).await;
        assert!(
            request
                .windows(b"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n".len())
                .any(|window| window == b"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n")
        );
        second
            .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nBODY")
            .await
            .unwrap();
    });

    let mut config =
        HttpProxyConfig::new("127.0.0.1".into(), address.port(), "example.com".into(), 80)
            .with_credentials("user".into(), "pass".into());
    config.connect_timeout = Duration::from_secs(2);
    config.read_timeout = Duration::from_secs(2);
    config.write_timeout = Duration::from_secs(2);

    let (mut stream, response) = forward_get_with_auth(&config, "/file").await.unwrap();
    assert_eq!(response.status_code, 200);
    let mut body = [0; 4];
    stream.read_exact(&mut body).await.unwrap();
    assert_eq!(&body, b"BODY");
    server.await.unwrap();
}

#[tokio::test]
async fn forward_get_rejects_injected_path_before_connecting_to_proxy() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let config = HttpProxyConfig::new("127.0.0.1".into(), address.port(), "example.com".into(), 80);

    let result = forward_get_with_auth(&config, "/file\r\nX-Injected: yes").await;
    assert!(matches!(
        result,
        Err(crate::error::Aria2Error::InvalidArgument(_))
    ));
    assert!(
        tokio::time::timeout(Duration::from_millis(50), listener.accept())
            .await
            .is_err()
    );
}

#[tokio::test]
async fn forward_connect_authenticates_probe_then_returns_fresh_stream() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let server = tokio::spawn(async move {
        let (mut first, _) = listener.accept().await.unwrap();
        let request = read_request_head(&mut first).await;
        assert!(request.starts_with(b"HEAD http://example.com:80 HTTP/1.1\r\n"));
        assert!(
            !request
                .windows(b"Proxy-Authorization:".len())
                .any(|window| window == b"Proxy-Authorization:")
        );
        first
            .write_all(
                b"HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"proxy\"\r\nContent-Length: 0\r\n\r\n",
            )
            .await
            .unwrap();
        drop(first);

        let (mut second, _) = listener.accept().await.unwrap();
        let request = read_request_head(&mut second).await;
        assert!(
            request
                .windows(b"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n".len())
                .any(|window| window == b"Proxy-Authorization: Basic dXNlcjpwYXNz\r\n")
        );
        second
            .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")
            .await
            .unwrap();
        drop(second);

        listener.accept().await.unwrap()
    });

    let mut config =
        HttpProxyConfig::new("127.0.0.1".into(), address.port(), "example.com".into(), 80)
            .with_credentials("user".into(), "pass".into());
    config.connect_timeout = Duration::from_secs(2);
    config.read_timeout = Duration::from_secs(2);
    config.write_timeout = Duration::from_secs(2);

    let stream = HttpProxyForward::new(config).connect(false).await.unwrap();
    drop(stream);
    server.await.unwrap();
}