Rework handling of using a proc for the web server response body

To address encoding issues and improve exception handling.
This commit is contained in:
Christopher Baines 2025-02-03 11:25:11 +01:00
parent 40cf026ea4
commit 41974a6817
2 changed files with 134 additions and 48 deletions

View file

@ -1,4 +1,7 @@
(use-modules (srfi srfi-71)
(rnrs bytevectors)
(ice-9 binary-ports)
(ice-9 textual-ports)
(tests)
(fibers)
(fibers channels)
@ -30,6 +33,55 @@
uri
#:port (non-blocking-open-socket-for-uri uri)))))))
(run-fibers-for-tests
(lambda ()
(let* ((web-server
(run-knots-web-server
(lambda (request)
(values '((content-type . (text/plain))
(content-length . 3))
(lambda (port)
(display "foo" port))))
#:port 0)) ;; Bind to any port
(port
(web-server-port web-server))
(uri
(build-uri 'http #:host "127.0.0.1" #:port port)))
(let ((response
body
(http-get
uri
#:port (non-blocking-open-socket-for-uri uri))))
(assert-equal
"foo"
body)))))
(run-fibers-for-tests
(lambda ()
(let* ((web-server
(run-knots-web-server
(lambda (request)
(values '((content-type . (text/plain
(charset . "utf-8"))))
(lambda (port)
(display "☺" port))))
#:port 0)) ;; Bind to any port
(port
(web-server-port web-server))
(uri
(build-uri 'http #:host "127.0.0.1" #:port port)))
(let ((response
body
(http-get
uri
#:port (non-blocking-open-socket-for-uri uri))))
(assert-equal
"☺"
body)))))
;; Test hanlding of exceptions when writing the response to a client
(run-fibers-for-tests
(lambda ()
(let* ((exception-handled-sucecssfully-channel