oxedyne/fe2o3/fe2o3_steel/tests/http10_close.rs
3.3 KiB, 1 run
created by r1870400018:65943, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Whether Steel ends a connection after the reply, by what the request asked. |
| 2 | //! |
| 3 | //! RFC 9112 section 9.3: a request in HTTP/1.1 is persistent unless it carries `Connection: |
| 4 | //! close`; one in HTTP/1.0 is not persistent unless it carries `Connection: keep-alive`. HTTP/1.0 |
| 5 | //! requests became acceptable to Steel when the library learned the version (a client such as `ab` |
| 6 | //! or `curl -0` is now answered), but the dispatcher kept every connection open for the next |
| 7 | //! request, so such a client waited out the idle timeout on each one, and a `Connection: close` |
| 8 | //! was honoured by the client dropping the line, never by the server. |
| 9 | //! |
| 10 | //! Each case sends one raw request through `handle_https` and watches the connection afterwards. |
| 11 | //! |
| 12 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 13 | //! Anthropic Claude |
| 14 | |
| 15 | mod vhost_rig; |
| 16 | |
| 17 | use vhost_rig::{ |
| 18 | Exchange, |
| 19 | Rig, |
| 20 | Scratch, |
| 21 | Site, |
| 22 | }; |
| 23 | |
| 24 | use oxedyne_fe2o3_core::prelude::*; |
| 25 | |
| 26 | use std::time::Duration; |
| 27 | |
| 28 | const HOST: &str = "close.test"; |
| 29 | |
| 30 | async fn ask(rig: &Rig, request_line: &str, fields: &[&str], patience_ms: u64) -> Outcome<Exchange> { |
| 31 | let mut raw = fmt!("{}\r\nHost: {}\r\n", request_line, HOST); |
| 32 | for f in fields { |
| 33 | raw.push_str(f); |
| 34 | raw.push_str("\r\n"); |
| 35 | } |
| 36 | raw.push_str("\r\n"); |
| 37 | rig.exchange(HOST, &raw, Duration::from_millis(patience_ms)).await |
| 38 | } |
| 39 | |
| 40 | // A rig over one vhost, and one raw request put to it. |
| 41 | fn put(label: &str, request_line: &str, fields: &[&str], patience_ms: u64) -> Outcome<Exchange> { |
| 42 | let web = res!(Scratch::new(label)); |
| 43 | res!(web.put("index.html", "<p>home</p>")); |
| 44 | let runtime = res!(vhost_rig::runtime()); |
| 45 | runtime.block_on(async { |
| 46 | let rig = res!(Rig::new(vec![Site::new(HOST, &web.0)])); |
| 47 | ask(&rig, request_line, fields, patience_ms).await |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn an_http_1_0_request_without_keep_alive_is_closed_after_the_reply() -> Outcome<()> { |
| 53 | let x = res!(put("h10_plain", "GET / HTTP/1.0", &[], 3000)); |
| 54 | assert_eq!(x.reply.status(), 200, "the HTTP/1.0 request must still be answered: {:?}", x.reply.head); |
| 55 | assert!(x.closed, "an HTTP/1.0 request without keep-alive must be closed after the reply"); |
| 56 | Ok(()) |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn an_http_1_0_request_that_asks_for_keep_alive_is_kept_open_and_told_so() -> Outcome<()> { |
| 61 | let x = res!(put("h10_keep", "GET / HTTP/1.0", &["Connection: Keep-Alive"], 600)); |
| 62 | assert_eq!(x.reply.status(), 200); |
| 63 | assert!(!x.closed, "an HTTP/1.0 request that asks for keep-alive must be kept open"); |
| 64 | assert_eq!(x.reply.field("connection").as_deref(), Some("keep-alive"), |
| 65 | "the reply must grant the keep-alive an HTTP/1.0 client asked for: {:?}", x.reply.head); |
| 66 | Ok(()) |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn an_http_1_1_request_is_kept_open_by_default() -> Outcome<()> { |
| 71 | let x = res!(put("h11_plain", "GET / HTTP/1.1", &[], 600)); |
| 72 | assert_eq!(x.reply.status(), 200); |
| 73 | assert!(!x.closed, "an HTTP/1.1 request must be kept open by default"); |
| 74 | Ok(()) |
| 75 | } |
| 76 | |
| 77 | #[test] |
| 78 | fn an_http_1_1_request_with_connection_close_is_closed_by_the_server() -> Outcome<()> { |
| 79 | let x = res!(put("h11_close", "GET / HTTP/1.1", &["Connection: close"], 3000)); |
| 80 | assert_eq!(x.reply.status(), 200); |
| 81 | assert_eq!(x.reply.field("connection").as_deref(), Some("close")); |
| 82 | assert!(x.closed, "an HTTP/1.1 request with Connection: close must be closed after the reply"); |
| 83 | Ok(()) |
| 84 | } |