diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
| commit | 7175543e00caa9ce35487fa7f8b1b479c1ba00a2 (patch) | |
| tree | 10f89f85ca35d6ac7515bbe88518255788d6dc22 /tests/sse.rs | |
| download | agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.tar.gz agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.zip | |
M0: streaming provider, SSE decoder, mock provider, i686 build gate
Diffstat (limited to 'tests/sse.rs')
| -rw-r--r-- | tests/sse.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/sse.rs b/tests/sse.rs new file mode 100644 index 0000000..7994a0a --- /dev/null +++ b/tests/sse.rs | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | use agent686::provider::SseDecoder; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn reassembles_lines_split_at_every_byte() { | ||
| 5 | // Fed one byte at a time, so the decoder sees a split after every character, | ||
| 6 | // including in the middle of the multi-byte ones. | ||
| 7 | let wire = "data: {\"a\":\"café ✓\"}\n\ndata: [DONE]\n\n"; | ||
| 8 | let mut decoder = SseDecoder::new(); | ||
| 9 | let mut got = Vec::new(); | ||
| 10 | for byte in wire.as_bytes() { | ||
| 11 | got.extend(decoder.feed(std::slice::from_ref(byte))); | ||
| 12 | } | ||
| 13 | assert_eq!(got, vec!["{\"a\":\"café ✓\"}".to_string(), "[DONE]".to_string()]); | ||
| 14 | } | ||
| 15 | |||
| 16 | #[test] | ||
| 17 | fn handles_crlf_comments_and_event_lines() { | ||
| 18 | let wire = ": keep-alive\r\nevent: message\r\ndata:{\"x\":1}\r\n\r\n"; | ||
| 19 | let mut decoder = SseDecoder::new(); | ||
| 20 | let got = decoder.feed(wire.as_bytes()); | ||
| 21 | assert_eq!(got, vec!["{\"x\":1}".to_string()]); | ||
| 22 | } | ||
| 23 | |||
| 24 | #[test] | ||
| 25 | fn keeps_incomplete_line_until_finished() { | ||
| 26 | let mut decoder = SseDecoder::new(); | ||
| 27 | assert!(decoder.feed(b"data: {\"x\"").is_empty()); | ||
| 28 | assert!(decoder.feed(b":1}").is_empty()); | ||
| 29 | assert_eq!( | ||
| 30 | decoder.feed(b"\n"), | ||
| 31 | vec!["{\"x\":1}".to_string()] | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | |||
| 35 | #[test] | ||
| 36 | fn ignores_non_data_lines() { | ||
| 37 | let mut decoder = SseDecoder::new(); | ||
| 38 | let got = decoder.feed(b"id: 1\nretry: 2000\n\n"); | ||
| 39 | assert!(got.is_empty()); | ||
| 40 | } | ||
