]> gitweb.factorcode.org Git - factor.git/commitdiff
mime.multipart: removed call to maybe-fill-bytes, fixes multipart parsing of short...
authorBjörn Lindqvist <bjourne@gmail.com>
Wed, 29 Oct 2014 18:34:08 +0000 (19:34 +0100)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 12 May 2015 16:31:21 +0000 (09:31 -0700)
basis/mime/multipart/multipart-tests.factor
basis/mime/multipart/multipart.factor

index 3ded3da21aa98941d96aa9e689f38efa6312a7f3..229c64859bfae4c79afb0bfee09cfabda2d61487 100644 (file)
@@ -4,37 +4,60 @@ USING: accessors assocs continuations fry http.server io
 io.encodings.ascii io.files io.files.unique
 io.servers io.streams.duplex io.streams.string
 kernel math.ranges mime.multipart multiline namespaces random
-sequences strings threads tools.test ;
+sequences sorting strings threads tools.test ;
 IN: mime.multipart.tests
 
-: upload-separator ( -- seq )
-   "----WebKitFormBoundary6odjpVPXIighAE2L" ;
+CONSTANT: separator1 "----WebKitFormBoundary6odjpVPXIighAE2L"
 
-: upload ( -- seq )
-   "------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" ;
+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"
 
 : mime-test-stream ( -- stream )
-   upload
+   upload1
    "mime" "test" make-unique-file ascii
    [ set-file-contents ] [ <file-reader> ] 2bi ;
 
 [ ] [ mime-test-stream [ ] with-input-stream ] unit-test
 
 [ t ] [
-    mime-test-stream [ upload-separator parse-multipart ] with-input-stream
+    mime-test-stream [ separator1 parse-multipart ] with-input-stream
     "file1" swap key?
 ] unit-test
 
 [ t ] [
-    mime-test-stream [ upload-separator parse-multipart ] with-input-stream
+    mime-test-stream [ separator1 parse-multipart ] with-input-stream
     "file1" swap key?
 ] unit-test
 
 [ t ] [
-    mime-test-stream [ upload-separator parse-multipart ] with-input-stream
+    mime-test-stream [ separator1 parse-multipart ] with-input-stream
     "file1" of filename>> "up.txt" =
 ] unit-test
 
+CONSTANT: separator2 "768de80194d942619886d23f1337aa15"
+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"
+
+[
+    "upload.txt"
+    H{
+        { "content-disposition"
+          "form-data; name=\"text\"; filename=\"upload.txt\"" }
+        { "content-type" "text/plain" }
+    }
+] [
+    upload2 [ separator2 parse-multipart ] with-string-reader
+    "text" of [ filename>> ] [ headers>> ] bi
+] unit-test
+
+CONSTANT: separator3 "3f116598c7f0431b9f98148ed235c822"
+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"
+
+[
+    { "text" "text2" }
+] [
+    upload3 [ separator3 parse-multipart ] with-string-reader
+    keys natural-sort
+] unit-test
+
 SYMBOL: mime-test-server
 
 : with-test-server ( quot -- )
index 15f008ef1aa459b08d74aa0d88b98f85590475e6..d2797f1a6f8356bc68d44ed66a42fb83f56fbaa0 100644 (file)
@@ -44,9 +44,6 @@ ERROR: end-of-stream multipart ;
     [ '[ _ B{ } append-as ] change-bytes ]
     [ t >>end-of-stream? ] if* ;
 
-: maybe-fill-bytes ( multipart -- multipart )
-    dup bytes>> length 256 < [ fill-bytes ] when ;
-
 : split-bytes ( bytes separator -- leftover-bytes safe-to-dump )
     dupd [ length ] bi@ 1 - - short cut-slice swap ;
 
@@ -68,7 +65,6 @@ ERROR: end-of-stream multipart ;
     [ dump-until-separator ] with-string-writer ;
 
 : read-header ( multipart -- multipart )
-    maybe-fill-bytes
     dup bytes>> "--\r\n" sequence= [
         t >>end-of-stream?
     ] [