web-http.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. ;;;; web-http.test --- HTTP library -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010-2011, 2014-2017 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-suite web-http)
  19. #:use-module (web uri)
  20. #:use-module (web http)
  21. #:use-module (rnrs bytevectors)
  22. #:use-module (rnrs io ports)
  23. #:use-module (ice-9 regex)
  24. #:use-module (ice-9 control)
  25. #:use-module (srfi srfi-19)
  26. #:use-module (test-suite lib))
  27. (define-syntax pass-if-expected-exception
  28. (syntax-rules ()
  29. ((_ name exception-predicate? exp)
  30. (pass-if name
  31. (with-exception-handler
  32. (lambda (exn)
  33. (if (exception-predicate? exn)
  34. #t
  35. (error "unexpected exception" exn)))
  36. (lambda ()
  37. exp
  38. #f)
  39. #:unwind? #t)))))
  40. (define-syntax pass-if-only-parse
  41. (syntax-rules ()
  42. ((_ sym str val)
  43. (pass-if (format #f "~a: ~s -> ~s" 'sym str val)
  44. (and (equal? (parse-header 'sym str)
  45. val)
  46. (valid-header? 'sym val))))))
  47. (define-syntax-rule (pass-if-reparse sym val)
  48. (pass-if-equal (format #f "~a: ~s reparse" 'sym val) val
  49. (let ((str (call-with-output-string
  50. (lambda (port)
  51. (write-header 'sym val port)))))
  52. (call-with-values (lambda () (read-header (open-input-string str)))
  53. (lambda (sym* val*)
  54. (unless (eq? 'sym sym*) (error "unexpected header"))
  55. val*)))))
  56. (define-syntax pass-if-parse
  57. (syntax-rules ()
  58. ((_ sym str val)
  59. (begin
  60. (pass-if-only-parse sym str val)
  61. (pass-if-reparse sym val)))))
  62. (define-syntax pass-if-round-trip
  63. (syntax-rules ()
  64. ((_ str)
  65. (pass-if-equal (format #f "~s round trip" str)
  66. str
  67. (call-with-output-string
  68. (lambda (port)
  69. (call-with-values
  70. (lambda () (read-header (open-input-string str)))
  71. (lambda (sym val)
  72. (write-header sym val port)))))))))
  73. (define-syntax pass-if-any-error
  74. (syntax-rules ()
  75. ((_ sym str)
  76. (pass-if (format #f "~a: ~s -> any error" 'sym str)
  77. (% (catch #t
  78. (lambda ()
  79. (parse-header 'sym str)
  80. (abort (lambda () (error "expected exception"))))
  81. (lambda (k . args)
  82. #t))
  83. (lambda (k thunk)
  84. (thunk)))))))
  85. (define-syntax pass-if-parse-error
  86. (syntax-rules ()
  87. ((_ sym str expected-component)
  88. (pass-if (format #f "~a: ~s -> ~a error" 'sym str 'expected-component)
  89. (catch 'bad-header
  90. (lambda ()
  91. (parse-header 'sym str)
  92. (error "expected exception" 'expected-component))
  93. (lambda (k component arg)
  94. (if (or (not 'expected-component)
  95. (eq? 'expected-component component))
  96. #t
  97. (error "unexpected exception" component arg))))))))
  98. (define-syntax pass-if-read-request-line
  99. (syntax-rules ()
  100. ((_ str expected-method expected-uri expected-version)
  101. (pass-if str
  102. (equal? (call-with-values
  103. (lambda ()
  104. (read-request-line (open-input-string
  105. (string-append str "\r\n"))))
  106. list)
  107. (list 'expected-method
  108. expected-uri
  109. 'expected-version))))))
  110. (define-syntax pass-if-write-request-line
  111. (syntax-rules ()
  112. ((_ expected-str method uri version)
  113. (pass-if expected-str
  114. (equal? (string-append expected-str "\r\n")
  115. (call-with-output-string
  116. (lambda (port)
  117. (write-request-line 'method uri 'version port))))))))
  118. (define-syntax pass-if-read-response-line
  119. (syntax-rules ()
  120. ((_ str expected-version expected-code expected-phrase)
  121. (pass-if str
  122. (equal? (call-with-values
  123. (lambda ()
  124. (read-response-line (open-input-string
  125. (string-append str "\r\n"))))
  126. list)
  127. (list 'expected-version
  128. expected-code
  129. expected-phrase))))))
  130. (define-syntax pass-if-write-response-line
  131. (syntax-rules ()
  132. ((_ expected-str version code phrase)
  133. (pass-if expected-str
  134. (equal? (string-append expected-str "\r\n")
  135. (call-with-output-string
  136. (lambda (port)
  137. (write-response-line 'version code phrase port))))))))
  138. (with-test-prefix "read-request-line"
  139. (pass-if-read-request-line "GET / HTTP/1.1"
  140. GET
  141. (build-uri-reference
  142. #:path "/")
  143. (1 . 1))
  144. (pass-if-read-request-line "GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1"
  145. GET
  146. (build-uri-reference
  147. #:scheme 'http
  148. #:host "www.w3.org"
  149. #:path "/pub/WWW/TheProject.html")
  150. (1 . 1))
  151. (pass-if-read-request-line "GET /pub/WWW/TheProject.html HTTP/1.1"
  152. GET
  153. (build-uri-reference
  154. #:path "/pub/WWW/TheProject.html")
  155. (1 . 1))
  156. (pass-if-read-request-line "HEAD /etc/hosts?foo=bar HTTP/1.1"
  157. HEAD
  158. (build-uri-reference
  159. #:path "/etc/hosts"
  160. #:query "foo=bar")
  161. (1 . 1)))
  162. (with-test-prefix "write-request-line"
  163. (pass-if-write-request-line "GET / HTTP/1.1"
  164. GET
  165. (build-uri-reference
  166. #:path "/")
  167. (1 . 1))
  168. ;;; FIXME: Test fails due to scheme, host always being removed.
  169. ;;; However, it should be supported to request these be present, and
  170. ;;; that is possible with absolute/relative URI support.
  171. ;; (pass-if-write-request-line "GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1"
  172. ;; GET
  173. ;; (build-uri 'http
  174. ;; #:host "www.w3.org"
  175. ;; #:path "/pub/WWW/TheProject.html")
  176. ;; (1 . 1))
  177. (pass-if-write-request-line "GET /pub/WWW/TheProject.html HTTP/1.1"
  178. GET
  179. (build-uri-reference
  180. #:path "/pub/WWW/TheProject.html")
  181. (1 . 1))
  182. (pass-if-write-request-line "GET /?foo HTTP/1.1"
  183. GET
  184. (build-uri 'http #:query "foo")
  185. (1 . 1))
  186. (pass-if-write-request-line "HEAD /etc/hosts?foo=bar HTTP/1.1"
  187. HEAD
  188. (build-uri-reference
  189. #:path "/etc/hosts"
  190. #:query "foo=bar")
  191. (1 . 1)))
  192. (with-test-prefix "read-response-line"
  193. (pass-if-exception "missing CR/LF"
  194. `(bad-header . "")
  195. (call-with-input-string "HTTP/1.1 200 Almost okay"
  196. (lambda (port)
  197. (read-response-line port))))
  198. (pass-if-read-response-line "HTTP/1.0 404 Not Found"
  199. (1 . 0) 404 "Not Found")
  200. (pass-if-read-response-line "HTTP/1.1 200 OK"
  201. (1 . 1) 200 "OK")
  202. ;; Empty reason phrases are valid; see <http://bugs.gnu.org/22273>.
  203. (pass-if-read-response-line "HTTP/1.1 302 "
  204. (1 . 1) 302 ""))
  205. (with-test-prefix "write-response-line"
  206. (pass-if-write-response-line "HTTP/1.0 404 Not Found"
  207. (1 . 0) 404 "Not Found")
  208. (pass-if-write-response-line "HTTP/1.1 200 OK"
  209. (1 . 1) 200 "OK"))
  210. (with-test-prefix "general headers"
  211. (pass-if-parse cache-control "no-transform" '(no-transform))
  212. (pass-if-parse cache-control "no-transform,foo" '(no-transform foo))
  213. (pass-if-parse cache-control "no-cache" '(no-cache))
  214. (pass-if-parse cache-control "no-cache=\"Authorization, Date\""
  215. '((no-cache . (authorization date))))
  216. (pass-if-parse cache-control "private=\"Foo\""
  217. '((private . (foo))))
  218. (pass-if-parse cache-control "no-cache,max-age=10"
  219. '(no-cache (max-age . 10)))
  220. (pass-if-parse cache-control "max-stale" '(max-stale))
  221. (pass-if-parse cache-control "max-stale=10" '((max-stale . 10)))
  222. (pass-if-round-trip "Cache-Control: acme-cache-extension\r\n")
  223. (pass-if-round-trip "Cache-Control: acme-cache-extension=20\r\n")
  224. (pass-if-round-trip "Cache-Control: acme-cache-extension=100 quux\r\n")
  225. (pass-if-round-trip "Cache-Control: acme-cache-extension=\"100, quux\"\r\n")
  226. (pass-if-parse connection "close" '(close))
  227. (pass-if-parse connection "Content-Encoding" '(content-encoding))
  228. (pass-if-parse date "Tue, 15 Nov 1994 08:12:31 GMT"
  229. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  230. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  231. (pass-if-parse date "Tue, 15 Nov 1994 16:12:31 +0800"
  232. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  233. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  234. (pass-if-parse date "Wed, 7 Sep 2011 11:25:00 GMT"
  235. (string->date "Wed, 7 Sep 2011 11:25:00 +0000"
  236. "~a,~e ~b ~Y ~H:~M:~S ~z"))
  237. ;; This is a non-conforming date (lack of leading zero for the hours)
  238. ;; that some HTTP servers provide. See <http://bugs.gnu.org/23421>.
  239. (pass-if-parse date "Sun, 06 Nov 1994 8:49:37 GMT"
  240. (string->date "Sun, 6 Nov 1994 08:49:37 +0000"
  241. "~a,~e ~b ~Y ~H:~M:~S ~z"))
  242. (pass-if-parse date "Sun, 6 Nov 1994 8:49:37 GMT"
  243. (string->date "Sun, 6 Nov 1994 08:49:37 +0000"
  244. "~a,~e ~b ~Y ~H:~M:~S ~z"))
  245. (pass-if-parse-error date "Tue, 15 Nov 1994 08:12:31 EST" date)
  246. (pass-if-any-error date "Tue, 15 Qux 1994 08:12:31 EST")
  247. (pass-if-parse pragma "no-cache" '(no-cache))
  248. (pass-if-parse pragma "no-cache, foo" '(no-cache foo))
  249. (pass-if-parse trailer "foo, bar" '(foo bar))
  250. (pass-if-parse trailer "connection, bar" '(connection bar))
  251. (pass-if-parse transfer-encoding "foo, chunked" '((foo) (chunked)))
  252. (pass-if-parse upgrade "qux" '("qux"))
  253. (pass-if-parse via "xyzzy" '("xyzzy"))
  254. (pass-if-parse warning "123 foo \"core breach imminent\""
  255. '((123 "foo" "core breach imminent" #f)))
  256. (pass-if-parse
  257. warning
  258. "123 foo \"core breach imminent\" \"Tue, 15 Nov 1994 08:12:31 GMT\""
  259. `((123 "foo" "core breach imminent"
  260. ,(string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  261. "~a, ~d ~b ~Y ~H:~M:~S ~z")))))
  262. (with-test-prefix "entity headers"
  263. (pass-if-parse allow "foo, bar" '(foo bar))
  264. (pass-if-parse content-disposition "form-data; name=\"file\"; filename=\"q.go\""
  265. '(form-data (name . "file") (filename . "q.go")))
  266. (pass-if-parse content-encoding "qux, baz" '(qux baz))
  267. (pass-if-parse content-language "qux, baz" '("qux" "baz"))
  268. (pass-if-parse content-length "100" 100)
  269. (pass-if-parse content-length "0" 0)
  270. (pass-if-parse content-length "010" 10)
  271. (pass-if-parse content-location "http://foo/"
  272. (build-uri 'http #:host "foo" #:path "/"))
  273. (pass-if-parse content-location "//foo/"
  274. (build-uri-reference #:host "foo" #:path "/"))
  275. (pass-if-parse content-location "/etc/foo"
  276. (build-uri-reference #:path "/etc/foo"))
  277. (pass-if-parse content-location "foo"
  278. (build-uri-reference #:path "foo"))
  279. (pass-if-parse content-range "bytes 10-20/*" '(bytes (10 . 20) *))
  280. (pass-if-parse content-range "bytes */*" '(bytes * *))
  281. (pass-if-parse content-range "bytes */30" '(bytes * 30))
  282. (pass-if-parse content-type "foo/bar" '(foo/bar))
  283. (pass-if-parse content-type "foo/bar; baz=qux" '(foo/bar (baz . "qux")))
  284. (pass-if-parse expires "Tue, 15 Nov 1994 08:12:31 GMT"
  285. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  286. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  287. (pass-if-parse last-modified "Tue, 15 Nov 1994 08:12:31 GMT"
  288. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  289. "~a, ~d ~b ~Y ~H:~M:~S ~z")))
  290. (with-test-prefix "request headers"
  291. (pass-if-parse accept "text/*;q=0.3, text/html;q=0.7, text/html;level=1"
  292. '((text/* (q . 300))
  293. (text/html (q . 700))
  294. (text/html (level . "1"))))
  295. (pass-if-parse accept-charset "iso-8859-5, unicode-1-1;q=0.8"
  296. '((1000 . "iso-8859-5") (800 . "unicode-1-1")))
  297. (pass-if-parse accept-encoding "gzip;q=1.0, identity; q=0.5, *;q=0"
  298. '((1000 . "gzip")
  299. (500 . "identity")
  300. (0 . "*")))
  301. (pass-if-parse accept-language "da, en-gb;q=0.8, en;q=0.7"
  302. '((1000 . "da") (800 . "en-gb") (700 . "en")))
  303. ;; Allow nonstandard .2 to mean 0.2
  304. (pass-if-parse accept-language "en-gb;q=.2" '((200 . "en-gb")))
  305. (pass-if-parse authorization "Basic foooo" '(basic . "foooo"))
  306. (pass-if-parse authorization "Digest foooo" '(digest foooo))
  307. (pass-if-parse authorization "Digest foo=bar,baz=qux"
  308. '(digest (foo . "bar") (baz . "qux")))
  309. (pass-if-parse authorization "basic foooo" '(basic . "foooo"))
  310. (pass-if-parse authorization "digest foooo" '(digest foooo))
  311. (pass-if-parse authorization "digest foo=bar,baz=qux"
  312. '(digest (foo . "bar") (baz . "qux")))
  313. (pass-if-round-trip "Authorization: Basic foooo\r\n")
  314. (pass-if-round-trip "Authorization: Bearer token\r\n")
  315. (pass-if-round-trip "Authorization: Digest foooo\r\n")
  316. (pass-if-round-trip "Authorization: Digest foo=bar, baz=qux\r\n")
  317. (pass-if-parse expect "100-continue, foo" '((100-continue) (foo)))
  318. (pass-if-parse from "foo@bar" "foo@bar")
  319. (pass-if-parse host "qux" '("qux" . #f))
  320. (pass-if-parse host "qux:80" '("qux" . 80))
  321. (pass-if-parse host "[2001:db8::1]" '("2001:db8::1" . #f))
  322. (pass-if-parse host "[2001:db8::1]:80" '("2001:db8::1" . 80))
  323. (pass-if-parse host "[::ffff:192.0.2.1]" '("::ffff:192.0.2.1" . #f))
  324. (pass-if-round-trip "Host: [2001:db8::1]\r\n")
  325. (pass-if-parse if-match "\"xyzzy\", W/\"qux\""
  326. '(("xyzzy" . #t) ("qux" . #f)))
  327. (pass-if-parse if-match "*" '*)
  328. (pass-if-parse if-modified-since "Tue, 15 Nov 1994 08:12:31 GMT"
  329. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  330. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  331. (pass-if-parse if-none-match "\"xyzzy\", W/\"qux\""
  332. '(("xyzzy" . #t) ("qux" . #f)))
  333. (pass-if-parse if-none-match "xyzzy, W/\"qux\""
  334. '(("xyzzy" . #t) ("qux" . #f)))
  335. (pass-if-parse if-none-match "*" '*)
  336. (pass-if-parse if-range "\"foo\"" '("foo" . #t))
  337. (pass-if-parse if-range "Tue, 15 Nov 1994 08:12:31 GMT"
  338. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  339. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  340. (pass-if-parse if-unmodified-since "Tue, 15 Nov 1994 08:12:31 GMT"
  341. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  342. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  343. (pass-if-parse max-forwards "10" 10)
  344. (pass-if-parse max-forwards "00" 0)
  345. (pass-if-parse proxy-authorization "Basic foooo" '(basic . "foooo"))
  346. (pass-if-parse proxy-authorization "Digest foooo" '(digest foooo))
  347. (pass-if-parse proxy-authorization "Digest foo=bar,baz=qux"
  348. '(digest (foo . "bar") (baz . "qux")))
  349. (pass-if-parse range "bytes=10-20" '(bytes (10 . 20)))
  350. (pass-if-parse range "bytes=10-" '(bytes (10 . #f)))
  351. (pass-if-parse range "bytes=-20" '(bytes (#f . 20)))
  352. (pass-if-parse range "bytes=-20,-30" '(bytes (#f . 20) (#f . 30)))
  353. (pass-if-parse referer "http://foo/bar?baz"
  354. (build-uri 'http #:host "foo" #:path "/bar" #:query "baz"))
  355. (pass-if-parse referer "//foo/bar?baz"
  356. (build-uri-reference #:host "foo"
  357. #:path "/bar"
  358. #:query "baz"))
  359. (pass-if-parse referer "/etc/foo"
  360. (build-uri-reference #:path "/etc/foo"))
  361. (pass-if-parse referer "foo"
  362. (build-uri-reference #:path "foo"))
  363. (pass-if-parse te "trailers" '((trailers)))
  364. (pass-if-parse te "trailers,foo" '((trailers) (foo)))
  365. (pass-if-parse user-agent "guile" "guile"))
  366. ;; Response headers
  367. ;;
  368. (with-test-prefix "response headers"
  369. (pass-if-parse accept-ranges "foo,bar" '(foo bar))
  370. (pass-if-parse age "30" 30)
  371. (pass-if-parse etag "\"foo\"" '("foo" . #t))
  372. (pass-if-parse etag "W/\"foo\"" '("foo" . #f))
  373. (pass-if-parse etag "foo" '("foo" . #t))
  374. (pass-if-parse location "http://other-place"
  375. (build-uri 'http #:host "other-place"))
  376. (pass-if-only-parse location "#foo"
  377. (build-uri-reference #:fragment "foo"))
  378. (pass-if-only-parse location "/#foo"
  379. (build-uri-reference #:path "/" #:fragment "foo"))
  380. (pass-if-parse location "/foo"
  381. (build-uri-reference #:path "/foo"))
  382. (pass-if-parse location "//server/foo"
  383. (build-uri-reference #:host "server" #:path "/foo"))
  384. (pass-if-parse proxy-authenticate "Basic realm=\"guile\""
  385. '((basic (realm . "guile"))))
  386. (pass-if-parse retry-after "Tue, 15 Nov 1994 08:12:31 GMT"
  387. (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
  388. "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  389. (pass-if-parse retry-after "20" 20)
  390. (pass-if-parse server "guile!" "guile!")
  391. (pass-if-parse vary "*" '*)
  392. (pass-if-parse vary "foo, bar" '(foo bar))
  393. (pass-if-parse www-authenticate "Basic realm=\"guile\""
  394. '((basic (realm . "guile")))))
  395. (with-test-prefix "chunked encoding"
  396. (let* ((s "5\r\nFirst\r\nA\r\n line\n Sec\r\n8\r\nond line\r\n0\r\n\r\n")
  397. (p (make-chunked-input-port (open-input-string s))))
  398. (pass-if-equal
  399. "First line\n Second line"
  400. (get-string-all p))
  401. (pass-if (port-eof? (make-chunked-input-port (open-input-string "0\r\n"))))
  402. (pass-if-equal "reads chunks without buffering"
  403. ;; Make sure the chunked input port does not read more than what
  404. ;; the client asked. See <http://bugs.gnu.org/19939>
  405. `("First " "chunk." "Second " "chunk."
  406. (1 1 1 6 6 1 1
  407. 1 1 1 7 6 1 1))
  408. (let* ((str "C\r\nFirst chunk.\r\nD\r\nSecond chunk.\r\n")
  409. (requests '())
  410. (read! (let ((port (open-input-string str)))
  411. (lambda (bv index count)
  412. (set! requests (cons count requests))
  413. (let ((n (get-bytevector-n! port bv index
  414. count)))
  415. (if (eof-object? n) 0 n)))))
  416. (input (make-custom-binary-input-port "chunky" read!
  417. #f #f #f))
  418. (port (make-chunked-input-port input)))
  419. (setvbuf input 'none)
  420. (setvbuf port 'none)
  421. (list (utf8->string (get-bytevector-n port 6))
  422. (utf8->string (get-bytevector-n port 6))
  423. (utf8->string (get-bytevector-n port 7))
  424. (utf8->string (get-bytevector-n port 6))
  425. (reverse requests))))
  426. (pass-if-equal "reads across chunk boundaries"
  427. ;; Same, but read across chunk boundaries.
  428. `("First " "chunk.Second " "chunk."
  429. (1 1 1 6 6 1 1
  430. 1 1 1 7 6 1 1))
  431. (let* ((str "C\r\nFirst chunk.\r\nD\r\nSecond chunk.\r\n")
  432. (requests '())
  433. (read! (let ((port (open-input-string str)))
  434. (lambda (bv index count)
  435. (set! requests (cons count requests))
  436. (let ((n (get-bytevector-n! port bv index
  437. count)))
  438. (if (eof-object? n) 0 n)))))
  439. (input (make-custom-binary-input-port "chunky" read!
  440. #f #f #f))
  441. (port (make-chunked-input-port input)))
  442. (setvbuf input 'none)
  443. (setvbuf port 'none)
  444. (list (utf8->string (get-bytevector-n port 6))
  445. (utf8->string (get-bytevector-n port 13))
  446. (utf8->string (get-bytevector-n port 6))
  447. (reverse requests)))))
  448. (pass-if-equal "EOF instead of chunk header"
  449. "Only chunk."
  450. ;; Omit the second chunk header, leading to a premature EOF. This
  451. ;; used to cause 'read-chunk-header' to throw to wrong-type-arg.
  452. ;; See the backtrace at
  453. ;; <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=19976#5>.
  454. (let* ((str "B\r\nOnly chunk.")
  455. (port (make-chunked-input-port (open-input-string str))))
  456. (get-string-all port)))
  457. (pass-if-expected-exception "Exception on premature chunk end"
  458. chunked-input-ended-prematurely-error?
  459. (let* ((str "b\r\nFirst chunk\r\nc\r\nSecond chun")
  460. (port (make-chunked-input-port (open-input-string str))))
  461. (get-string-all port)))
  462. (pass-if-equal
  463. (call-with-output-string
  464. (lambda (out-raw)
  465. (let ((out-chunked (make-chunked-output-port out-raw
  466. #:keep-alive? #t)))
  467. (display "First chunk" out-chunked)
  468. (force-output out-chunked)
  469. (display "Second chunk" out-chunked)
  470. (force-output out-chunked)
  471. (display "Third chunk" out-chunked)
  472. (close-port out-chunked))))
  473. "b\r\nFirst chunk\r\nc\r\nSecond chunk\r\nb\r\nThird chunk\r\n0\r\n\r\n"))