]> gitweb.factorcode.org Git - factor.git/blob - basis/mime/multipart/multipart-tests.factor
factor: add newlines to .factor files
[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.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
9
10 CONSTANT: separator1 "----WebKitFormBoundary6odjpVPXIighAE2L"
11
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"
13
14 : mime-test-stream ( -- stream )
15    upload1
16    [ "mime" "test" unique-file ] with-temp-directory
17    ascii [ set-file-contents ] [ <file-reader> ] 2bi ;
18
19 { } [ mime-test-stream [ ] with-input-stream ] unit-test
20
21 { t } [
22     mime-test-stream [ separator1 parse-multipart ] with-input-stream
23     "file1" swap key?
24 ] unit-test
25
26 { t } [
27     mime-test-stream [ separator1 parse-multipart ] with-input-stream
28     "file1" swap key?
29 ] unit-test
30
31 { t } [
32     mime-test-stream [ separator1 parse-multipart ] with-input-stream
33     "file1" of filename>> "up.txt" =
34 ] unit-test
35
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"
38
39 {
40     "upload.txt"
41     H{
42         { "content-disposition"
43           "form-data; name=\"text\"; filename=\"upload.txt\"" }
44         { "content-type" "text/plain" }
45     }
46 } [
47     upload2 [ separator2 parse-multipart ] with-string-reader
48     "text" of [ filename>> ] [ headers>> ] bi
49 ] unit-test
50
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"
53
54 {
55     { "text" "text2" }
56 } [
57     upload3 [ separator3 parse-multipart ] with-string-reader
58     keys natural-sort
59 ] unit-test
60
61 SYMBOL: mime-test-server
62
63 : with-test-server ( quot -- )
64     [
65         <http-server>
66             f >>secure
67             0 >>insecure
68     ] dip with-threaded-server ; inline
69
70 : test-server-port ( -- n )
71     mime-test-server get insecure>> ;
72
73 : a-stream ( n -- stream )
74     CHAR: a <string> <string-reader> ;
75
76 { } [
77     [
78     ] with-test-server
79 ] unit-test
80
81 [
82     "--\r\n\r\n" <string-reader> [
83         "\r\n\r\n" <multipart>
84         "\r\n\r\n" parse-multipart
85     ] with-input-stream
86 ] [ mime-decoding-ran-out-of-bytes? ] must-fail-with
87