AtlatestRepositorysigil-http
sigil-http / tree / testtest-upload-bytes.sgl
1
(import (sigil core) (sigil io) (sigil test) (sigil string) (sigil process) (sigil http client))3
(define (same-bytes? a b)4
(and (= (bytevector-length a) (bytevector-length b))5
(let loop ((i 0))6
(or (= i (bytevector-length a))7
(and (= (bytevector-u8-ref a i) (bytevector-u8-ref b i)) (loop (+ i 1)))))))8
(define (sample-bytes size)9
(let ((bytes (make-bytevector size)))10
(let loop ((i 0))11
(when (< i size) (bytevector-u8-set! bytes i (modulo i 256)) (loop (+ i 1)))) bytes))13
(test-group "binary request assembly"14
(test "arbitrary bytes bypass UTF-8 encoding"15
(let* ((body (sample-bytes 256))16
(wire (build-request-bytes 'PUT (parse-url "http://example.com/object") #{} body))17
(split (- (bytevector-length wire) 256))18
(head (utf8->string (bytevector-copy wire 0 split))))19
(assert-true (string-contains? head "content-length: 256\r\n"))20
(assert-true (same-bytes? (bytevector-copy wire split) body))))21
(test "dict framing headers replaced case-insensitively"22
(let* ((wire (build-request-bytes 'PUT (parse-url "http://example.com/")23
#{ Content-Length: "999" content-length: "88" Transfer-Encoding: "chunked" } (make-bytevector 0)))24
(head (string-downcase (utf8->string wire))))25
(assert-equal (length (string-split head "content-length:")) 2)26
(assert-true (string-contains? head "content-length: 0\r\n"))27
(assert-false (string-contains? head "transfer-encoding:"))))28
(test "alist framing headers replaced case-insensitively"29
(let ((head (string-downcase (utf8->string30
(build-request-bytes 'PUT (parse-url "http://example.com/")31
'(("Content-Length" . "999") ("TRANSFER-ENCODING" . "chunked")) (make-bytevector 0))))))32
(assert-equal (length (string-split head "content-length:")) 2)33
(assert-false (string-contains? head "transfer-encoding:"))))34
(test "string body preserves existing UTF-8 wire format"35
(let ((url (parse-url "http://example.com/")))36
(let ((wire (utf8->string (build-request-bytes 'POST url #{} "héllo"))))37
(assert-true (string-contains? wire "Content-Length: 6\r\n"))38
(assert-true (string-contains? wire "\r\n\r\nhéllo"))))))40
(when (getenv "HTTP_UPLOAD_TEST_PORT")41
(test-group "binary upload network"42
(test "large binary body and empty body round-trip through HTTP"43
(let ((url (string-append "http://127.0.0.1:" (getenv "HTTP_UPLOAD_TEST_PORT") "/bytes")))44
(for-each (lambda (body)45
(let ((result (http-fetch-bytes 'PUT url headers: #{ content-type: "application/octet-stream" }46
body: body timeout: 5)))47
(assert-true result)48
(assert-equal (dict-ref result status:) 200)49
(assert-true (same-bytes? body (dict-ref result body:)))))50
(list (sample-bytes (* 1024 1024 2)) (make-bytevector 0)))))))