web-http.test 22 KB

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