Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_net/tests/bodiless_status.rs

1.7 KiB, 1 run

created by r1870400018:59921, 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#![cfg(feature = "async")]
2//! A response whose status carries no body must not be framed as though it had one.
3//!
4//! RFC 9110 §8.6 forbids `Content-Length` on a `1xx` or `204`, and on a `304` the field would
5//! have to state the size of the representation not sent. The writer used to add
6//! `Content-Length: 0` to every message it sent, so a `204` for an empty map tile went out
7//! carrying a field the specification forbids.
8//!
9//! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\
10//! Anthropic Claude
11
12use oxedyne_fe2o3_core::prelude::*;
13use oxedyne_fe2o3_net::http::{
14 msg::HttpMessage,
15 status::HttpStatus,
16};
17
18
19async fn wire(msg: HttpMessage) -> Outcome<String> {
20 let mut out: Vec<u8> = Vec::new();
21 res!(msg.write_all(&mut out).await);
22 Ok(String::from_utf8_lossy(&out).to_lowercase())
23}
24
25#[tokio::test]
26async fn no_content_length_on_bodiless_statuses() -> Outcome<()> {
27 for status in [HttpStatus::NoContent, HttpStatus::NotModified, HttpStatus::EarlyHints] {
28 let head = res!(wire(HttpMessage::new_response(status)).await);
29 assert!(!head.contains("content-length"),
30 "a {} response carried a Content-Length:\n{}", status as u16, head);
31 }
32 Ok(())
33}
34
35#[tokio::test]
36async fn content_length_still_frames_other_statuses() -> Outcome<()> {
37 let head = res!(wire(HttpMessage::new_response(HttpStatus::OK)).await);
38 assert!(head.contains("content-length: 0\r\n"),
39 "an empty 200 lost its Content-Length:\n{}", head);
40 let head = res!(wire(HttpMessage::respond_with_text(HttpStatus::NotFound, "gone")).await);
41 assert!(head.contains("content-length: 4\r\n"),
42 "a 404 with a body lost its Content-Length:\n{}", head);
43 Ok(())
44}