]> gitweb.factorcode.org Git - factor.git/blob - basis/mime/multipart/multipart-tests.factor
Squashed commit of the following:
[factor.git] / basis / mime / multipart / multipart-tests.factor
1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs continuations fry http.server io
4 io.encodings.ascii io.files io.files.unique
5 io.servers.connection io.streams.duplex io.streams.string
6 kernel math.ranges mime.multipart multiline namespaces random
7 sequences strings threads tools.test ;
8 IN: mime.multipart.tests
9
10 : upload-separator ( -- seq )
11    "----WebKitFormBoundary6odjpVPXIighAE2L" ;
12
13 : upload ( -- seq )
14    "------WebKitFormBoundary6odjpVPXIighAE2L\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"up.txt\"\r\nContent-Type: text/plain\r\n\r\nuploaded!\n\r\n------WebKitFormBoundary6odjpVPXIighAE2L\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"\"\r\n\r\n\r\n------WebKitFormBoundary6odjpVPXIighAE2L\r\nContent-Disposition: form-data; name=\"file3\"; filename=\"\"\r\n\r\n\r\n------WebKitFormBoundary6odjpVPXIighAE2L\r\nContent-Disposition: form-data; name=\"text1\"\r\n\r\nlol\r\n------WebKitFormBoundary6odjpVPXIighAE2L--\r\n" ;
15
16 : mime-test-stream ( -- stream )
17    upload
18    "mime" "test" make-unique-file ascii
19    [ set-file-contents ] [ <file-reader> ] 2bi ;
20
21 [ ] [ mime-test-stream [ ] with-input-stream ] unit-test
22
23 [ t ] [
24     mime-test-stream [ upload-separator parse-multipart ] with-input-stream
25     "file1" swap key?
26 ] unit-test
27
28 [ t ] [
29     mime-test-stream [ upload-separator parse-multipart ] with-input-stream
30     "file1" swap key?
31 ] unit-test
32
33 [ t ] [
34     mime-test-stream [ upload-separator parse-multipart ] with-input-stream
35     "file1" swap at filename>> "up.txt" =
36 ] unit-test
37
38 SYMBOL: mime-test-server
39
40 : with-test-server ( quot -- )
41     [
42         <http-server>
43             f >>secure
44             0 >>insecure
45     ] dip with-threaded-server ; inline
46
47 : test-server-port ( -- n )
48     mime-test-server get insecure>> ;
49
50 : a-stream ( n -- stream )
51     CHAR: a <string> <string-reader> ;
52
53 [ ] [
54     [
55     ] with-test-server
56 ] unit-test