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