| 1 |
;; |
| 2 |
;; testing rfc.* modules |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use gauche.sequence) |
| 7 |
(use gauche.process) |
| 8 |
(use util.match) |
| 9 |
(use srfi.19) |
| 10 |
(use srfi.13) |
| 11 |
(test-start "rfc") |
| 12 |
|
| 13 |
;; rfc.822 test is in ext/rfc |
| 14 |
|
| 15 |
;;-------------------------------------------------------------------- |
| 16 |
(test-section "rfc.base64") |
| 17 |
(use rfc.base64) |
| 18 |
(test-module 'rfc.base64) |
| 19 |
|
| 20 |
;; NB: encode from/decode to uvector are tested in gauche.vport. |
| 21 |
|
| 22 |
(test* "base64 encode" "" (base64-encode-message "")) |
| 23 |
(test* "base64 encode" "YQ==" (base64-encode-message "a")) |
| 24 |
(test* "base64 encode" "MA==" (base64-encode-message "0")) |
| 25 |
(test* "base64 encode" "Cg==" (base64-encode-message "\n")) |
| 26 |
(test* "base64 encode" "YTA=" (base64-encode-message "a0")) |
| 27 |
(test* "base64 encode" "YTAK" (base64-encode-message "a0\n")) |
| 28 |
(test* "base64 encode" "PQk0" (base64-encode-message "=\t4")) |
| 29 |
(test* "base64 encode" "eTQ5YQ==" (base64-encode-message "y49a")) |
| 30 |
(test* "base64 encode" "RWdqYWk=" (base64-encode-message "Egjai")) |
| 31 |
(test* "base64 encode" "OTNiamFl" (base64-encode-message "93bjae")) |
| 32 |
(test* "base64 encode" "QkFSMGVyOQ==" (base64-encode-message "BAR0er9")) |
| 33 |
|
| 34 |
(test* "base64 encode w/ line width (default)" |
| 35 |
"MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2\n" |
| 36 |
(base64-encode-message "012345678901234567890123456789012345678901234567890123456")) |
| 37 |
(test* "base64 encode w/ line width 10, e1" |
| 38 |
"MDEyMzQ1Ng\n==" |
| 39 |
(base64-encode-message "0123456" :line-width 10)) |
| 40 |
(test* "base64 encode w/ line width 11, e1" |
| 41 |
"MDEyMzQ1Ng=\n=" |
| 42 |
(base64-encode-message "0123456" :line-width 11)) |
| 43 |
(test* "base64 encode w/ line width 12, e1" |
| 44 |
"MDEyMzQ1Ng==\n" |
| 45 |
(base64-encode-message "0123456" :line-width 12)) |
| 46 |
(test* "base64 encode w/ line width 11, e2" |
| 47 |
"MDEyMzQ1Njc\n=" |
| 48 |
(base64-encode-message "01234567" :line-width 11)) |
| 49 |
(test* "base64 encode w/ line width 12, e2" |
| 50 |
"MDEyMzQ1Njc=\n" |
| 51 |
(base64-encode-message "01234567" :line-width 12)) |
| 52 |
(test* "base64 encode w/ line width 4" |
| 53 |
"MDEy\nMzQ=\n" |
| 54 |
(base64-encode-message "01234" :line-width 4)) |
| 55 |
(test* "base64 encode w/ line width 3" |
| 56 |
"MDE\nyMz\nQ=" |
| 57 |
(base64-encode-message "01234" :line-width 3)) |
| 58 |
(test* "base64 encode w/ line width 2" |
| 59 |
"MD\nEy\nMz\nQ=\n" |
| 60 |
(base64-encode-message "01234" :line-width 2)) |
| 61 |
(test* "base64 encode w/ line width 1" |
| 62 |
"M\nD\nE\ny\nM\nz\nQ\n=\n" |
| 63 |
(base64-encode-message "01234" :line-width 1)) |
| 64 |
(test* "base64 encode w/ line width 0" |
| 65 |
"MDEyMzQ=" |
| 66 |
(base64-encode-message "01234" :line-width 0)) |
| 67 |
|
| 68 |
(test* "base64 decode" "" (base64-decode-string-to <string> "")) |
| 69 |
(test* "base64 decode" "a" (base64-decode-string-to <string> "YQ==")) |
| 70 |
(test* "base64 decode" "a" (base64-decode-string-to <string> "YQ=")) |
| 71 |
(test* "base64 decode" "a" (base64-decode-string-to <string> "YQ")) |
| 72 |
(test* "base64 decode" "a0" (base64-decode-string-to <string> "YTA=")) |
| 73 |
(test* "base64 decode" "a0" (base64-decode-string-to <string> "YTA")) |
| 74 |
(test* "base64 decode" "a0\n" (base64-decode-string-to <string> "YTAK")) |
| 75 |
(test* "base64 decode" "y49a" (base64-decode-string-to <string> "eTQ5YQ==")) |
| 76 |
(test* "base64 decode" "Egjai" (base64-decode-string-to <string> "RWdqYWk=")) |
| 77 |
(test* "base64 decode" "93bjae" (base64-decode-string-to <string> "OTNiamFl")) |
| 78 |
(test* "base64 decode" "BAR0er9" (base64-decode-string-to <string> "QkFSMGVyOQ==")) |
| 79 |
(test* "base64 decode" "BAR0er9" (base64-decode-string-to <string> "QkFS\r\nMGVyOQ\r\n==")) |
| 80 |
|
| 81 |
(test* "base64 standard encode" "YTA+YTA/" (base64-encode-message "a0>a0?")) |
| 82 |
(test* "base64 standard decode" "a0>a0?" (base64-decode-string-to <string> "YTA+YTA/")) |
| 83 |
(test* "base64 url-safe encode" "YTA-YTA_" (base64-encode-message "a0>a0?" :url-safe #t)) |
| 84 |
(test* "base64 url-safe decode" "a0>a0?" (base64-decode-string-to <string> "YTA-YTA_" :url-safe #t)) |
| 85 |
|
| 86 |
(test* "base64 standard encode (digits)" "YTA+YTA/" (base64-encode-message "a0>a0?" :digits "+/")) |
| 87 |
(test* "base64 standard decode (digits)" "a0>a0?" (base64-decode-string-to <string> "YTA+YTA/" :digits "+/")) |
| 88 |
(test* "base64 url-safe encode (digits)" "YTA-YTA_" (base64-encode-message "a0>a0?" :digits "-_")) |
| 89 |
(test* "base64 url-safe decode (digits)" "a0>a0?" (base64-decode-string-to <string> "YTA-YTA_" :digits "-_")) |
| 90 |
(test* "base64 weird encode (digits)" "YTA.YTA?" (base64-encode-message "a0>a0?" :digits ".?")) |
| 91 |
(test* "base64 weird decode (digits)" "a0>a0?" (base64-decode-string-to <string> "YTA.YTA?" :digits ".?")) |
| 92 |
|
| 93 |
(test* "base64 omit-padding" "YQ" (base64-encode-message "a" :omit-padding #t)) |
| 94 |
(test* "base64 omit-padding" "YTA" (base64-encode-message "a0" :omit-padding #t)) |
| 95 |
|
| 96 |
;; Test data from RFC4648 |
| 97 |
(define (test-base32 orig encoded encoded-hex) |
| 98 |
(define (test-1 name encoded encoder decoder) |
| 99 |
(define orig-bv (string->u8vector orig)) |
| 100 |
(define enc-no-pads (string-trim-right encoded #\=)) |
| 101 |
(test* #"~name encode" encoded (encoder orig)) |
| 102 |
(test* #"~name decode" orig (decoder <string> encoded)) |
| 103 |
(test* #"~name encode (bv)" encoded (encoder orig-bv)) |
| 104 |
(test* #"~name decode (bv)" orig-bv (decoder <u8vector> encoded)) |
| 105 |
(test* #"~name encode (no pads)" enc-no-pads (encoder orig :omit-padding #t)) |
| 106 |
(test* #"~name decode (no pads)" orig (decoder <string> enc-no-pads)) |
| 107 |
(when (string-index encoded #\=) |
| 108 |
(test* #"~name decode (no pads, error)" (test-error) |
| 109 |
(decoder <string> enc-no-pads :strict #t)))) |
| 110 |
|
| 111 |
(test-1 "base32" encoded base32-encode-message base32-decode-string-to) |
| 112 |
(test-1 "base32hex" encoded-hex base32hex-encode-message base32hex-decode-string-to) |
| 113 |
) |
| 114 |
|
| 115 |
(let ((data '(;orig base32 base32hex |
| 116 |
("" "" "") |
| 117 |
("f" "MY======" "CO======") |
| 118 |
("fo" "MZXQ====" "CPNG====") |
| 119 |
("foo" "MZXW6===" "CPNMU===") |
| 120 |
("foob" "MZXW6YQ=" "CPNMUOG=") |
| 121 |
("fooba" "MZXW6YTB" "CPNMUOJ1") |
| 122 |
("foobar" "MZXW6YTBOI======" "CPNMUOJ1E8======")))) |
| 123 |
(for-each (cut apply test-base32 <>) data)) |
| 124 |
|
| 125 |
(define (test-base16 orig encoded) |
| 126 |
(define orig-bv (string->u8vector orig)) |
| 127 |
(test* "base16 encode" encoded (base16-encode-message orig)) |
| 128 |
(test* "base16 decode" orig (base16-decode-string-to <string> encoded)) |
| 129 |
(test* "base16 encode (bv)" encoded (base16-encode-message orig-bv)) |
| 130 |
(test* "base16 decode (bv)" orig-bv (base16-decode-string-to <u8vector> encoded))) |
| 131 |
|
| 132 |
(let ((data '(("" "") |
| 133 |
("f" "66") |
| 134 |
("fo" "666F") |
| 135 |
("foo" "666F6F") |
| 136 |
("foob" "666F6F62") |
| 137 |
("fooba" "666F6F6261") |
| 138 |
("foobar" "666F6F626172")))) |
| 139 |
(for-each (cut apply test-base16 <>) data)) |
| 140 |
|
| 141 |
;;-------------------------------------------------------------------- |
| 142 |
(test-section "rfc.quoted-printable") |
| 143 |
(use rfc.quoted-printable) |
| 144 |
(test-module 'rfc.quoted-printable) |
| 145 |
|
| 146 |
(test* "encode" "abcd=0Cefg" |
| 147 |
(quoted-printable-encode-message "abcd\x0cefg")) |
| 148 |
(test* "encode" |
| 149 |
"abcd\r\nefg" |
| 150 |
(quoted-printable-encode-message "abcd\r\nefg")) |
| 151 |
(test* "encode (tab/space at eol)" |
| 152 |
"abcd=09\r\nefg=20\r\n" |
| 153 |
(quoted-printable-encode-message "abcd\t\r\nefg \r\n")) |
| 154 |
(test* "encode (soft line break)" |
| 155 |
"0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abc=\r\ndefghij0123456789abcdefghij" |
| 156 |
(quoted-printable-encode-message "0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij")) |
| 157 |
(test* "encode (soft line break w/line-width)" |
| 158 |
"0123456789abcdefg=\r\nhij0123456789abcd=\r\nefghij" |
| 159 |
(quoted-printable-encode-message |
| 160 |
"0123456789abcdefghij0123456789abcdefghij" |
| 161 |
:line-width 20)) |
| 162 |
(test* "encode (soft line break w/line-width)" |
| 163 |
"0123456789abcdef=3D=\r\nghij0123456789a=3D=\r\n=3Dbcdefghij" |
| 164 |
(quoted-printable-encode-message |
| 165 |
"0123456789abcdef=ghij0123456789a==bcdefghij" |
| 166 |
:line-width 20)) |
| 167 |
(test* "encode (soft line break w/line-width lower bound)" |
| 168 |
"a=\r\n=3F=\r\nb" |
| 169 |
(quoted-printable-encode-message "a?b" :line-width 4)) |
| 170 |
(test* "encode (no line break)" |
| 171 |
"0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij" |
| 172 |
(quoted-printable-encode-message "0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij0123456789abcdefghij" |
| 173 |
:line-width #f)) |
| 174 |
(test* "encode (hard line break)" |
| 175 |
"a\r\nb\r\nc\r\n" |
| 176 |
(quoted-printable-encode-message "a\rb\nc\r\n")) |
| 177 |
(test* "encode (binary)" |
| 178 |
"a=0Db=0Ac=0D=0A" |
| 179 |
(quoted-printable-encode-message "a\rb\nc\r\n" :binary #t)) |
| 180 |
|
| 181 |
(test* "decode" "\x01\x08abcde=\r\n" |
| 182 |
(quoted-printable-decode-string-to <string> "=01=08abc=64=65=3D\r\n")) |
| 183 |
(test* "decode (soft line break)" |
| 184 |
"Now's the time for all folk to come to the aid of their country." |
| 185 |
(quoted-printable-decode-string-to <string> "Now's the time =\r\nfor all folk to come= \r\n to the aid of their country.")) |
| 186 |
(test* "decode (robustness)" |
| 187 |
"foo=1qr = j\r\n" |
| 188 |
(quoted-printable-decode-string-to <string> "foo=1qr = j\r\n=")) |
| 189 |
|
| 190 |
|
| 191 |
;;-------------------------------------------------------------------- |
| 192 |
(test-section "rfc.cookie") |
| 193 |
(use rfc.cookie) |
| 194 |
(test-module 'rfc.cookie) |
| 195 |
(use scheme.list) |
| 196 |
(use srfi.19) |
| 197 |
|
| 198 |
(test* "parse, old" '(("foo" "bar") |
| 199 |
("aaa" "bbb" :path "/a/b" :domain "a.b.com") |
| 200 |
("x12" "Yy \"yY\" ;; Zz" :port "100, 200, 300") |
| 201 |
("zzz" #f) |
| 202 |
("_n_" "") |
| 203 |
("mmm" "ppp")) |
| 204 |
(parse-cookie-string " foo=bar; aaa = bbb ; $Path=/a/b;$Domain =a.b.com;x12=\"Yy \\\"yY\\\" ;; Zz\"; $Port=\"100, 200, 300\";zzz ;_n_=;mmm=ppp")) |
| 205 |
|
| 206 |
(test* "parse, new" '(("$Version" "1") |
| 207 |
("foo" "bar") |
| 208 |
("aaa" "bbb" :path "/a/b" :domain "a.b.com") |
| 209 |
("x12" "Yy \"yY\" ;; Zz" :port "100, 200, 300") |
| 210 |
("zzz" #f) |
| 211 |
("_n_" "") |
| 212 |
("mmm" "ppp")) |
| 213 |
(parse-cookie-string "$Version=1; foo=bar, aaa = bbb ; $Path=/a/b;$Domain =a.b.com,x12=\"Yy \\\"yY\\\" ;; Zz\"; $Port=\"100, 200, 300\",zzz ,_n_=,mmm=ppp")) |
| 214 |
|
| 215 |
(test* "parse, new" '(("foo" "bar") |
| 216 |
("aaa" "bbb" :path "/a/b" :domain "a.b.com") |
| 217 |
("x12" "Yy \"yY\" ;; Zz" :port "100, 200, 300") |
| 218 |
("zzz" #f) |
| 219 |
("_n_" "") |
| 220 |
("mmm" "ppp")) |
| 221 |
(parse-cookie-string " foo=bar, aaa = bbb ; $Path=/a/b;$Domain =a.b.com,x12=\"Yy \\\"yY\\\" ;; Zz\"; $Port=\"100, 200, 300\",zzz ,_n_=,mmm=ppp" |
| 222 |
1)) |
| 223 |
|
| 224 |
(define *cookie-spec* |
| 225 |
'(("guest-id" "foo123" |
| 226 |
:domain "foo.com" :path "/abc" |
| 227 |
:expires 1000000000 :max-age 864000 |
| 228 |
:discard #t :comment "hogehoge" |
| 229 |
:comment-url "http://foo.com/hogehoge" |
| 230 |
:port "80, 8080" :version 1) |
| 231 |
("guest-account" "87975348" |
| 232 |
:domain "zzz.com" :path "/zzz" |
| 233 |
:discard #f :secure #t :comment "ZzzZzz, OooOoo" |
| 234 |
:comment-url "http://foo.com/hogehoge"))) |
| 235 |
|
| 236 |
(test* "cookie" |
| 237 |
'("guest-id=foo123;Domain=foo.com;Path=/abc;Expires=Sun, 09-Sep-2001 01:46:40 GMT;Max-Age=864000;Discard;Comment=hogehoge;CommentURL=\"http://foo.com/hogehoge\";Port=\"80, 8080\";Version=1" |
| 238 |
"guest-account=87975348;Domain=zzz.com;Path=/zzz;Secure;Comment=\"ZzzZzz, OooOoo\";CommentURL=\"http://foo.com/hogehoge\"") |
| 239 |
(construct-cookie-string *cookie-spec*)) |
| 240 |
|
| 241 |
;; test for formatting SRFI-19 time/date |
| 242 |
(test* "cookie, SRFI-19 date" |
| 243 |
'("foo=bar;Expires=Sun, 09-Sep-2001 01:46:40 GMT" |
| 244 |
"foo=baz;Expires=Sun, 09-Sep-2001 01:46:40 GMT") |
| 245 |
(construct-cookie-string |
| 246 |
`(("foo" "bar" :expires ,(make-time time-utc 0 1000000000)) |
| 247 |
("foo" "baz" :expires ,(make-date 0 40 46 1 9 9 2001 0))) |
| 248 |
0)) |
| 249 |
|
| 250 |
(test* "cookie date" (make-date 0 1 2 3 11 12 2023 0) |
| 251 |
(parse-cookie-date "Sun, 11-Dec-2023 03:02:01 GMT")) |
| 252 |
(test* "cookie date" (make-date 0 3 4 5 2 1 1999 0) |
| 253 |
(parse-cookie-date "Sun, 2-January-99 5:4:3 garbage")) |
| 254 |
|
| 255 |
(let ([jar (make-cookie-jar)]) |
| 256 |
(define (test-jar n host port path expect) |
| 257 |
(test* #"cookie-jar-get ~n" expect |
| 258 |
(map (^e (list (~ e'name) (~ e'value))) |
| 259 |
(cookie-jar-get jar host port path)) |
| 260 |
(cut lset= equal? <> <>))) |
| 261 |
|
| 262 |
(cookie-jar-put! jar "foo.example.com" "/index.html" |
| 263 |
'(("a" "ABC" :path "/" :domain "example.com") |
| 264 |
("d" "DEF" :path "/" :domain "example.org") |
| 265 |
("g" "GHI" :path "/" :domain "foo.example.com"))) |
| 266 |
(test-jar 1 "foo.example.com" 80 "/" |
| 267 |
'(("a" "ABC") ("g" "GHI"))) |
| 268 |
|
| 269 |
(cookie-jar-put! jar "foo.bar.example.org" "/x/y/z" |
| 270 |
'(("x" "XYZ" :domain "bar.example.org") |
| 271 |
("y" "YZX" :domain "foo.example.org") |
| 272 |
("z" "ZXY" :path "/x/") |
| 273 |
("w" "WXY" :path "/x/w"))) |
| 274 |
(test-jar 2 "foo.bar.example.org" #f "/x/y" |
| 275 |
'(("x" "XYZ") ("z" "ZXY"))) |
| 276 |
(test-jar 3 "foo.bar.example.org" #f "/x/w" |
| 277 |
'(("z" "ZXY") ("w" "WXY"))) |
| 278 |
) |
| 279 |
|
| 280 |
;;-------------------------------------------------------------------- |
| 281 |
(test-section "rfc.ftp") |
| 282 |
(use rfc.ftp) |
| 283 |
(test-module 'rfc.ftp) |
| 284 |
|
| 285 |
;;-------------------------------------------------------------------- |
| 286 |
(test-section "rfc.icmp") |
| 287 |
(use rfc.icmp) |
| 288 |
(use gauche.uvector) |
| 289 |
(test-module 'rfc.icmp) |
| 290 |
|
| 291 |
;; WRITEME |
| 292 |
|
| 293 |
;;-------------------------------------------------------------------- |
| 294 |
(test-section "rfc.ip") |
| 295 |
(use rfc.ip) |
| 296 |
(use gauche.uvector) |
| 297 |
(test-module 'rfc.ip) |
| 298 |
|
| 299 |
(test* "ip-version" 4 |
| 300 |
(ip-version '#u8(69 0 0 36 139 12 0 0 64 1 241 202 |
| 301 |
127 0 0 1 127 0 0 1 0 0 205 245 50 10 0 0) |
| 302 |
0)) |
| 303 |
(test* "ip-version" 6 |
| 304 |
(ip-version '#u8(#x60 0 0 0 0 0 17 0 |
| 305 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 |
| 306 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 307 |
0)) |
| 308 |
|
| 309 |
(test* "ip-protocol" 1 |
| 310 |
(ip-protocol '#u8(69 0 0 36 139 12 0 0 64 1 241 202 |
| 311 |
127 0 0 1 127 0 0 1 0 0 205 245 50 10 0 0) |
| 312 |
0)) |
| 313 |
(test* "ip-protocol" 17 |
| 314 |
(ip-protocol '#u8(#x60 0 0 0 0 0 17 0 |
| 315 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 |
| 316 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1) |
| 317 |
0)) |
| 318 |
(test* "ip-protocol" 17 |
| 319 |
(ip-protocol '#u8(#x60 0 0 0 0 0 0 0 |
| 320 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 |
| 321 |
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 |
| 322 |
60 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 323 |
43 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 324 |
17 0 0 0 0 0 0 0) |
| 325 |
0)) |
| 326 |
|
| 327 |
;;-------------------------------------------------------------------- |
| 328 |
;; NB: rfc.json test is moved to under ext/peg, since it totally |
| 329 |
;; depends on parser.peg. |
| 330 |
|
| 331 |
;;-------------------------------------------------------------------- |
| 332 |
;; NB: rfc.mime test is in ext/mime |
| 333 |
|
| 334 |
;;-------------------------------------------------------------------- |
| 335 |
(test-section "rfc.uri") |
| 336 |
(use rfc.uri) |
| 337 |
(test-module 'rfc.uri) |
| 338 |
|
| 339 |
(test* "encode" "abc%3C%20%3E%20%22%20%23%25%7B%7C%7D%5C%5E" |
| 340 |
(uri-encode-string "abc< > \" #%{|}\\^")) |
| 341 |
(test* "encode (noescape)" ".a%21%2Ap" |
| 342 |
(uri-encode-string ".a!*p" :noescape *rfc3986-unreserved-char-set*)) |
| 343 |
(test* "decode" "abc< > \" #%?{|}\\^" |
| 344 |
(uri-decode-string "abc%3c%20%3E%20%22%20%23%25%3f%7B%7C%7d%5c%5e")) |
| 345 |
(test* "decode" "abc<+>+\"+#%?{|}\\^" |
| 346 |
(uri-decode-string "abc%3C+%3e+%22+%23%25%3F%7b%7c%7D%5C%5E")) |
| 347 |
(test* "decode" "abc< > \" #%?{|}\\^" |
| 348 |
(uri-decode-string "abc%3c+%3e+%22+%23%25%3f%7b%7c%7d%5c%5e" |
| 349 |
:cgi-decode #t)) |
| 350 |
(test* "decode" "%" (uri-decode-string "%")) |
| 351 |
(test* "decode" "a%" (uri-decode-string "a%")) |
| 352 |
(test* "decode" "a%y" (uri-decode-string "a%y")) |
| 353 |
(test* "decode" "a%ay" (uri-decode-string "a%ay")) |
| 354 |
(test* "decode" "" (uri-decode-string "")) |
| 355 |
|
| 356 |
(test* "uri-scheme&specific" '("http" "//practical-scheme.net/gauche/") |
| 357 |
(receive r |
| 358 |
(uri-scheme&specific "http://practical-scheme.net/gauche/") |
| 359 |
r)) |
| 360 |
|
| 361 |
(test* "uri-scheme&specific" '(#f "/dev/tty") |
| 362 |
(receive r |
| 363 |
(uri-scheme&specific "/dev/tty") |
| 364 |
r)) |
| 365 |
|
| 366 |
(test* "uri-decompose-hierarchical" '("www.example.com:8080" |
| 367 |
"/about/company" |
| 368 |
"abc=def&ghi%20" |
| 369 |
"zzz") |
| 370 |
(receive r |
| 371 |
(uri-decompose-hierarchical |
| 372 |
"//www.example.com:8080/about/company?abc=def&ghi%20#zzz") |
| 373 |
r)) |
| 374 |
|
| 375 |
(test* "uri-decompose-hierarchical" '("www.example.com:8080" |
| 376 |
"/about/company" |
| 377 |
#f |
| 378 |
"zzz") |
| 379 |
(receive r |
| 380 |
(uri-decompose-hierarchical |
| 381 |
"//www.example.com:8080/about/company#zzz") |
| 382 |
r)) |
| 383 |
|
| 384 |
(test* "uri-decompose-hierarchical" '("www.example.com:8080" |
| 385 |
"/" |
| 386 |
"abc" |
| 387 |
#f) |
| 388 |
(receive r |
| 389 |
(uri-decompose-hierarchical |
| 390 |
"//www.example.com:8080/?abc") |
| 391 |
r)) |
| 392 |
|
| 393 |
(test* "uri-decompose-hierarchical" '("www.example.com:8080" |
| 394 |
#f |
| 395 |
#f |
| 396 |
#f) |
| 397 |
(receive r (uri-decompose-hierarchical "//www.example.com:8080") r)) |
| 398 |
|
| 399 |
(test* "uri-decompose-hierarchical" '((#f #f #f #f) |
| 400 |
("" #f #f #f) |
| 401 |
(#f "/" #f #f) |
| 402 |
("" "/" #f #f)) |
| 403 |
(map (lambda (specific) |
| 404 |
(receive r (uri-decompose-hierarchical specific) r)) |
| 405 |
'("" "//" "/" "///"))) |
| 406 |
|
| 407 |
(test* "uri-decompose-authority" '(#f "www.example.com" #f) |
| 408 |
(receive r (uri-decompose-authority "www.example.com") r)) |
| 409 |
(test* "uri-decompose-authority" '(#f "www.example.com" "8080") |
| 410 |
(receive r (uri-decompose-authority "www.example.com:8080") r)) |
| 411 |
(test* "uri-decompose-authority" '("foo:bar" "www.example.com" #f) |
| 412 |
(receive r (uri-decompose-authority "foo:[email protected]") r)) |
| 413 |
(test* "uri-decompose-authority" '(#f "::1" #f) |
| 414 |
(receive r (uri-decompose-authority "[::1]") r)) |
| 415 |
(test* "uri-decompose-authority" '(#f "::1" "8080") |
| 416 |
(receive r (uri-decompose-authority "[::1]:8080") r)) |
| 417 |
(test* "uri-decompose-authority" '("foo:bar" "::1" #f) |
| 418 |
(receive r (uri-decompose-authority "foo:bar@[::1]") r)) |
| 419 |
|
| 420 |
(test* "uri-parse" '("https" "shiro" "www.example.com" 443 "/login" "abc" "def") |
| 421 |
(receive r (uri-parse "https://[email protected]:443/login?abc#def") |
| 422 |
r)) |
| 423 |
(test* "uri-parse" '("ftp" "anonymous:anonymous" "ftp.example.com" #f |
| 424 |
"/pub/foo" #f #f) |
| 425 |
(receive r (uri-parse "ftp://anonymous:[email protected]/pub/foo") |
| 426 |
r)) |
| 427 |
(test* "uri-parse" '("file" #f #f #f "/usr/local/lib/abc" #f #f) |
| 428 |
(receive r (uri-parse "file:/usr/local/lib/abc") |
| 429 |
r)) |
| 430 |
(test* "uri-parse" '(#f #f #f #f "/usr/local/lib" #f #f) |
| 431 |
(receive r (uri-parse "/usr/local/lib") r)) |
| 432 |
(test* "uri-parse" '("mailto" #f #f #f "[email protected]" #f #f) |
| 433 |
(receive r (uri-parse "mailto:[email protected]") r)) |
| 434 |
|
| 435 |
(let ([base0 "http://a/b/c/d;p?q"]) |
| 436 |
(define (t base rel expect) |
| 437 |
(test* (format "merging ~s onto ~s" rel base) |
| 438 |
expect (uri-merge base rel))) |
| 439 |
(define t0 (pa$ t base0)) |
| 440 |
;; examples given in RFC3986 section 5.4 |
| 441 |
;; normal path |
| 442 |
(t0 "g:h" "g:h") |
| 443 |
(t0 "g" "http://a/b/c/g") |
| 444 |
(t0 "./g" "http://a/b/c/g") |
| 445 |
(t0 "g/" "http://a/b/c/g/") |
| 446 |
(t0 "/g" "http://a/g") |
| 447 |
(t0 "//g" "http://g") |
| 448 |
(t0 "?y" "http://a/b/c/d;p?y") |
| 449 |
(t0 "g?y" "http://a/b/c/g?y") |
| 450 |
(t0 "#s" "http://a/b/c/d;p?q#s") |
| 451 |
(t0 "g#s" "http://a/b/c/g#s") |
| 452 |
(t0 "g?y#s" "http://a/b/c/g?y#s") |
| 453 |
(t0 ";x" "http://a/b/c/;x") |
| 454 |
(t0 "g;x" "http://a/b/c/g;x") |
| 455 |
(t0 "g;x?y#s" "http://a/b/c/g;x?y#s") |
| 456 |
(t0 "" "http://a/b/c/d;p?q") |
| 457 |
(t0 "." "http://a/b/c/") |
| 458 |
(t0 "./" "http://a/b/c/") |
| 459 |
(t0 ".." "http://a/b/") |
| 460 |
(t0 "../" "http://a/b/") |
| 461 |
(t0 "../g" "http://a/b/g") |
| 462 |
(t0 "../.." "http://a/") |
| 463 |
(t0 "../../" "http://a/") |
| 464 |
(t0 "../../g" "http://a/g") |
| 465 |
;; failure path |
| 466 |
(t0 "../../../g" "http://a/g") |
| 467 |
(t0 "../../../../g" "http://a/g") |
| 468 |
(t0 "/./g" "http://a/g") |
| 469 |
(t0 "/../g" "http://a/g") |
| 470 |
(t0 "g." "http://a/b/c/g.") |
| 471 |
(t0 ".g" "http://a/b/c/.g") |
| 472 |
(t0 "g.." "http://a/b/c/g..") |
| 473 |
(t0 "..g" "http://a/b/c/..g") |
| 474 |
(t0 "./../g" "http://a/b/g") |
| 475 |
(t0 "./g/." "http://a/b/c/g/") |
| 476 |
(t0 "g/./h" "http://a/b/c/g/h") |
| 477 |
(t0 "g/../h" "http://a/b/c/h") |
| 478 |
(t0 "g;x=1/./y" "http://a/b/c/g;x=1/y") |
| 479 |
(t0 "g;x=1/../y" "http://a/b/c/y") |
| 480 |
(t0 "g?y/./x" "http://a/b/c/g?y/./x") |
| 481 |
(t0 "g?y/../x" "http://a/b/c/g?y/../x") |
| 482 |
(t0 "g#s/./x" "http://a/b/c/g#s/./x") |
| 483 |
(t0 "g#s/../x" "http://a/b/c/g#s/../x") |
| 484 |
(t0 "http:g" "http:g") ;; for strict parser |
| 485 |
|
| 486 |
;; some edge cases. the first case works since we do pre-normalization |
| 487 |
;; of the base URI (RFC3986 5.2.1), which is optional. |
| 488 |
(t "http://example.com/foo/.." "./" "http://example.com/") |
| 489 |
(t "http://example.com/" "./foo/bar/.." "http://example.com/foo/") |
| 490 |
|
| 491 |
;; empty base-path case |
| 492 |
(t "http://example.com" "foo" "http://example.com/foo") |
| 493 |
(t "http://example.com" "./foo" "http://example.com/foo") |
| 494 |
(t "http://example.com" "../foo" "http://example.com/foo") |
| 495 |
) |
| 496 |
|
| 497 |
;; data uri scheme. NB: this uses gauche.vport internally (for now), |
| 498 |
;; so it depends on ext/vport. |
| 499 |
|
| 500 |
(let ([d0 "data:text/plain;charset=utf-8,%21%40%23%24%25%5E%26%2A%28%29_abc"] |
| 501 |
[d1 "data:application/octed-stream;base64,IUAjJCVeJiooKV9hYmM="]) |
| 502 |
(test* "data uri decode->encode (text)" d0 |
| 503 |
(receive (ct data) (uri-decompose-data d0) |
| 504 |
(uri-compose-data data))) |
| 505 |
(test* "data uri decode->encode (binary)" d1 |
| 506 |
(receive (ct data) (uri-decompose-data d1) |
| 507 |
(uri-compose-data data :content-type ct))) |
| 508 |
) |
| 509 |
|
| 510 |
;; uri-ref |
| 511 |
;; test-data : ((input (parts expected) ...) ...) |
| 512 |
(let ([data '(("http://[email protected]:8080/path/to/resource?query#frag" |
| 513 |
(scheme "http") |
| 514 |
(userinfo "user") |
| 515 |
(host "foo.bar.baz") |
| 516 |
(port 8080) |
| 517 |
(authority "//[email protected]:8080/") |
| 518 |
(scheme+authority "http://[email protected]:8080/") |
| 519 |
(host+port "foo.bar.baz:8080") |
| 520 |
(userinfo+host+port "[email protected]:8080") |
| 521 |
(path "/path/to/resource") |
| 522 |
(path+query "/path/to/resource?query") |
| 523 |
(query "query") |
| 524 |
(path+query+fragment "/path/to/resource?query#frag") |
| 525 |
(fragment "frag") |
| 526 |
((host+port path+query) |
| 527 |
("foo.bar.baz:8080" "/path/to/resource?query"))) |
| 528 |
("mailto:[email protected]?subject=Hello" |
| 529 |
(scheme "mailto") |
| 530 |
(userinfo #f) |
| 531 |
(host #f) |
| 532 |
(port #f) |
| 533 |
(authority "///") |
| 534 |
(scheme+authority "mailto:///") |
| 535 |
(host+port "") |
| 536 |
(userinfo+host+port "") |
| 537 |
(path "[email protected]") |
| 538 |
(path+query "[email protected]?subject=Hello") |
| 539 |
(query "subject=Hello") |
| 540 |
(path+query+fragment "[email protected]?subject=Hello") |
| 541 |
(fragment #f)))]) |
| 542 |
(define (test-uri-ref datum) |
| 543 |
(test* #"test uri-ref ~(car datum)" '() |
| 544 |
(let ([input (car datum)] |
| 545 |
[testers (cdr datum)] |
| 546 |
[bad '()]) |
| 547 |
(dolist [tester testers] |
| 548 |
(let1 r (uri-ref input (car tester)) |
| 549 |
(unless (equal? r (cadr tester)) |
| 550 |
(push! bad `(,(car tester) :expeced ,(cadr tester) |
| 551 |
:got ,r))))) |
| 552 |
bad))) |
| 553 |
|
| 554 |
(for-each test-uri-ref data)) |
| 555 |
|
| 556 |
;;-------------------------------------------------------------------- |
| 557 |
(test-section "rfc.http") |
| 558 |
|
| 559 |
(use rfc.http.tunnel) |
| 560 |
(test-module 'rfc.http.tunnel) |
| 561 |
(use rfc.http) |
| 562 |
(test-module 'rfc.http) |
| 563 |
|
| 564 |
|
| 565 |
(use rfc.822) |
| 566 |
(use rfc.mime) |
| 567 |
|
| 568 |
(test* "http-user-agent" #"gauche.http/~(gauche-version)" |
| 569 |
(and (parameter? http-user-agent) |
| 570 |
(http-user-agent))) |
| 571 |
|
| 572 |
(test* "http-compose-query" "/search?q=foo%20bar&n=20" |
| 573 |
(http-compose-query "/search" '((q "foo bar") (n 20)))) |
| 574 |
|
| 575 |
(define *http-port* 6726) |
| 576 |
|
| 577 |
(define (alist-equal? alis1 alis2) |
| 578 |
(define (%sort alis) |
| 579 |
(sort alis (lambda (a b) (string<? (car a) (car b))))) |
| 580 |
(and (list? alis1) |
| 581 |
(list? alis2) |
| 582 |
(equal? (%sort alis1) (%sort alis2)))) |
| 583 |
|
| 584 |
(define *simple-httpd* |
| 585 |
'( |
| 586 |
(use gauche.net) |
| 587 |
(use rfc.822) |
| 588 |
(define *http-port* 6726) |
| 589 |
|
| 590 |
(define %predefined-contents |
| 591 |
(let1 ht (make-hash-table 'string=?) |
| 592 |
(hash-table-put! ht "/redirect01" |
| 593 |
`("HTTP/1.x 302 Moved Temporarily\n" |
| 594 |
,#"Location: http://localhost:~|*http-port*|/redirect02\n\n")) |
| 595 |
(hash-table-put! ht "/redirect11" |
| 596 |
'("HTTP/1.x 302 Moved Temporarily\n" |
| 597 |
"Location: /redirect12\n\n")) |
| 598 |
(hash-table-put! ht "/redirect21" |
| 599 |
'("HTTP/1.x 301 Moved Permanently\n" |
| 600 |
"Location: /redirect12\n\n")) |
| 601 |
(hash-table-put! ht "/loop1" |
| 602 |
'("HTTP/1.x 302 Moved Temporarily\n" |
| 603 |
"Location: /loop2\n\n")) |
| 604 |
(hash-table-put! ht "/loop2" |
| 605 |
'("HTTP/1.x 302 Moved Temporarily\n" |
| 606 |
"Location: /loop1\n\n")) |
| 607 |
(hash-table-put! ht "/chunked" |
| 608 |
'("HTTP/1.x 200 OK\nTransfer-Encoding: chunked\n\n" |
| 609 |
"2\r\nOK\n0\r\n\r\n")) |
| 610 |
(hash-table-put! ht "/void" |
| 611 |
'("HTTP/1.x 404 Not found\nContent-type: text/plain\n" |
| 612 |
"Content-length: 9\n\nNot found")) |
| 613 |
ht)) |
| 614 |
|
| 615 |
(define (http-server socket) |
| 616 |
(let loop () |
| 617 |
(let* ([client (socket-accept socket)] |
| 618 |
[in (socket-input-port client)] |
| 619 |
[out (socket-output-port client)] |
| 620 |
[request-line (read-line in)]) |
| 621 |
(rxmatch-if (#/^(\S+) (\S+) HTTP\/1\.1$/ request-line) |
| 622 |
(#f method request-uri) |
| 623 |
(let* ([headers (rfc822-read-headers in)] |
| 624 |
[bodylen |
| 625 |
(cond [(assoc-ref headers "content-length") |
| 626 |
=> (^e (string->number (car e)))] |
| 627 |
[else 0])] |
| 628 |
[body (read-block bodylen in)]) |
| 629 |
(cond |
| 630 |
[(equal? request-uri "/exit") |
| 631 |
(socket-close client) |
| 632 |
(sys-exit 0)] |
| 633 |
[(hash-table-get %predefined-contents request-uri #f) |
| 634 |
=> (cut for-each (cut display <> out) <>)] |
| 635 |
[else |
| 636 |
(display "HTTP/1.x 200 OK\nContent-Type: text/plain\n\n" out) |
| 637 |
(write `(("method" ,method) |
| 638 |
("request-uri" ,request-uri) |
| 639 |
("request-body" ,(string-incomplete->complete body)) |
| 640 |
,@headers) |
| 641 |
out)]) |
| 642 |
(socket-close client) |
| 643 |
(loop)) |
| 644 |
(error "malformed request line:" request-line))))) |
| 645 |
|
| 646 |
(define (main args) |
| 647 |
(let1 socket (make-server-socket 'inet *http-port* :reuse-addr? #t) |
| 648 |
(print "ready") (flush) ; handshake |
| 649 |
(http-server socket) |
| 650 |
0))) |
| 651 |
) |
| 652 |
|
| 653 |
;; sys-fork causes problems on windows platform (both cygwin and mingw), so |
| 654 |
;; we go with run-process. We assume ./gosh is THE gosh we want to run. |
| 655 |
(with-output-to-file "testsrv.o" (lambda () (for-each write *simple-httpd*))) |
| 656 |
(let1 p (run-process '("./gosh" "-ftest" "./testsrv.o") :output :pipe) |
| 657 |
(read-line (process-output p)) ; handshake |
| 658 |
) |
| 659 |
|
| 660 |
(let ([expected `(("method" "GET") |
| 661 |
("request-uri" "/get") |
| 662 |
("request-body" "") |
| 663 |
("host" ,#"localhost:~*http-port*") |
| 664 |
("user-agent" ,#"gauche.http/~(gauche-version)") |
| 665 |
("my-header" "foo"))] |
| 666 |
[host #"localhost:~*http-port*"]) |
| 667 |
(define (req-body . args) |
| 668 |
(values-ref (apply http-request args) 2)) |
| 669 |
|
| 670 |
(test* "http-get, default string receiver" expected |
| 671 |
(receive (code headers body) |
| 672 |
(http-request 'GET host "/get" :my-header "foo") |
| 673 |
(and (equal? code "200") |
| 674 |
(equal? headers '(("content-type" "text/plain"))) |
| 675 |
(read-from-string body))) |
| 676 |
alist-equal?) |
| 677 |
|
| 678 |
(test* "http-get, custom receiver" expected |
| 679 |
(req-body 'GET host "/get" |
| 680 |
:receiver (lambda (code hdrs total retr) |
| 681 |
(let loop ((result '())) |
| 682 |
(receive (port size) (retr) |
| 683 |
(if (and size (= size 0)) |
| 684 |
result |
| 685 |
(loop (append result (read port))))))) |
| 686 |
:my-header "foo") |
| 687 |
alist-equal?) |
| 688 |
|
| 689 |
(sys-unlink "test.o") |
| 690 |
(test* "http-get, file receiver" expected |
| 691 |
(let1 f (req-body 'GET host "/get" |
| 692 |
:receiver (http-file-receiver "test.o") |
| 693 |
:my-header :foo) |
| 694 |
(and (equal? f "test.o") |
| 695 |
(with-input-from-file "test.o" read))) |
| 696 |
alist-equal?) |
| 697 |
(sys-unlink "test.o") |
| 698 |
|
| 699 |
(test* "http-get, file receiver (tmp)" expected |
| 700 |
(let1 f (req-body 'GET host "/get" |
| 701 |
:receiver (http-file-receiver "test.o" |
| 702 |
:temporary #t) |
| 703 |
:my-header :foo) |
| 704 |
(and (not (equal? f "test.o")) |
| 705 |
(begin0 (with-input-from-file f read) |
| 706 |
(sys-unlink f)))) |
| 707 |
alist-equal?) |
| 708 |
|
| 709 |
(let () |
| 710 |
(define cond-receiver |
| 711 |
(http-cond-receiver |
| 712 |
["404" (lambda (code hdrs total retr) |
| 713 |
(let1 sink (open-output-file |
| 714 |
(cond-expand |
| 715 |
[gauche.os.windows "NUL"] |
| 716 |
[else "/dev/null"])) |
| 717 |
(let loop () |
| 718 |
(receive (port size) (retr) |
| 719 |
(cond |
| 720 |
[(and size (= size 0)) (close-output-port sink) 404] |
| 721 |
[else (copy-port port sink :size size) (loop)])))))] ["200" (lambda (code hdrs total retr) |
| 722 |
(let loop ((result '())) |
| 723 |
(receive (port size) (retr) |
| 724 |
(if (and size (= size 0)) |
| 725 |
result |
| 726 |
(loop (append result (read port)))))))] |
| 727 |
)) |
| 728 |
|
| 729 |
(test* "http-get, cond-receiver" expected |
| 730 |
(req-body 'GET host "/get" |
| 731 |
:receiver cond-receiver |
| 732 |
:my-header :foo)) |
| 733 |
|
| 734 |
(test* "http-get, cond-receiver" 404 |
| 735 |
(req-body 'GET host "/void" |
| 736 |
:receiver cond-receiver |
| 737 |
:my-header :foo))) |
| 738 |
) |
| 739 |
|
| 740 |
(test* "http-get (redirect)" "/redirect02" |
| 741 |
(receive (code headers body) |
| 742 |
(http-request 'GET #"localhost:~*http-port*" "/redirect01") |
| 743 |
(cond ((assoc-ref (read-from-string body) "request-uri") |
| 744 |
=> car)))) |
| 745 |
|
| 746 |
(test* "http-get (redirect)" "/redirect12" |
| 747 |
(receive (code headers body) |
| 748 |
(http-request 'GET #"localhost:~*http-port*" "/redirect11") |
| 749 |
(cond ((assoc-ref (read-from-string body) "request-uri") |
| 750 |
=> car)))) |
| 751 |
|
| 752 |
(test* "http-get (no redirect)" "/redirect12" |
| 753 |
(receive (code headers body) |
| 754 |
(http-request 'GET #"localhost:~*http-port*" "/redirect11" |
| 755 |
:redirect-handler #f) |
| 756 |
(rfc822-header-ref headers "location"))) |
| 757 |
|
| 758 |
(test* "http-get (custom redirect)" "/foofoo" |
| 759 |
(receive (code headers body) |
| 760 |
(http-request 'GET #"localhost:~*http-port*" "/redirect11" |
| 761 |
:redirect-handler (^[meth code hdrs body] |
| 762 |
`(GET . "/foofoo"))) |
| 763 |
(cond ((assoc-ref (read-from-string body) "request-uri") |
| 764 |
=> car)))) |
| 765 |
|
| 766 |
(test* "http-get (custom redirect to HEAD)" #f |
| 767 |
(receive (code headers body) |
| 768 |
(http-request 'GET #"localhost:~*http-port*" "/redirect11" |
| 769 |
:redirect-handler (^[meth code hdrs body] |
| 770 |
`(HEAD . "/foofoo"))) |
| 771 |
body)) |
| 772 |
|
| 773 |
(test* "http-get (no-redirect 301 POST)" '("301" "/redirect12") |
| 774 |
(receive (code headers body) |
| 775 |
(http-request 'POST #"localhost:~*http-port*" "/redirect21" |
| 776 |
:sender (http-null-sender)) |
| 777 |
(list code (rfc822-header-ref headers "location")))) |
| 778 |
|
| 779 |
(test* "http-get (loop)" (test-error <http-error>) |
| 780 |
(http-request 'GET #"localhost:~*http-port*" "/loop1")) |
| 781 |
|
| 782 |
(test* "http-get (chunked body)" "OK" |
| 783 |
(receive (code headers body) |
| 784 |
(http-request 'GET #"localhost:~*http-port*" "/chunked") |
| 785 |
body)) |
| 786 |
|
| 787 |
(test* "http-head" #t |
| 788 |
(receive (code headers body) |
| 789 |
(http-request 'HEAD #"localhost:~*http-port*" "/") |
| 790 |
(and (equal? code "200") |
| 791 |
(equal? headers '(("content-type" "text/plain"))) |
| 792 |
(not body)))) |
| 793 |
|
| 794 |
(let1 expected `(("method" "POST") |
| 795 |
("request-uri" "/post") |
| 796 |
("content-length" "4") |
| 797 |
("host" ,#"localhost:~*http-port*") |
| 798 |
("user-agent" ,#"gauche.http/~(gauche-version)") |
| 799 |
("request-body" "data")) |
| 800 |
(define (tester msg thunk) |
| 801 |
(test* #"http-post ~msg" expected |
| 802 |
(receive (code headers body) (thunk) |
| 803 |
(and (equal? code "200") |
| 804 |
(equal? headers '(("content-type" "text/plain"))) |
| 805 |
(read-from-string body))) |
| 806 |
alist-equal?)) |
| 807 |
|
| 808 |
(tester "(new API)" |
| 809 |
(lambda () |
| 810 |
(http-request 'POST #"localhost:~*http-port*" "/post" |
| 811 |
:sender (http-string-sender "data")))) |
| 812 |
|
| 813 |
(tester "(old API)" |
| 814 |
(lambda () |
| 815 |
(http-post #"localhost:~*http-port*" "/post" "data"))) |
| 816 |
) |
| 817 |
|
| 818 |
(let1 expected '(("a" "b") ("c" "d")) |
| 819 |
(define (tester msg thunk) |
| 820 |
(test* #"http-post (multipart/form-data) ~msg" expected |
| 821 |
(receive (code headers body) (thunk) |
| 822 |
|
| 823 |
(and-let* ([ (equal? code "200") ] |
| 824 |
[ (equal? headers '(("content-type" "text/plain"))) ] |
| 825 |
[r (read-from-string body)] |
| 826 |
[body (assoc "request-body" r)] |
| 827 |
[part (call-with-input-string (cadr body) |
| 828 |
(cut mime-parse-message <> r mime-body->string))] |
| 829 |
[ (is-a? part <mime-part>) ] |
| 830 |
[ (list? (ref part'content)) ]) |
| 831 |
(map (lambda (p) |
| 832 |
(match (mime-parse-content-disposition |
| 833 |
(rfc822-header-ref (ref p'headers) |
| 834 |
"content-disposition")) |
| 835 |
[("form-data" ("name" . name)) |
| 836 |
(list name (ref p'content))] |
| 837 |
[else |
| 838 |
(list (ref p'headers) (ref p'content))])) |
| 839 |
(ref part'content)))))) |
| 840 |
|
| 841 |
(tester "(new API)" |
| 842 |
(lambda () |
| 843 |
(http-request 'POST #"localhost:~*http-port*" "/post" |
| 844 |
:sender |
| 845 |
(http-multipart-sender '(("a" "b") ("c" "d")))))) |
| 846 |
(tester "(old API)" |
| 847 |
(lambda () |
| 848 |
(http-post #"localhost:~*http-port*" "/post" |
| 849 |
'(("a" "b") ("c" "d"))))) |
| 850 |
) |
| 851 |
|
| 852 |
(test* "<http-error>" #t |
| 853 |
(guard (e (else (is-a? e <http-error>))) |
| 854 |
(http-request 'GET #"localhost:~*http-port*" "/exit"))) |
| 855 |
|
| 856 |
(sys-waitpid -1) |
| 857 |
|
| 858 |
|
| 859 |
;;-------------------------------------------------------------------- |
| 860 |
(test-section "rfc.uuid") |
| 861 |
|
| 862 |
(use rfc.uuid) |
| 863 |
(test-module 'rfc.uuid) |
| 864 |
(use srfi.13) |
| 865 |
(use srfi.27) |
| 866 |
|
| 867 |
(let1 u "f81d4fae-7dec-11d0-a765-00a0c91e6bf6" |
| 868 |
(test* "uuid-parse & write 1" u |
| 869 |
(uuid->string (parse-uuid u))) |
| 870 |
(test* "uuid-parse & write 2" u |
| 871 |
(uuid->string (parse-uuid #"urn:uuid:~u"))) |
| 872 |
(test* "uuid-parse & write 3" u |
| 873 |
(uuid->string (parse-uuid #"{~u}"))) |
| 874 |
(test* "uuid-parse & write 4" u |
| 875 |
(uuid->string (parse-uuid (string-delete #\- u)))) |
| 876 |
|
| 877 |
(test* "uuid-parse error case" (test-error) |
| 878 |
(parse-uuid "kaboom")) |
| 879 |
(test* "uuid-parse error case (#f)" #f |
| 880 |
(parse-uuid "kaboom" :if-invalid #f)) |
| 881 |
) |
| 882 |
|
| 883 |
(let* ([u1 (uuid1)] |
| 884 |
[u2 (uuid1)]) |
| 885 |
(test* "uuid-version" 1 (uuid-version u1)) |
| 886 |
(test* "uuid1 timestamp" #t (<? uuid-comparator u1 u2)) |
| 887 |
(test* "uuid1 node id" #t |
| 888 |
(u8vector=? (uvector-alias <u8vector> (uuid-value u1) 10) |
| 889 |
(uvector-alias <u8vector> (uuid-value u2) 10))) |
| 890 |
) |
| 891 |
|
| 892 |
(let* ([u (uuid4)]) |
| 893 |
(test* "uuid-version" 4 (uuid-version u))) |
| 894 |
|
| 895 |
;; test random source parameterization |
| 896 |
(let* ([rs (make-random-source)] |
| 897 |
[uuid00 #f] |
| 898 |
[uuid01 #f] |
| 899 |
[uuid10 #f] |
| 900 |
[uuid11 #f]) |
| 901 |
(random-source-state-set! rs (random-source-state-ref (uuid-random-source))) |
| 902 |
(set! uuid00 (make-uuid4-generator)) |
| 903 |
(parameterize ([uuid-random-source rs]) |
| 904 |
(set! uuid10 (make-uuid4-generator)) |
| 905 |
(set! uuid11 (make-uuid4-generator))) |
| 906 |
(set! uuid01 (make-uuid4-generator)) |
| 907 |
(test* "uuid-random-source" '(#t #t) |
| 908 |
(list (equal? (uuid->string (uuid00)) (uuid->string (uuid10))) |
| 909 |
(equal? (uuid->string (uuid01)) (uuid->string (uuid11)))))) |
| 910 |
|
| 911 |
(test-end) |