Some testing and fixing receiving messages over SSE

This commit is contained in:
2026-06-14 10:05:23 -04:00
parent 7cdd7aecad
commit 0dd2919fad
4 changed files with 188 additions and 36 deletions
+64 -15
View File
@@ -60,24 +60,73 @@
(ert-deftest noise-sse--filter-processes-complete-event ()
"Process filter parses and calls handler for complete events."
(let ((processed-events '()))
(cl-letf (((symbol-function 'noise-sse--handle-event)
(lambda (event) (push event processed-events))))
(let ((noise-sse--buffer ""))
(noise-sse--filter nil "data: {\"type\":\"message\"}\n\n")
(should (= 1 (length processed-events)))
(should (hash-table-p (car processed-events)))))))
(let ((processed-events '())
(noise-sse--buffer "")
(noise-sse--http-buffer "")
(noise-sse--chunk-buffer "")
(noise-sse--headers-received t)
(noise-sse--chunked-response-p nil))
(let ((noise-sse--event-handler
(lambda (event) (push event processed-events))))
(noise-sse--filter nil "data: {\"type\":\"message\"}\n\n")
(should (= 1 (length processed-events)))
(should (hash-table-p (car processed-events))))))
(ert-deftest noise-sse--filter-buffers-incomplete-event ()
"Process filter buffers incomplete events until newline."
(let ((processed-events '()))
(cl-letf (((symbol-function 'noise-sse--handle-event)
(lambda (event) (push event processed-events))))
(let ((noise-sse--buffer ""))
(noise-sse--filter nil "data: {\"type\":\"message\"")
(should (= 0 (length processed-events)))
(noise-sse--filter nil "}\n\n")
(should (= 1 (length processed-events)))))))
(let ((processed-events '())
(noise-sse--buffer "")
(noise-sse--http-buffer "")
(noise-sse--chunk-buffer "")
(noise-sse--headers-received t)
(noise-sse--chunked-response-p nil))
(let ((noise-sse--event-handler
(lambda (event) (push event processed-events))))
(noise-sse--filter nil "data: {\"type\":\"message\"")
(should (= 0 (length processed-events)))
(noise-sse--filter nil "}\n\n")
(should (= 1 (length processed-events))))))
(ert-deftest noise-sse--filter-skips-http-headers ()
"HTTP response headers should not block the first SSE event."
(let ((processed-events '())
(noise-sse--buffer "")
(noise-sse--http-buffer "")
(noise-sse--chunk-buffer "")
(noise-sse--headers-received nil)
(noise-sse--chunked-response-p nil))
(let ((noise-sse--event-handler
(lambda (event) (push event processed-events))))
(noise-sse--filter nil
(concat "HTTP/1.1 200 OK\r\n"
"Content-Type: text/event-stream\r\n\r\n"
"data: {\"type\":\"message\"}\n\n"))
(should (= 1 (length processed-events)))
(should (equal "message" (gethash "type" (car processed-events)))))))
(ert-deftest noise-sse--filter-decodes-chunked-http-body ()
"Chunked HTTP SSE responses should be decoded before parsing events."
(let ((processed-events '())
(noise-sse--buffer "")
(noise-sse--http-buffer "")
(noise-sse--chunk-buffer "")
(noise-sse--headers-received nil)
(noise-sse--chunked-response-p nil))
(let ((noise-sse--event-handler
(lambda (event) (push event processed-events))))
(noise-sse--filter nil
(concat "HTTP/1.1 200 OK\r\n"
"Transfer-Encoding: chunked\r\n"
"Content-Type: text/event-stream\r\n\r\n"
"12\r\n"
"data: {\"type\":\"msg"
"\r\n"
"4\r\n"
"\"}\n\n"
"\r\n"
"0\r\n\r\n"))
(should (= 1 (length processed-events)))
(should (equal "msg" (gethash "type" (car processed-events)))))))
(ert-deftest noise-sse--build-url ()
"Build SSE endpoint URL from base address."