Debian Codegraph @

test/www.scm

raw
1 ;;
2 ;; test www.* modules
3 ;;
4
5 (use gauche.test)
6 (use gauche.charconv)
7 (use text.tree)
8 (use rfc.822)
9 (use file.util)
10 (test-start "www.* modules")
11
12 ;;------------------------------------------------
13 (test-section "www.cgi")
14 (use www.cgi)
15 (test-module 'www.cgi)
16
17 (define params #f)
18 (define qs1 "a=foo+bar&boo=baz=doo&z%3Dz=%21%26&a=+%20&#=#&z=z=8&r&r=2")
19 (define qr1 '(("a" "foo bar" " ") ("boo" "baz=doo") ("z=z" "!&") ("#" "#") ("z" "z=8") ("r" #t "2")))
20 (define qs1b "a=foo+bar;boo=baz=doo;z%3Dz=%21%26;a=+%20;#=#;z=z=8;r;r=2")
21
22 (define qs2 "zz=aa&aa=zz")
23 (define qr2 '(("zz" "aa") ("aa" "zz")))
24
25 (test* "cgi-parse-parameters" qr1
26 (cgi-parse-parameters :query-string qs1))
27 (test* "cgi-parse-parameters" qr1
28 (cgi-parse-parameters :query-string qs1b))
29
30 (define ps1 "--boundary
31 Content-Disposition: form-data; name=\"aaa\"
32
33 111
34 --boundary
35 Content-Disposition: form-data; name=\"bbb\"; filename=\"x.txt\"
36 Content-Type: text/plain
37
38 abc
39 def
40 ghi
41
42 --boundary
43 Content-Disposition: form-data; name=\"ccc\"; filename=\"\"
44
45 --boundary
46 Content-Disposition: form-data: name=\"ddd\"; filename=\"ttt\\bbb\"
47 Content-Type: application/octet-stream
48 Content-Transfer-Encoding: base64
49
50 VGhpcyBpcyBhIHRlc3Qgc2VudGVuY2Uu
51 --boundary--
52 ")
53
54 (define ps2 "--boundary
55 Content-Disposition: form-data; name=aaa
56
57 111
58 --boundary
59 Content-Disposition: form-data; name=bbb; filename=x.txt
60 Content-Type: text/plain
61
62 abc
63 def
64 ghi
65
66 --boundary
67 Content-Disposition: form-data; name=ccc ; filename=\"\"
68
69 --boundary
70 Content-Disposition: form-data;name=ddd; filename=\"ttt\\bbb\"
71 Content-Type: application/octet-stream
72 Content-Transfer-Encoding: base64
73
74 VGhpcyBpcyBhIHRlc3Qgc2VudGVuY2Uu
75 --boundary--
76 ")
77
78 (define pr1 '(("aaa" "111") ("bbb" "abc\ndef\nghi\n") ("ccc" #f) ("ddd" "This is a test sentence.")))
79
80 (define pr2 '(("aaa" "111") ("bbb" "x.txt") ("ccc" #f) ("ddd" "ttt\\bbb")))
81
82 (define (multipart-parse-test src)
83 (test* "cgi-parse-parameters (multipart)" pr1
84 (parameterize ((cgi-metavariables
85 `(("REQUEST_METHOD" "POST")
86 ("CONTENT_TYPE" "multipart/form-data; boundary=boundary")
87 ("CONTENT_LENGTH" ,(string-size src)))))
88 (with-input-from-string src
89 (lambda () (cgi-parse-parameters)))))
90
91 (test* "cgi-parse-parameters (multipart, custom handler)" pr2
92 (parameterize ((cgi-metavariables
93 `(("REQUEST_METHOD" "POST")
94 ("CONTENT_TYPE" "multipart/form-data; boundary=boundary")
95 ("CONTENT_LENGTH" ,(string-size src)))))
96 (with-input-from-string src
97 (lambda ()
98 (cgi-parse-parameters
99 :part-handlers
100 `((#t ,(lambda (name filename info inp)
101 (let loop ((line (read-line inp)))
102 (if (eof-object? line)
103 filename
104 (loop (read-line inp))))))))))
105 ))
106
107 (test* "cgi-parse-parameters (multipart, custom handler 2)" "abc\ndef\nghi\n"
108 (parameterize ((cgi-metavariables
109 `(("REQUEST_METHOD" "POST")
110 ("CONTENT_TYPE" "multipart/form-data; boundary=boundary")
111 ("CONTENT_LENGTH" ,(string-size src)))))
112 (let1 r
113 (with-input-from-string src
114 (lambda ()
115 (cgi-parse-parameters
116 :part-handlers `(("bbb" file :prefix "./bbb")))))
117 (let* ((tmpfile (cgi-get-parameter "bbb" r))
118 (content (file->string tmpfile)))
119 (sys-unlink tmpfile)
120 content))))
121
122 (test* "cgi-parse-parameters (multipart, custom handler 3)" "abc\ndef\nghi\n"
123 (parameterize ((cgi-metavariables
124 `(("REQUEST_METHOD" "POST")
125 ("CONTENT_TYPE" "multipart/form-data; boundary=boundary")
126 ("CONTENT_LENGTH" ,(string-size src)))))
127 (let1 r
128 (with-input-from-string src
129 (lambda ()
130 (cgi-parse-parameters
131 :part-handlers `((#/b{3}/ file :prefix "./bbb")))))
132 (let* ((tmpfile (cgi-get-parameter "bbb" r))
133 (content (file->string tmpfile)))
134 (sys-unlink tmpfile)
135 content))))
136 )
137
138 (multipart-parse-test ps1)
139 (multipart-parse-test ps2)
140
141 (define ps3 "--boundary
142 Content-Disposition: form-data; name=aaa
143
144 111
145 --boundary
146 Content-Disposition: form-data; name=bbb
147
148 000
149 --boundary
150 Content-Disposition: form-data; name=aaa
151
152 222
153 --boundary
154 Content-Disposition: form-data; name=\"aaa\"
155
156 333
157 --boundary
158 Content-Disposition: form-data; name=bbb
159
160 999
161 --boundary--")
162
163 (define pr3 '(("aaa" "111" "222" "333") ("bbb" "000" "999")))
164
165 (test* "cgi-parse-parameter (multipart, multivalue)" pr3
166 (parameterize ((cgi-metavariables
167 `(("REQUEST_METHOD" "POST")
168 ("CONTENT_TYPE" "multipart/form-data; boundary=boundary")
169 ("CONTENT_LENGTH" ,(string-size ps3)))))
170 (with-input-from-string ps3 cgi-parse-parameters)))
171
172 (test* "cgi-get-parameter" "foo bar"
173 (cgi-get-parameter "a" qr1))
174 (test* "cgi-get-parameter" '("foo bar" " ")
175 (cgi-get-parameter "a" qr1 :list #t))
176 (test* "cgi-get-parameter" #t
177 (cgi-get-parameter "r" qr1))
178 (test* "cgi-get-parameter" '(#t "2")
179 (cgi-get-parameter "r" qr1 :list #t))
180 (test* "cgi-get-parameter" '("baz=doo")
181 (cgi-get-parameter "boo" qr1 :list #t))
182 (test* "cgi-get-parameter" 'none
183 (cgi-get-parameter "booz" qr1 :default 'none))
184 (test* "cgi-get-parameter" #f
185 (cgi-get-parameter "booz" qr1))
186 (test* "cgi-get-parameter" '()
187 (cgi-get-parameter "booz" qr1 :list #t))
188 (test* "cgi-get-parameter" '(0 2)
189 (cgi-get-parameter "r" qr1 :convert x->integer :list #t))
190
191 (test* "cgi-get-query (GET)" qr1
192 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "GET")
193 ("QUERY_STRING" ,qs1))))
194 (with-input-from-string qs2
195 cgi-parse-parameters)))
196 (test* "cgi-get-query (HEAD)" qr1
197 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "HEAD")
198 ("QUERY_STRING" ,qs1))))
199 (with-input-from-string qs2
200 cgi-parse-parameters)))
201 (test* "cgi-get-query (POST)" qr2
202 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "POST")
203 ("QUERY_STRING" ,qs1))))
204 (with-input-from-string qs2
205 cgi-parse-parameters)))
206 (test* "cgi-get-query (POST)" qr2
207 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "POST")
208 ("CONTENT_LENGTH" ,(string-length qs2)))))
209 (with-input-from-string qs2
210 cgi-parse-parameters)))
211 (test* "cgi-get-query (POST)" '(("zz" "aa"))
212 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "POST")
213 ("CONTENT_LENGTH" 5))))
214 (with-input-from-string qs2
215 cgi-parse-parameters)))
216
217 (test* "cgi-header" "Content-type: text/html\r\n\r\n"
218 (tree->string (cgi-header)))
219
220 (test* "cgi-header" "Location: http://foo.bar/\r\n\r\n"
221 (tree->string (cgi-header :location "http://foo.bar/")))
222
223 (test* "cgi-header" "Content-type: hoge\r\nLocation: http://foo.bar/\r\n\r\n"
224 (tree->string
225 (cgi-header :location "http://foo.bar/" :content-type "hoge")))
226
227 (test* "cgi-header" "Content-type: text/plain; charset=utf-8\r\n\r\n"
228 (tree->string
229 (cgi-header :content-type "text/plain; charset=utf-8")))
230
231 (test* "cgi-header"
232 "Content-type: text/html\r\nSet-cookie: hoge\r\nSet-cookie: poge\r\n\r\n"
233 (tree->string
234 (cgi-header :cookies '("hoge" "poge"))))
235
236 (test* "cgi-header"
237 "Content-type: text/html\r\nSet-cookie: hoge\r\nSet-cookie: poge\r\nx-foo: foo\r\n\r\n"
238 (tree->string
239 (cgi-header :x-foo "foo" :cookies '("hoge" "poge"))))
240
241 (test* "cgi-main" "Content-type: text/plain\r\n\r\na=foo bar"
242 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "GET")
243 ("QUERY_STRING" ,qs1))))
244 (with-output-to-string
245 (lambda ()
246 (cgi-main
247 (lambda (params)
248 `(,(cgi-header :content-type "text/plain")
249 "a="
250 ,(cgi-get-parameter "a" params))))))))
251
252 (unless (eq? (gauche-character-encoding) 'none)
253 (test* "cgi-output-character-encoding" #*"\xe3\x81\x82"
254 (string-complete->incomplete
255 (parameterize ((cgi-metavariables `(("REQUEST_METHOD" "GET")
256 ("QUERY_STRING" "")))
257 (cgi-output-character-encoding 'utf8))
258 (with-output-to-string
259 (lambda ()
260 (cgi-main
261 (lambda (params)
262 (string #\u3042)))))))))
263
264 ;;------------------------------------------------
265 (test-section "www.cgi.test")
266 (use www.cgi.test)
267 (test-module 'www.cgi.test)
268
269 (test* "cgi-test-environment-ref" "remote"
270 (cgi-test-environment-ref "REMOTE_HOST"))
271 (test* "cgi-test-environment-ref" "zzz"
272 (cgi-test-environment-ref 'ZZZ "zzz"))
273 (test* "cgi-test-environment-set!" "foo.com"
274 (begin
275 (set! (cgi-test-environment-ref 'REMOTE_HOST) "foo.com")
276 (cgi-test-environment-ref "REMOTE_HOST")))
277
278 (cond-expand
279 [gauche.os.windows
280 ;; windows can't support #! magic anyways.
281 ]
282 [else
283 (sys-system "rm -rf test.o")
284 (sys-mkdir "test.o" #o755)
285
286 (with-output-to-file "test.o/cgitest.cgi"
287 (lambda ()
288 (print "#!/bin/sh")
289 (print "echo Content-type: text/plain")
290 (print "echo")
291 (print "echo \"SERVER_NAME = $SERVER_NAME\"")
292 (print "echo \"REMOTE_HOST = $REMOTE_HOST\"")
293 (print "echo \"REQUEST_METHOD = $REQUEST_METHOD\"")
294 (print "echo \"CONTENT_TYPE = $CONTENT_TYPE\"")
295 (print "echo \"QUERY_STRING = $QUERY_STRING\"")))
296
297 (sys-chmod "test.o/cgitest.cgi" #o755)
298
299 (test* "call-with-cgi-script" '(("content-type" "text/plain"))
300 (call-with-cgi-script "test.o/cgitest.cgi"
301 (lambda (p)
302 (rfc822-header->list p)))
303 )
304
305 (test* "run-cgi-script->string-list"
306 '((("content-type" "text/plain"))
307 ("SERVER_NAME = localhost"
308 "REMOTE_HOST = foo.com"
309 "REQUEST_METHOD = GET"
310 "CONTENT_TYPE = "
311 "QUERY_STRING = "))
312 (receive r (run-cgi-script->string-list "test.o/cgitest.cgi")
313 r)
314 )
315
316 (test* "run-cgi-script->string-list (using parameters/GET)"
317 '("SERVER_NAME = localhost"
318 "REMOTE_HOST = foo.com"
319 "REQUEST_METHOD = GET"
320 "CONTENT_TYPE = "
321 "QUERY_STRING = a=b&%26%26%24%26=%21%40%21%40")
322 (receive (_ body)
323 (run-cgi-script->string-list "test.o/cgitest.cgi"
324 :parameters '((a . b) (&&$& . !@!@)))
325 body))
326
327 (test* "run-cgi-script->string-list (using parameters/HEAD)"
328 '("SERVER_NAME = localhost"
329 "REMOTE_HOST = foo.com"
330 "REQUEST_METHOD = HEAD"
331 "CONTENT_TYPE = "
332 "QUERY_STRING = a=b&%26%26%24%26=%21%40%21%40")
333 (receive (_ body)
334 (run-cgi-script->string-list "test.o/cgitest.cgi"
335 :environment '((REQUEST_METHOD . HEAD))
336 :parameters '((a . b) (&&$& . !@!@)))
337 body))
338
339 (with-output-to-file "test.o/cgitest.cgi"
340 (lambda ()
341 (print "#!/bin/sh")
342 (print "echo Content-type: text/plain")
343 (print "echo")
344 (print "echo \"REQUEST_METHOD = $REQUEST_METHOD\"")
345 (print "echo \"CONTENT_TYPE = $CONTENT_TYPE\"")
346 (print "echo \"CONTENT_LENGTH = $CONTENT_LENGTH\"")
347 (print "echo \"QUERY_STRING = $QUERY_STRING\"")
348 (print "cat")))
349
350 (test* "run-cgi-script->string-list (using parameters)"
351 '("REQUEST_METHOD = POST"
352 "CONTENT_TYPE = application/x-www-form-urlencoded"
353 "CONTENT_LENGTH = 29"
354 "QUERY_STRING = "
355 "a=b&%26%26%24%26=%21%40%21%40")
356 (receive (_ body)
357 (run-cgi-script->string-list "test.o/cgitest.cgi"
358 :environment '((REQUEST_METHOD . POST))
359 :parameters '((a . b) (&&$& . !@!@)))
360 body))
361
362 (sys-system "rm -rf test.o")])
363
364 ;;------------------------------------------------
365 (test-section "www.css")
366 (use www.css)
367 (test-module 'www.css)
368
369 ;; NB: this assumes the test is run either under src/ or test/
370 (define (run-css-test)
371 (dolist [infile (glob "../test/data/css-*.css")]
372 (define sxcss
373 (call-with-input-file (path-swap-extension infile "sxcss") read))
374 (test* #"css parser ~infile"
375 sxcss
376 (parse-css-file infile)
377 equal?)
378 (test* #"css constructor ~infile"
379 sxcss
380 (with-input-from-string
381 (with-output-to-string
382 (cut construct-css sxcss))
383 parse-css)
384 equal?)))
385
386 (run-css-test)
387
388 (test* "parse-css-selector-string (failure)"
389 #f
390 (parse-css-selector-string ":::"))
391
392 (test* "parse-css-selector-string (nth-child)"
393 '(* (: (nth-child 1)))
394 (parse-css-selector-string ":nth-child(1)"))
395
396 (test* "parse-css-selector-string (nth-child an+b)"
397 '(* (: (nth-child (:an+b 2 1))))
398 (parse-css-selector-string ":nth-child(2n+1)"))
399
400 (test* "parse-css-selector-string (nth-child an+b)"
401 '(* (: (nth-child (:an+b 1 0))))
402 (parse-css-selector-string ":nth-child(n)"))
403
404 (test* "parse-css-selector-string (nth-child an+b)"
405 '(* (: (nth-child (:an+b -1 0))))
406 (parse-css-selector-string ":nth-child(-n)"))
407
408 (test* "parse-css-selector-string (nth-child an+b)"
409 '(* (: (nth-child (:an+b -1 -6))))
410 (parse-css-selector-string ":nth-child(-n-6)"))
411
412 (test* "parse-css-selector-string (nth-child an+b)"
413 '(* (: (nth-child (:an+b -3 4))))
414 (parse-css-selector-string ":nth-child(-3n+4)"))
415
416 (test* "parse-css-selector-string (nth-child an+b)"
417 '(* (: (nth-child (:an+b -3 -4))))
418 (parse-css-selector-string ":nth-child(-3n-4)"))
419
420 (test* "parse-css-selector-string (nth-child an+b)"
421 #f
422 (parse-css-selector-string ":nth-child(- n)"))
423
424 (test* "parse-css-selector-string (nth-child an+b)"
425 '(* (:not (: (nth-child (:an+b -1 0)))))
426 (parse-css-selector-string ":not(:nth-child(-n))"))
427
428 (test* "parse-css-selector-string (nth-child an+b)"
429 #f
430 (parse-css-selector-string ":not(:nth-child(- n))"))
431
432 (test* "parse-css-selector-string (nth-child an+b)"
433 '(* (: (nth-child (:an+b 2 -1))))
434 (parse-css-selector-string ":nth-child(2n- 1)"))
435
436 (test-end)