summaryrefslogtreecommitdiff
path: root/test-suite/tests/web-http.test
blob: 3c2acf11c62402704147f58eb6ce303dc05b9c6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
;;;; web-http.test --- HTTP library        -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; 	Copyright (C) 2010-2011, 2014-2017 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


(define-module (test-suite web-http)
  #:use-module (web uri)
  #:use-module (web http)
  #:use-module (rnrs bytevectors)
  #:use-module (rnrs io ports)
  #:use-module (ice-9 regex)
  #:use-module (ice-9 control)
  #:use-module (srfi srfi-19)
  #:use-module (test-suite lib))


(define-syntax pass-if-named-exception
  (syntax-rules ()
    ((_ name k pat exp)
     (pass-if name
       (catch 'k
         (lambda () exp (error "expected exception" 'k))
         (lambda (k message args)
           (if (string-match pat message)
               #t
               (error "unexpected exception" message args))))))))

(define-syntax pass-if-only-parse
  (syntax-rules ()
    ((_ sym str val)
     (pass-if (format #f "~a: ~s -> ~s" 'sym str val)
       (and (equal? (parse-header 'sym str)
                    val)
            (valid-header? 'sym val))))))

(define-syntax-rule (pass-if-reparse sym val)
  (pass-if-equal (format #f "~a: ~s reparse" 'sym val) val
    (let ((str (call-with-output-string
                 (lambda (port)
                   (write-header 'sym val port)))))
      (call-with-values (lambda () (read-header (open-input-string str)))
        (lambda (sym* val*)
          (unless (eq? 'sym sym*) (error "unexpected header"))
          val*)))))

(define-syntax pass-if-parse
  (syntax-rules ()
    ((_ sym str val)
     (begin
       (pass-if-only-parse sym str val)
       (pass-if-reparse sym val)))))

(define-syntax pass-if-round-trip
  (syntax-rules ()
    ((_ str)
     (pass-if-equal (format #f "~s round trip" str)
         str
       (call-with-output-string
        (lambda (port)
          (call-with-values
              (lambda () (read-header (open-input-string str)))
            (lambda (sym val)
              (write-header sym val port)))))))))

(define-syntax pass-if-any-error
  (syntax-rules ()
    ((_ sym str)
     (pass-if (format #f "~a: ~s -> any error" 'sym str)
       (% (catch #t
            (lambda ()
              (parse-header 'sym str)
              (abort (lambda () (error "expected exception"))))
            (lambda (k . args)
              #t))
          (lambda (k thunk)
            (thunk)))))))

(define-syntax pass-if-parse-error
  (syntax-rules ()
    ((_ sym str expected-component)
     (pass-if (format #f "~a: ~s -> ~a error" 'sym str 'expected-component)
       (catch 'bad-header
         (lambda ()
           (parse-header 'sym str)
           (error "expected exception" 'expected-component))
         (lambda (k component arg)
           (if (or (not 'expected-component)
                   (eq? 'expected-component component))
               #t
               (error "unexpected exception" component arg))))))))

(define-syntax pass-if-read-request-line
  (syntax-rules ()
    ((_ str expected-method expected-uri expected-version)
     (pass-if str
       (equal? (call-with-values
                   (lambda ()
                     (read-request-line (open-input-string
                                         (string-append str "\r\n"))))
                 list)
               (list 'expected-method
                     expected-uri
                     'expected-version))))))

(define-syntax pass-if-write-request-line
  (syntax-rules ()
    ((_ expected-str method uri version)
     (pass-if expected-str
       (equal? (string-append expected-str "\r\n")
               (call-with-output-string
                (lambda (port)
                  (write-request-line 'method uri 'version port))))))))

(define-syntax pass-if-read-response-line
  (syntax-rules ()
    ((_ str expected-version expected-code expected-phrase)
     (pass-if str
       (equal? (call-with-values
                   (lambda ()
                     (read-response-line (open-input-string
                                          (string-append str "\r\n"))))
                 list)
               (list 'expected-version
                     expected-code
                     expected-phrase))))))

(define-syntax pass-if-write-response-line
  (syntax-rules ()
    ((_ expected-str version code phrase)
     (pass-if expected-str
       (equal? (string-append expected-str "\r\n")
               (call-with-output-string
                (lambda (port)
                  (write-response-line 'version code phrase port))))))))

(with-test-prefix "read-request-line"
  (pass-if-read-request-line "GET / HTTP/1.1"
                             GET
                             (build-uri-reference
                              #:path "/")
                             (1 . 1))
  (pass-if-read-request-line "GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1"
                             GET
                             (build-uri-reference
                              #:scheme 'http
                              #:host "www.w3.org"
                              #:path "/pub/WWW/TheProject.html")
                             (1 . 1))
  (pass-if-read-request-line "GET /pub/WWW/TheProject.html HTTP/1.1"
                             GET
                             (build-uri-reference
                              #:path "/pub/WWW/TheProject.html")
                             (1 . 1))
  (pass-if-read-request-line "HEAD /etc/hosts?foo=bar HTTP/1.1"
                             HEAD
                             (build-uri-reference
                              #:path "/etc/hosts"
                              #:query "foo=bar")
                             (1 . 1)))

(with-test-prefix "write-request-line"
  (pass-if-write-request-line "GET / HTTP/1.1"
                              GET
                              (build-uri-reference
                               #:path "/")
                              (1 . 1))
  ;;; FIXME: Test fails due to scheme, host always being removed.
  ;;; However, it should be supported to request these be present, and
  ;;; that is possible with absolute/relative URI support.
  ;; (pass-if-write-request-line "GET http://www.w3.org/pub/WWW/TheProject.html HTTP/1.1"
  ;;                             GET
  ;;                             (build-uri 'http
  ;;                                        #:host "www.w3.org"
  ;;                                        #:path "/pub/WWW/TheProject.html")
  ;;                             (1 . 1))
  (pass-if-write-request-line "GET /pub/WWW/TheProject.html HTTP/1.1"
                              GET
                              (build-uri-reference
                               #:path "/pub/WWW/TheProject.html")
                              (1 . 1))
  (pass-if-write-request-line "GET /?foo HTTP/1.1"
                              GET
                              (build-uri 'http #:query "foo")
                              (1 . 1))
  (pass-if-write-request-line "HEAD /etc/hosts?foo=bar HTTP/1.1"
                              HEAD
                              (build-uri-reference
                               #:path "/etc/hosts"
                               #:query "foo=bar")
                              (1 . 1)))

(with-test-prefix "read-response-line"
  (pass-if-exception "missing CR/LF"
      `(bad-header . "")
    (call-with-input-string "HTTP/1.1 200 Almost okay"
      (lambda (port)
        (read-response-line port))))
  (pass-if-read-response-line "HTTP/1.0 404 Not Found"
                              (1 . 0) 404 "Not Found")
  (pass-if-read-response-line "HTTP/1.1 200 OK"
                              (1 . 1) 200 "OK")

  ;; Empty reason phrases are valid; see <http://bugs.gnu.org/22273>.
  (pass-if-read-response-line "HTTP/1.1 302 "
                              (1 . 1) 302 ""))

(with-test-prefix "write-response-line"
  (pass-if-write-response-line "HTTP/1.0 404 Not Found"
                               (1 . 0) 404 "Not Found")
  (pass-if-write-response-line "HTTP/1.1 200 OK"
                               (1 . 1) 200 "OK"))

(with-test-prefix "general headers"

  (pass-if-parse cache-control "no-transform" '(no-transform))
  (pass-if-parse cache-control "no-transform,foo" '(no-transform foo))
  (pass-if-parse cache-control "no-cache" '(no-cache))
  (pass-if-parse cache-control "no-cache=\"Authorization, Date\""
                 '((no-cache . (authorization date))))
  (pass-if-parse cache-control "private=\"Foo\""
                 '((private . (foo))))
  (pass-if-parse cache-control "no-cache,max-age=10"
                 '(no-cache (max-age . 10)))
  (pass-if-parse cache-control "max-stale" '(max-stale))
  (pass-if-parse cache-control "max-stale=10" '((max-stale . 10)))
  (pass-if-round-trip "Cache-Control: acme-cache-extension\r\n")
  (pass-if-round-trip "Cache-Control: acme-cache-extension=20\r\n")
  (pass-if-round-trip "Cache-Control: acme-cache-extension=100 quux\r\n")
  (pass-if-round-trip "Cache-Control: acme-cache-extension=\"100, quux\"\r\n")

  (pass-if-parse connection "close" '(close))
  (pass-if-parse connection "Content-Encoding" '(content-encoding))

  (pass-if-parse date "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                               "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse date "Tue, 15 Nov 1994 16:12:31 +0800"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                               "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse date "Wed, 7 Sep 2011 11:25:00 GMT"
                 (string->date "Wed, 7 Sep 2011 11:25:00 +0000"
                               "~a,~e ~b ~Y ~H:~M:~S ~z"))

  ;; This is a non-conforming date (lack of leading zero for the hours)
  ;; that some HTTP servers provide.  See <http://bugs.gnu.org/23421>.
  (pass-if-parse date "Sun, 06 Nov 1994  8:49:37 GMT"
                 (string->date "Sun, 6 Nov 1994 08:49:37 +0000"
                               "~a,~e ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse date "Sun, 6 Nov 1994  8:49:37 GMT"
                 (string->date "Sun, 6 Nov 1994 08:49:37 +0000"
                               "~a,~e ~b ~Y ~H:~M:~S ~z"))

  (pass-if-parse-error date "Tue, 15 Nov 1994 08:12:31 EST" date)
  (pass-if-any-error date "Tue, 15 Qux 1994 08:12:31 EST")

  (pass-if-parse pragma "no-cache" '(no-cache))
  (pass-if-parse pragma "no-cache, foo" '(no-cache foo))

  (pass-if-parse trailer "foo, bar" '(foo bar))
  (pass-if-parse trailer "connection, bar" '(connection bar))

  (pass-if-parse transfer-encoding "foo, chunked" '((foo) (chunked)))

  (pass-if-parse upgrade "qux" '("qux"))

  (pass-if-parse via "xyzzy" '("xyzzy"))

  (pass-if-parse warning "123 foo \"core breach imminent\""
                 '((123 "foo" "core breach imminent" #f)))
  (pass-if-parse
   warning
   "123 foo \"core breach imminent\" \"Tue, 15 Nov 1994 08:12:31 GMT\""
   `((123 "foo" "core breach imminent"
          ,(string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z")))))

(with-test-prefix "entity headers"
  (pass-if-parse allow "foo, bar" '(foo bar))
  (pass-if-parse content-disposition "form-data; name=\"file\"; filename=\"q.go\""
                 '(form-data (name . "file") (filename . "q.go")))
  (pass-if-parse content-encoding "qux, baz" '(qux baz))
  (pass-if-parse content-language "qux, baz" '("qux" "baz"))
  (pass-if-parse content-length "100" 100)
  (pass-if-parse content-length "0" 0)
  (pass-if-parse content-length "010" 10)
  (pass-if-parse content-location "http://foo/"
                 (build-uri 'http #:host "foo" #:path "/"))
  (pass-if-parse content-location "//foo/"
                 (build-uri-reference #:host "foo" #:path "/"))
  (pass-if-parse content-location "/etc/foo"
                 (build-uri-reference #:path "/etc/foo"))
  (pass-if-parse content-location "foo"
                 (build-uri-reference #:path "foo"))
  (pass-if-parse content-range "bytes 10-20/*" '(bytes (10 . 20) *))
  (pass-if-parse content-range "bytes */*" '(bytes * *))
  (pass-if-parse content-range "bytes */30" '(bytes * 30))
  (pass-if-parse content-type "foo/bar" '(foo/bar))
  (pass-if-parse content-type "foo/bar; baz=qux" '(foo/bar (baz . "qux")))
  (pass-if-parse expires "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse last-modified "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z")))

(with-test-prefix "request headers"
  (pass-if-parse accept "text/*;q=0.3, text/html;q=0.7, text/html;level=1"
                 '((text/* (q . 300))
                   (text/html (q . 700))
                   (text/html (level . "1"))))
  (pass-if-parse accept-charset "iso-8859-5, unicode-1-1;q=0.8"
                 '((1000 . "iso-8859-5") (800 . "unicode-1-1")))
  (pass-if-parse accept-encoding "gzip;q=1.0, identity; q=0.5, *;q=0"
                 '((1000 . "gzip")
                   (500 . "identity")
                   (0 . "*")))
  (pass-if-parse accept-language "da, en-gb;q=0.8, en;q=0.7"
                 '((1000 . "da") (800 . "en-gb") (700 . "en")))
  ;; Allow nonstandard .2 to mean 0.2
  (pass-if-parse accept-language "en-gb;q=.2" '((200 . "en-gb")))
  (pass-if-parse authorization "Basic foooo" '(basic . "foooo"))
  (pass-if-parse authorization "Digest foooo" '(digest foooo))
  (pass-if-parse authorization "Digest foo=bar,baz=qux"
                 '(digest (foo . "bar") (baz . "qux")))
  (pass-if-parse authorization "basic foooo" '(basic . "foooo"))
  (pass-if-parse authorization "digest foooo" '(digest foooo))
  (pass-if-parse authorization "digest foo=bar,baz=qux"
                 '(digest (foo . "bar") (baz . "qux")))
  (pass-if-round-trip "Authorization: Basic foooo\r\n")
  (pass-if-round-trip "Authorization: Bearer token\r\n")
  (pass-if-round-trip "Authorization: Digest foooo\r\n")
  (pass-if-round-trip "Authorization: Digest foo=bar, baz=qux\r\n")
  (pass-if-parse expect "100-continue, foo" '((100-continue) (foo)))
  (pass-if-parse from "foo@bar" "foo@bar")
  (pass-if-parse host "qux" '("qux" . #f))
  (pass-if-parse host "qux:80" '("qux" . 80))
  (pass-if-parse host "[2001:db8::1]" '("2001:db8::1" . #f))
  (pass-if-parse host "[2001:db8::1]:80" '("2001:db8::1" . 80))
  (pass-if-parse host "[::ffff:192.0.2.1]" '("::ffff:192.0.2.1" . #f))
  (pass-if-round-trip "Host: [2001:db8::1]\r\n")
  (pass-if-parse if-match "\"xyzzy\", W/\"qux\""
                 '(("xyzzy" . #t) ("qux" . #f)))
  (pass-if-parse if-match "*" '*)
  (pass-if-parse if-modified-since "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse if-none-match "\"xyzzy\", W/\"qux\""
                 '(("xyzzy" . #t) ("qux" . #f)))
  (pass-if-parse if-none-match "xyzzy, W/\"qux\""
                 '(("xyzzy" . #t) ("qux" . #f)))
  (pass-if-parse if-none-match "*" '*)
  (pass-if-parse if-range "\"foo\"" '("foo" . #t))
  (pass-if-parse if-range  "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse if-unmodified-since "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse max-forwards "10" 10)
  (pass-if-parse max-forwards "00" 0)
  (pass-if-parse proxy-authorization "Basic foooo" '(basic . "foooo"))
  (pass-if-parse proxy-authorization "Digest foooo" '(digest foooo))
  (pass-if-parse proxy-authorization "Digest foo=bar,baz=qux"
                 '(digest (foo . "bar") (baz . "qux")))
  (pass-if-parse range "bytes=10-20" '(bytes (10 . 20)))
  (pass-if-parse range "bytes=10-" '(bytes (10 . #f)))
  (pass-if-parse range "bytes=-20" '(bytes (#f . 20)))
  (pass-if-parse range "bytes=-20,-30" '(bytes (#f . 20) (#f . 30)))
  (pass-if-parse referer "http://foo/bar?baz"
                 (build-uri 'http #:host "foo" #:path "/bar" #:query "baz"))
  (pass-if-parse referer "//foo/bar?baz"
                 (build-uri-reference #:host "foo"
                                      #:path "/bar"
                                      #:query "baz"))
  (pass-if-parse referer "/etc/foo"
                 (build-uri-reference #:path "/etc/foo"))
  (pass-if-parse referer "foo"
                 (build-uri-reference #:path "foo"))
  (pass-if-parse te "trailers" '((trailers)))
  (pass-if-parse te "trailers,foo" '((trailers) (foo)))
  (pass-if-parse user-agent "guile" "guile"))


;; Response headers
;;
(with-test-prefix "response headers"
  (pass-if-parse accept-ranges "foo,bar" '(foo bar))
  (pass-if-parse age "30" 30)
  (pass-if-parse etag "\"foo\"" '("foo" . #t))
  (pass-if-parse etag "W/\"foo\"" '("foo" . #f))
  (pass-if-parse etag "foo" '("foo" . #t))
  (pass-if-parse location "http://other-place"
                 (build-uri 'http #:host "other-place"))
  (pass-if-only-parse location "#foo"
                      (build-uri-reference #:fragment "foo"))
  (pass-if-only-parse location "/#foo"
                      (build-uri-reference #:path "/" #:fragment "foo"))
  (pass-if-parse location "/foo"
                 (build-uri-reference #:path "/foo"))
  (pass-if-parse location "//server/foo"
                 (build-uri-reference #:host "server" #:path "/foo"))
  (pass-if-parse proxy-authenticate "Basic realm=\"guile\""
                 '((basic (realm . "guile"))))
  (pass-if-parse retry-after "Tue, 15 Nov 1994 08:12:31 GMT"
                 (string->date "Tue, 15 Nov 1994 08:12:31 +0000"
                         "~a, ~d ~b ~Y ~H:~M:~S ~z"))
  (pass-if-parse retry-after "20" 20)
  (pass-if-parse server "guile!" "guile!")
  (pass-if-parse vary "*" '*)
  (pass-if-parse vary "foo, bar" '(foo bar))
  (pass-if-parse www-authenticate "Basic realm=\"guile\""
                 '((basic (realm . "guile")))))

(with-test-prefix "chunked encoding"
  (let* ((s "5\r\nFirst\r\nA\r\n line\n Sec\r\n8\r\nond line\r\n0\r\n\r\n")
         (p (make-chunked-input-port (open-input-string s))))
    (pass-if-equal
        "First line\n Second line"
        (get-string-all p))
    (pass-if (port-eof? (make-chunked-input-port (open-input-string "0\r\n"))))

    (pass-if-equal "reads chunks without buffering"
        ;; Make sure the chunked input port does not read more than what
        ;; the client asked.  See <http://bugs.gnu.org/19939>
        `("First " "chunk." "Second " "chunk."
          (1 1 1 6 6 1 1
           1 1 1 7 6 1 1))
      (let* ((str      "C\r\nFirst chunk.\r\nD\r\nSecond chunk.\r\n")
             (requests '())
             (read!    (let ((port (open-input-string str)))
                         (lambda (bv index count)
                           (set! requests (cons count requests))
                           (let ((n (get-bytevector-n! port bv index
                                                       count)))
                             (if (eof-object? n) 0 n)))))
             (input    (make-custom-binary-input-port "chunky" read!
                                                      #f #f #f))
             (port     (make-chunked-input-port input)))
        (setvbuf input 'none)
        (setvbuf port 'none)
        (list (utf8->string (get-bytevector-n port 6))
              (utf8->string (get-bytevector-n port 6))
              (utf8->string (get-bytevector-n port 7))
              (utf8->string (get-bytevector-n port 6))
              (reverse requests))))

    (pass-if-equal "reads across chunk boundaries"
        ;; Same, but read across chunk boundaries.
        `("First " "chunk.Second " "chunk."
          (1 1 1 6 6 1 1
           1 1 1 7 6 1 1))
      (let* ((str      "C\r\nFirst chunk.\r\nD\r\nSecond chunk.\r\n")
             (requests '())
             (read!    (let ((port (open-input-string str)))
                         (lambda (bv index count)
                           (set! requests (cons count requests))
                           (let ((n (get-bytevector-n! port bv index
                                                       count)))
                             (if (eof-object? n) 0 n)))))
             (input    (make-custom-binary-input-port "chunky" read!
                                                      #f #f #f))
             (port     (make-chunked-input-port input)))
        (setvbuf input 'none)
        (setvbuf port 'none)
        (list (utf8->string (get-bytevector-n port 6))
              (utf8->string (get-bytevector-n port 13))
              (utf8->string (get-bytevector-n port 6))
              (reverse requests)))))

  (pass-if-equal "EOF instead of chunk header"
      "Only chunk."
    ;; Omit the second chunk header, leading to a premature EOF.  This
    ;; used to cause 'read-chunk-header' to throw to wrong-type-arg.
    ;; See the backtrace at
    ;; <http://debbugs.gnu.org/cgi/bugreport.cgi?bug=19976#5>.
    (let* ((str  "B\r\nOnly chunk.")
           (port (make-chunked-input-port (open-input-string str))))
      (get-string-all port)))

  (pass-if-equal
      (call-with-output-string
       (lambda (out-raw)
         (let ((out-chunked (make-chunked-output-port out-raw
                                                      #:keep-alive? #t)))
           (display "First chunk" out-chunked)
           (force-output out-chunked)
           (display "Second chunk" out-chunked)
           (force-output out-chunked)
           (display "Third chunk" out-chunked)
           (close-port out-chunked))))
      "b\r\nFirst chunk\r\nc\r\nSecond chunk\r\nb\r\nThird chunk\r\n0\r\n\r\n"))