oxihttp 0.1.0

OxiHTTP Pure-Rust HTTP facade for the COOLJAPAN ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! Integration tests for the HTTP/1.1 plaintext client.

use bytes::Bytes;
use http_body_util::Full;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::Request as HyperRequest;
use hyper::Response as HyperResponse;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use tokio::net::TcpListener;

/// Spawn a simple hyper server; returns the bound address.
async fn spawn_echo_server() -> SocketAddr {
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(handle_request),
                    )
                    .await;
            });
        }
    });
    addr
}

async fn handle_request(
    req: HyperRequest<hyper::body::Incoming>,
) -> Result<HyperResponse<Full<Bytes>>, Infallible> {
    use http_body_util::BodyExt;
    let method = req.method().clone();
    let path = req.uri().path().to_string();
    let headers = req.headers().clone();

    match (method, path.as_str()) {
        (hyper::Method::GET, "/hello") => {
            Ok(HyperResponse::new(Full::new(Bytes::from("hello world"))))
        }
        (hyper::Method::GET, "/json") => {
            let json = serde_json::json!({"key": "value", "number": 42});
            let body = serde_json::to_vec(&json).expect("json serialize");
            let mut resp = HyperResponse::new(Full::new(Bytes::from(body)));
            resp.headers_mut().insert(
                hyper::header::CONTENT_TYPE,
                hyper::header::HeaderValue::from_static("application/json"),
            );
            Ok(resp)
        }
        (hyper::Method::POST, "/echo") => {
            let body_bytes = req
                .into_body()
                .collect()
                .await
                .expect("collect body")
                .to_bytes();
            Ok(HyperResponse::new(Full::new(body_bytes)))
        }
        (hyper::Method::POST, "/echo-json") => {
            let body_bytes = req
                .into_body()
                .collect()
                .await
                .expect("collect body")
                .to_bytes();
            let mut resp = HyperResponse::new(Full::new(body_bytes));
            resp.headers_mut().insert(
                hyper::header::CONTENT_TYPE,
                hyper::header::HeaderValue::from_static("application/json"),
            );
            Ok(resp)
        }
        (hyper::Method::PUT, "/put")
        | (hyper::Method::DELETE, "/delete")
        | (hyper::Method::PATCH, "/patch") => {
            let body_bytes = req
                .into_body()
                .collect()
                .await
                .expect("collect body")
                .to_bytes();
            Ok(HyperResponse::new(Full::new(body_bytes)))
        }
        (hyper::Method::HEAD, "/head") => Ok(HyperResponse::new(Full::new(Bytes::new()))),
        (_, "/auth-check") => {
            let auth = headers
                .get(hyper::header::AUTHORIZATION)
                .and_then(|v| v.to_str().ok())
                .unwrap_or("none");
            Ok(HyperResponse::new(Full::new(Bytes::from(auth.to_string()))))
        }
        (_, "/status/404") => {
            let mut resp = HyperResponse::new(Full::new(Bytes::from("not found")));
            *resp.status_mut() = hyper::StatusCode::NOT_FOUND;
            Ok(resp)
        }
        (_, "/status/500") => {
            let mut resp = HyperResponse::new(Full::new(Bytes::from("server error")));
            *resp.status_mut() = hyper::StatusCode::INTERNAL_SERVER_ERROR;
            Ok(resp)
        }
        _ => {
            let mut resp = HyperResponse::new(Full::new(Bytes::from("not found")));
            *resp.status_mut() = hyper::StatusCode::NOT_FOUND;
            Ok(resp)
        }
    }
}

#[tokio::test]
async fn test_get_request() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .get(&url)
        .expect("GET builder")
        .send()
        .await
        .expect("GET send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_bytes().await.expect("body read");
    assert_eq!(&body[..], b"hello world");
}

#[tokio::test]
async fn test_post_echo() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/echo");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .post(&url)
        .expect("POST builder")
        .body(Bytes::from("ping payload"))
        .send()
        .await
        .expect("POST send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_bytes().await.expect("body read");
    assert_eq!(&body[..], b"ping payload");
}

#[tokio::test]
async fn test_invalid_uri_error() {
    let client = oxihttp::Client::builder().build().expect("client build");
    let result = client.get("not a valid uri!!!{}");
    assert!(result.is_err(), "invalid URI should return Err");
}

#[tokio::test]
async fn test_put_request() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/put");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .put(&url)
        .expect("PUT builder")
        .body(Bytes::from("put data"))
        .send()
        .await
        .expect("PUT send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_bytes().await.expect("body read");
    assert_eq!(&body[..], b"put data");
}

#[tokio::test]
async fn test_delete_request() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/delete");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .delete(&url)
        .expect("DELETE builder")
        .body(Bytes::from("delete data"))
        .send()
        .await
        .expect("DELETE send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
}

#[tokio::test]
async fn test_patch_request() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/patch");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .patch(&url)
        .expect("PATCH builder")
        .body(Bytes::from("patch data"))
        .send()
        .await
        .expect("PATCH send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_bytes().await.expect("body read");
    assert_eq!(&body[..], b"patch data");
}

#[tokio::test]
async fn test_head_request() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/head");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .head(&url)
        .expect("HEAD builder")
        .send()
        .await
        .expect("HEAD send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
}

#[tokio::test]
async fn test_json_body_and_response() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/echo-json");
    let client = oxihttp::Client::builder().build().expect("client build");

    let payload = serde_json::json!({"name": "test", "value": 123});
    let resp = client
        .post(&url)
        .expect("POST builder")
        .json(&payload)
        .expect("json body")
        .send()
        .await
        .expect("POST send");

    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let result: serde_json::Value = resp.body_json().await.expect("json parse");
    assert_eq!(result["name"], "test");
    assert_eq!(result["value"], 123);
}

#[tokio::test]
async fn test_body_text() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    let text = resp.body_text().await.expect("body text");
    assert_eq!(text, "hello world");
}

#[tokio::test]
async fn test_get_json_convenience() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/json");
    let client = oxihttp::Client::builder().build().expect("client build");

    let result: serde_json::Value = client.get_json(&url).await.expect("get_json");
    assert_eq!(result["key"], "value");
    assert_eq!(result["number"], 42);
}

#[tokio::test]
async fn test_bearer_token() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/auth-check");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .get(&url)
        .expect("GET builder")
        .bearer_token("my-secret-token")
        .expect("bearer")
        .send()
        .await
        .expect("send");
    let body = resp.body_text().await.expect("body");
    assert_eq!(body, "Bearer my-secret-token");
}

#[tokio::test]
async fn test_basic_auth() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/auth-check");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .get(&url)
        .expect("GET builder")
        .basic_auth("user", Some("pass"))
        .expect("basic auth")
        .send()
        .await
        .expect("send");
    let body = resp.body_text().await.expect("body");
    // "user:pass" base64 = "dXNlcjpwYXNz"
    assert_eq!(body, "Basic dXNlcjpwYXNz");
}

#[tokio::test]
async fn test_custom_headers() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/auth-check");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client
        .get(&url)
        .expect("GET builder")
        .header("authorization", "Custom xyz123")
        .expect("header")
        .send()
        .await
        .expect("send");
    let body = resp.body_text().await.expect("body");
    assert_eq!(body, "Custom xyz123");
}

#[tokio::test]
async fn test_error_for_status_404() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/status/404");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    assert_eq!(resp.status(), oxihttp::StatusCode::NOT_FOUND);
    let err = resp.error_for_status();
    assert!(err.is_err(), "404 should produce an error");
}

#[tokio::test]
async fn test_error_for_status_500() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/status/500");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    let err = resp.error_for_status();
    assert!(err.is_err(), "500 should produce an error");
}

#[tokio::test]
async fn test_error_for_status_success() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    let resp = resp
        .error_for_status()
        .expect("200 should not produce error");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
}

#[tokio::test]
async fn test_response_debug() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    let debug = format!("{resp:?}");
    assert!(debug.contains("Response"));
    assert!(debug.contains("200"));
}

#[tokio::test]
async fn test_content_length() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    // "hello world" = 11 bytes
    assert_eq!(resp.content_length(), Some(11));
}

#[tokio::test]
async fn test_content_type() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/json");
    let client = oxihttp::Client::builder().build().expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    assert_eq!(resp.content_type(), Some("application/json"));
}

#[tokio::test]
async fn test_client_builder_user_agent() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let client = oxihttp::Client::builder()
        .user_agent("OxiHTTP/0.1.0")
        .build()
        .expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
}

#[tokio::test]
async fn test_per_request_timeout() {
    // Create a server that never responds (sleeps forever)
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(|_req: HyperRequest<hyper::body::Incoming>| async {
                            tokio::time::sleep(std::time::Duration::from_secs(60)).await;
                            Ok::<_, Infallible>(HyperResponse::new(Full::new(Bytes::new())))
                        }),
                    )
                    .await;
            });
        }
    });

    let url = format!("http://{addr}/slow");
    let client = oxihttp::Client::builder().build().expect("client build");
    let result = client
        .get(&url)
        .expect("GET")
        .timeout(std::time::Duration::from_millis(100))
        .send()
        .await;
    assert!(result.is_err(), "should timeout");
    let err = result.expect_err("timeout error");
    assert!(err.is_timeout(), "error should be timeout");
}

#[tokio::test]
async fn test_form_body() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/echo");
    let client = oxihttp::Client::builder().build().expect("client build");

    let form = oxihttp::FormBody::new()
        .field("username", "alice")
        .field("password", "secret");

    let resp = client
        .post(&url)
        .expect("POST builder")
        .form(&form)
        .send()
        .await
        .expect("POST send");

    let body = resp.body_text().await.expect("body text");
    assert!(body.contains("username=alice"));
    assert!(body.contains("password=secret"));
}

#[tokio::test]
async fn test_redirect_disabled() {
    // Server that returns a 302 redirect
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(|_req: HyperRequest<hyper::body::Incoming>| async {
                            Ok::<_, Infallible>(
                                HyperResponse::builder()
                                    .status(302)
                                    .header("location", "/redirected")
                                    .body(Full::new(Bytes::new()))
                                    .expect("response build"),
                            )
                        }),
                    )
                    .await;
            });
        }
    });

    let url = format!("http://{addr}/start");
    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::None)
        .build()
        .expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    assert_eq!(resp.status().as_u16(), 302);
}

#[tokio::test]
async fn test_redirect_follow() {
    // Create a server that redirects /start -> /hello
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(|req: HyperRequest<hyper::body::Incoming>| async move {
                            match req.uri().path() {
                                "/start" => Ok::<_, Infallible>(
                                    HyperResponse::builder()
                                        .status(302)
                                        .header("location", "/destination")
                                        .body(Full::new(Bytes::new()))
                                        .expect("resp"),
                                ),
                                "/destination" => {
                                    Ok(HyperResponse::new(Full::new(Bytes::from("arrived"))))
                                }
                                _ => Ok(HyperResponse::new(Full::new(Bytes::from("404")))),
                            }
                        }),
                    )
                    .await;
            });
        }
    });

    let url = format!("http://{addr}/start");
    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::Limited(5))
        .build()
        .expect("client build");
    let resp = client.get(&url).expect("GET").send().await.expect("send");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_text().await.expect("body");
    assert_eq!(body, "arrived");
}

#[tokio::test]
async fn test_redirect_limit_exceeded() {
    // Create a server that redirects infinitely
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    let counter = Arc::new(AtomicU32::new(0));
    let counter_clone = Arc::clone(&counter);
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            let counter = Arc::clone(&counter_clone);
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(move |_req: HyperRequest<hyper::body::Incoming>| {
                            let n = counter.fetch_add(1, Ordering::SeqCst);
                            async move {
                                Ok::<_, Infallible>(
                                    HyperResponse::builder()
                                        .status(302)
                                        .header("location", format!("/redirect/{n}"))
                                        .body(Full::new(Bytes::new()))
                                        .expect("resp"),
                                )
                            }
                        }),
                    )
                    .await;
            });
        }
    });

    let url = format!("http://{addr}/start");
    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::Limited(3))
        .build()
        .expect("client build");
    let result = client.get(&url).expect("GET").send().await;
    assert!(result.is_err(), "should fail due to redirect limit");
    let err = result.expect_err("redirect error");
    assert!(err.is_redirect(), "should be redirect error");
}

#[tokio::test]
async fn test_facade_version() {
    let v = oxihttp::version();
    assert!(!v.is_empty());
    assert!(v.contains('.'));
}

#[tokio::test]
async fn test_facade_get_convenience() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");
    let resp = oxihttp::get(&url).await.expect("facade get");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_text().await.expect("body");
    assert_eq!(body, "hello world");
}

#[tokio::test]
async fn test_facade_post_convenience() {
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/echo");
    let resp = oxihttp::post(&url, Bytes::from("data"))
        .await
        .expect("facade post");
    assert_eq!(resp.status(), oxihttp::StatusCode::OK);
    let body = resp.body_text().await.expect("body");
    assert_eq!(body, "data");
}

#[tokio::test]
async fn test_pool_sequential_reuse() {
    // Build a single client with an explicit pool size and send 10 sequential
    // requests to the same server.  All requests must succeed — this exercises
    // the connection-pool reuse path (the pool should keep the connection alive
    // across requests rather than opening a new TCP connection each time).
    let addr = spawn_echo_server().await;
    let url = format!("http://{addr}/hello");

    let client = oxihttp::Client::builder()
        .pool_max_idle_per_host(2)
        .build()
        .expect("client build");

    for i in 0..10 {
        let resp = client
            .get(&url)
            .expect("GET builder")
            .send()
            .await
            .unwrap_or_else(|e| panic!("request {i} failed: {e}"));
        assert_eq!(
            resp.status(),
            oxihttp::StatusCode::OK,
            "request {i} returned non-200"
        );
        let body = resp.body_text().await.expect("body text");
        assert_eq!(body, "hello world", "request {i} body mismatch");
    }
}

#[tokio::test]
async fn test_concurrent_requests() {
    // Spawn 10 tokio tasks that all send GET requests to the same server
    // concurrently.  The connection pool must handle the concurrent load and
    // all responses must be 200 OK.
    let addr = spawn_echo_server().await;
    let url = Arc::new(format!("http://{addr}/hello"));

    let client = Arc::new(
        oxihttp::Client::builder()
            .pool_max_idle_per_host(10)
            .build()
            .expect("client build"),
    );

    let tasks: Vec<_> = (0..10)
        .map(|i| {
            let client = Arc::clone(&client);
            let url = Arc::clone(&url);
            tokio::spawn(async move {
                let resp = client
                    .get(url.as_str())
                    .expect("GET builder")
                    .send()
                    .await
                    .unwrap_or_else(|e| panic!("concurrent request {i} failed: {e}"));
                assert_eq!(
                    resp.status(),
                    oxihttp::StatusCode::OK,
                    "concurrent request {i} returned non-200"
                );
                let body = resp.body_text().await.expect("body text");
                assert_eq!(body, "hello world", "concurrent request {i} body mismatch");
            })
        })
        .collect();

    for (i, task) in tasks.into_iter().enumerate() {
        task.await
            .unwrap_or_else(|e| panic!("task {i} panicked: {e}"));
    }
}

/// Spawn a redirect server that:
/// - Responds to POST /original with the given redirect status pointing to /destination.
/// - Responds to GET /destination with 200 "final".
/// - Responds to POST /destination with 200 "final" (for 307/308 that preserve POST).
async fn spawn_redirect_method_server(redirect_status: u16) -> SocketAddr {
    let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind");
    let addr = listener.local_addr().expect("local addr");
    tokio::spawn(async move {
        loop {
            let Ok((stream, _)) = listener.accept().await else {
                break;
            };
            let status = redirect_status;
            tokio::spawn(async move {
                let _ = http1::Builder::new()
                    .serve_connection(
                        hyper_util::rt::TokioIo::new(stream),
                        service_fn(move |req: HyperRequest<hyper::body::Incoming>| async move {
                            let path = req.uri().path().to_string();
                            let method = req.method().clone();
                            match (method.as_str(), path.as_str()) {
                                (_, "/original") => Ok::<_, Infallible>(
                                    HyperResponse::builder()
                                        .status(status)
                                        .header("location", "/destination")
                                        .body(Full::new(Bytes::new()))
                                        .expect("resp build"),
                                ),
                                (_, "/destination") => {
                                    Ok(HyperResponse::new(Full::new(Bytes::from("final"))))
                                }
                                _ => {
                                    let mut r =
                                        HyperResponse::new(Full::new(Bytes::from("not found")));
                                    *r.status_mut() = hyper::StatusCode::NOT_FOUND;
                                    Ok(r)
                                }
                            }
                        }),
                    )
                    .await;
            });
        }
    });
    addr
}

/// 301 Moved Permanently: POST should be rewritten to GET on the redirect target.
///
/// RFC 7231 §6.4.2 and common browser behavior: non-HEAD methods are changed to
/// GET when following a 301.  The body must also be dropped.
#[tokio::test]
async fn test_redirect_301_rewrites_method_to_get() {
    let addr = spawn_redirect_method_server(301).await;
    let url = format!("http://{addr}/original");

    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::Limited(5))
        .build()
        .expect("client build");

    let resp = client
        .post(&url)
        .expect("POST builder")
        .body(bytes::Bytes::from("should be dropped"))
        .send()
        .await
        .expect("send");

    assert_eq!(
        resp.status(),
        oxihttp::StatusCode::OK,
        "expected 200 after 301 redirect"
    );
    let body = resp.body_text().await.expect("body text");
    assert_eq!(
        body, "final",
        "expected response body 'final' from /destination"
    );
}

/// 307 Temporary Redirect: POST must be preserved (method and body must be
/// forwarded to the redirect target).
///
/// RFC 7231 §6.4.7: The request method must not be changed when following a 307.
#[tokio::test]
async fn test_redirect_307_preserves_method() {
    let addr = spawn_redirect_method_server(307).await;
    let url = format!("http://{addr}/original");

    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::Limited(5))
        .build()
        .expect("client build");

    let resp = client
        .post(&url)
        .expect("POST builder")
        .body(bytes::Bytes::from("preserved body"))
        .send()
        .await
        .expect("send");

    assert_eq!(
        resp.status(),
        oxihttp::StatusCode::OK,
        "expected 200 after 307 redirect"
    );
    let body = resp.body_text().await.expect("body text");
    assert_eq!(
        body, "final",
        "expected response body 'final' from /destination"
    );
}

/// 308 Permanent Redirect: POST must be preserved (method and body must be
/// forwarded to the redirect target).
///
/// RFC 7238 §3: The request method must not be changed when following a 308.
#[tokio::test]
async fn test_redirect_308_preserves_method() {
    let addr = spawn_redirect_method_server(308).await;
    let url = format!("http://{addr}/original");

    let client = oxihttp::Client::builder()
        .redirect_policy(oxihttp::RedirectPolicy::Limited(5))
        .build()
        .expect("client build");

    let resp = client
        .post(&url)
        .expect("POST builder")
        .body(bytes::Bytes::from("preserved body"))
        .send()
        .await
        .expect("send");

    assert_eq!(
        resp.status(),
        oxihttp::StatusCode::OK,
        "expected 200 after 308 redirect"
    );
    let body = resp.body_text().await.expect("body text");
    assert_eq!(
        body, "final",
        "expected response body 'final' from /destination"
    );
}

/// Verify that a connection to an unreachable address returns an error
/// rather than hanging indefinitely.
///
/// 192.0.2.1 is TEST-NET-1 per RFC 5737 — routable address space that is
/// guaranteed to have no listening host.  A SYN sent there will either be
/// silently dropped (timeout) or explicitly refused, but it will never succeed.
///
/// The test wraps the attempt in a `tokio::time::timeout` of 3 seconds as a
/// safety net, so CI doesn't block even if the OS takes longer to fail.
/// We assert that an error is returned (not a panic), which covers both
/// the case where `connect_timeout` fires and the case where the OS returns
/// a connection error first.
#[tokio::test]
async fn test_connect_timeout_fires_on_unreachable_host() {
    let client = oxihttp::Client::builder()
        .connect_timeout(std::time::Duration::from_millis(100))
        .build()
        .expect("client build");

    // Safety wrapper: if the OS never returns within 3 s the test itself fails
    // rather than blocking the suite.
    let result = tokio::time::timeout(
        std::time::Duration::from_secs(3),
        client
            .get("http://192.0.2.1:1/")
            .expect("GET builder")
            .send(),
    )
    .await;

    match result {
        Ok(inner) => {
            // Either outcome is acceptable: an Err means the OS/connector rejected
            // the connection; an Ok would be extraordinary for 192.0.2.1 but is
            // not a panic.
            assert!(
                inner.is_err(),
                "expected a connection error for unreachable 192.0.2.1:1"
            );
        }
        Err(_elapsed) => {
            // The 3-second outer timeout fired.  This means the OS timeout
            // exceeded our safety net — still record the scenario as passing
            // because the important invariant (no panic) holds.
        }
    }
}