AtlatestRepositorysigil-http
sigil-http / tree / testserver-regression.sgl
1
;;; Regression tests for (sigil http server) request framing.3
(define-library (test server-regression)4
(import (sigil core)5
(sigil io)6
(sigil test)7
(sigil http request)8
(sigil http server))9
(export main)11
(begin13
(test-group "http-parse-request-buffer"15
(test "parses ASCII body using Content-Length"16
(let* ((body "test body data")17
(request-bytes18
(string->utf819
(string-append20
"POST /echo-body HTTP/1.1\r\n"21
"Host: example.test\r\n"22
"Content-Length: "23
(number->string (bytevector-length (string->utf8 body)))24
"\r\n"25
"\r\n"26
body)))27
(req (http-parse-request-buffer request-bytes)))28
(assert-true (http-request? req))29
(assert-equal (http-request-method req) 'POST)30
(assert-equal (http-request-path req) "/echo-body")31
(assert-equal (http-request-body req) body)32
(assert-equal (bytevector-length (http-request-raw-body req))33
(bytevector-length (string->utf8 body)))))35
(test "waits for complete byte-counted body"36
(let ((req (http-parse-request-buffer37
(string->utf838
(string-append39
"POST /echo-body HTTP/1.1\r\n"40
"Host: example.test\r\n"41
"Content-Length: 8\r\n"42
"\r\n"43
"abc")))))44
(assert-false req)))46
(test "parses UTF-8 body by byte Content-Length"47
;; "Γειά" is 8 bytes in UTF-8 but 4 characters. The server must use48
;; Content-Length as a byte count while preserving the text body API.49
(let* ((body "Γειά")50
(body-bytes (string->utf8 body))51
(header-bytes52
(string->utf853
(string-append54
"POST /echo-body HTTP/1.1\r\n"55
"Host: example.test\r\n"56
"Content-Type: text/plain; charset=utf-8\r\n"57
"Content-Length: "58
(number->string (bytevector-length body-bytes))59
"\r\n"60
"\r\n")))61
(request-bytes (bytevector-append header-bytes body-bytes))62
(req (http-parse-request-buffer request-bytes)))63
(assert-true (http-request? req))64
(assert-equal (http-request-body req) body)65
(assert-equal (bytevector-length (http-request-raw-body req)) 8)))67
(test "keeps binary image body as bytevector"68
(let* ((body-bytes (bytevector 0 255 216 17 128 0 42 99))69
(header-bytes70
(string->utf871
(string-append72
"POST /upload HTTP/1.1\r\n"73
"Host: example.test\r\n"74
"Content-Type: image/png\r\n"75
"Content-Length: "76
(number->string (bytevector-length body-bytes))77
"\r\n"78
"\r\n")))79
(request-bytes (bytevector-append header-bytes body-bytes))80
(req (http-parse-request-buffer request-bytes)))81
(assert-true (http-request? req))82
(assert-true (bytevector? (http-request-body req)))83
(assert-equal (bytevector-length (http-request-body req)) 8)84
(assert-equal (bytevector-length (http-request-raw-body req)) 8))))86
(run-tests)88
(define (main . args)89
#t)))