Skip to content

Commit

Permalink
Add a property-test to exercise parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Mar 28, 2022
1 parent eda88f5 commit c746827
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions eventsource-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ maplit = "1.0.1"
simplelog = "0.5.3"
tokio = { version = "1.2.0", features = ["macros", "rt-multi-thread"] }
test-case = "1.2.3"
proptest = "1.0.0"


[features]
default = ["rustls"]
Expand Down
15 changes: 15 additions & 0 deletions eventsource-client/src/event_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ impl EventParser {

if let Some(incomplete_line) = self.incomplete_line.as_mut() {
if let Some(line) = lines.next() {
assert!(
!line.is_empty(),
"split_inclusive should never yield an empty line"
);

trace!(
"extending line from previous chunk: {:?}+{:?}",
logify(incomplete_line),
Expand Down Expand Up @@ -370,6 +375,7 @@ impl EventParser {
#[cfg(test)]
mod tests {
use super::{Error::*, *};
use proptest::proptest;
use test_case::test_case;

fn field<'a>(key: &'a str, value: &'a str) -> Result<Option<(&'a str, &'a str)>> {
Expand Down Expand Up @@ -666,4 +672,13 @@ mod tests {
std::fs::read(format!("test-data/{}", name))
.unwrap_or_else(|_| panic!("couldn't read {}", name))
}

proptest! {
#[test]
fn test_decode_and_buffer_lines_does_not_crash(next in "(\r\n|\r|\n)*event: [^\n\r:]*(\r\n|\r|\n)", previous in "(\r\n|\r|\n)*event: [^\n\r:]*(\r\n|\r|\n)") {
let mut parser = EventParser::new();
parser.incomplete_line = Some(previous.as_bytes().to_vec());
parser.decode_and_buffer_lines(Bytes::from(next));
}
}
}

0 comments on commit c746827

Please sign in to comment.