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.temp io.files.unique
5 io.servers io.streams.duplex io.streams.string
6 kernel ranges mime.multipart multiline namespaces random
7 sequences sorting strings threads tools.test ;
8 IN: mime.multipart.tests
10 CONSTANT: separator1 "----WebKitFormBoundary6odjpVPXIighAE2L"
12 CONSTANT: upload1 "------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"
14 : mime-test-stream ( -- stream )
16 [ "mime" "test" unique-file ] with-temp-directory
17 ascii [ set-file-contents ] [ <file-reader> ] 2bi ;
19 { } [ mime-test-stream [ ] with-input-stream ] unit-test
22 mime-test-stream [ separator1 parse-multipart ] with-input-stream
27 mime-test-stream [ separator1 parse-multipart ] with-input-stream
32 mime-test-stream [ separator1 parse-multipart ] with-input-stream
33 "file1" of filename>> "up.txt" =
36 CONSTANT: separator2 "768de80194d942619886d23f1337aa15"
37 CONSTANT: upload2 "--768de80194d942619886d23f1337aa15\r\nContent-Disposition: form-data; name=\"text\"; filename=\"upload.txt\"\r\nContent-Type: text/plain\r\n\r\nhello\r\n--768de80194d942619886d23f1337aa15--\r\n"
42 { "content-disposition"
43 "form-data; name=\"text\"; filename=\"upload.txt\"" }
44 { "content-type" "text/plain" }
47 upload2 [ separator2 parse-multipart ] with-string-reader
48 "text" of [ filename>> ] [ headers>> ] bi
51 CONSTANT: separator3 "3f116598c7f0431b9f98148ed235c822"
52 CONSTANT: upload3 "--3f116598c7f0431b9f98148ed235c822\r\nContent-Disposition: form-data; name=\"text\"; filename=\"upload.txt\"\r\n\r\nhello\r\n--3f116598c7f0431b9f98148ed235c822\r\nContent-Disposition: form-data; name=\"text2\"; filename=\"upload.txt\"\r\n\r\nhello\r\n--3f116598c7f0431b9f98148ed235c822--\r\n"
57 upload3 [ separator3 parse-multipart ] with-string-reader
61 SYMBOL: mime-test-server
63 : with-test-server ( quot -- )
68 ] dip with-threaded-server ; inline
70 : test-server-port ( -- n )
71 mime-test-server get insecure>> ;
73 : a-stream ( n -- stream )
74 CHAR: a <string> <string-reader> ;
82 "--\r\n\r\n" <string-reader> [
83 "\r\n\r\n" <multipart>
84 "\r\n\r\n" parse-multipart
86 ] [ mime-decoding-ran-out-of-bytes? ] must-fail-with