diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index f9d0db4c..03a23dbb 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,11 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **fixed:** `sse::Event` will no longer drop the leading space of data, event ID and name values + that have it ([#600]) - **fixed:** `sse::Event` is more strict about what field values it supports, disallowing any SSE events that break the specification (such as field values containing carriage returns) ([#599]) - **added:** `axum::AddExtension::layer` ([#607]) [#599]: https://github.com/tokio-rs/axum/pull/599 +[#600]: https://github.com/tokio-rs/axum/pull/600 [#607]: https://github.com/tokio-rs/axum/pull/607 # 0.4.2 (06. December, 2021) diff --git a/axum/src/response/sse.rs b/axum/src/response/sse.rs index b8dbe23b..8fa77f13 100644 --- a/axum/src/response/sse.rs +++ b/axum/src/response/sse.rs @@ -315,7 +315,7 @@ impl fmt::Display for Event { } if let Some(event) = &self.event { - "event:".fmt(f)?; + "event: ".fmt(f)?; event.fmt(f)?; f.write_char('\n')?; } @@ -323,7 +323,7 @@ impl fmt::Display for Event { match &self.data { Some(DataType::Text(data)) => { for line in data.split('\n') { - "data:".fmt(f)?; + "data: ".fmt(f)?; line.fmt(f)?; f.write_char('\n')?; } @@ -338,7 +338,7 @@ impl fmt::Display for Event { } if let Some(id) = &self.id { - "id:".fmt(f)?; + "id: ".fmt(f)?; id.fmt(f)?; f.write_char('\n')?; } @@ -452,3 +452,12 @@ impl KeepAliveStream { Poll::Ready(event) } } + +#[test] +fn leading_space_is_not_stripped() { + let no_leading_space = Event::default().data("\tfoobar"); + assert_eq!(no_leading_space.to_string(), "data: \tfoobar\n\n"); + + let leading_space = Event::default().data(" foobar"); + assert_eq!(leading_space.to_string(), "data: foobar\n\n"); +}