spec_multipart.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. require 'rack/utils'
  2. require 'rack/mock'
  3. describe Rack::Multipart do
  4. def multipart_fixture(name, boundary = "AaB03x")
  5. file = multipart_file(name)
  6. data = File.open(file, 'rb') { |io| io.read }
  7. type = "multipart/form-data; boundary=#{boundary}"
  8. length = data.respond_to?(:bytesize) ? data.bytesize : data.size
  9. { "CONTENT_TYPE" => type,
  10. "CONTENT_LENGTH" => length.to_s,
  11. :input => StringIO.new(data) }
  12. end
  13. def multipart_file(name)
  14. File.join(File.dirname(__FILE__), "multipart", name.to_s)
  15. end
  16. should "return nil if content type is not multipart" do
  17. env = Rack::MockRequest.env_for("/",
  18. "CONTENT_TYPE" => 'application/x-www-form-urlencoded')
  19. Rack::Multipart.parse_multipart(env).should.equal nil
  20. end
  21. should "parse multipart content when content type present but filename is not" do
  22. env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
  23. params = Rack::Multipart.parse_multipart(env)
  24. params["text"].should.equal "contents"
  25. end
  26. should "raise RangeError if the key space is exhausted" do
  27. env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
  28. old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
  29. begin
  30. lambda { Rack::Multipart.parse_multipart(env) }.should.raise(RangeError)
  31. ensure
  32. Rack::Utils.key_space_limit = old
  33. end
  34. end
  35. should "parse multipart form webkit style" do
  36. env = Rack::MockRequest.env_for '/', multipart_fixture(:webkit)
  37. env['CONTENT_TYPE'] = "multipart/form-data; boundary=----WebKitFormBoundaryWLHCs9qmcJJoyjKR"
  38. params = Rack::Multipart.parse_multipart(env)
  39. params['profile']['bio'].should.include 'hello'
  40. end
  41. should "parse multipart upload with text file" do
  42. env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
  43. params = Rack::Multipart.parse_multipart(env)
  44. params["submit-name"].should.equal "Larry"
  45. params["submit-name-with-content"].should.equal "Berry"
  46. params["files"][:type].should.equal "text/plain"
  47. params["files"][:filename].should.equal "file1.txt"
  48. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  49. "name=\"files\"; filename=\"file1.txt\"\r\n" +
  50. "Content-Type: text/plain\r\n"
  51. params["files"][:name].should.equal "files"
  52. params["files"][:tempfile].read.should.equal "contents"
  53. end
  54. should "parse multipart upload with nested parameters" do
  55. env = Rack::MockRequest.env_for("/", multipart_fixture(:nested))
  56. params = Rack::Multipart.parse_multipart(env)
  57. params["foo"]["submit-name"].should.equal "Larry"
  58. params["foo"]["files"][:type].should.equal "text/plain"
  59. params["foo"]["files"][:filename].should.equal "file1.txt"
  60. params["foo"]["files"][:head].should.equal "Content-Disposition: form-data; " +
  61. "name=\"foo[files]\"; filename=\"file1.txt\"\r\n" +
  62. "Content-Type: text/plain\r\n"
  63. params["foo"]["files"][:name].should.equal "foo[files]"
  64. params["foo"]["files"][:tempfile].read.should.equal "contents"
  65. end
  66. should "parse multipart upload with binary file" do
  67. env = Rack::MockRequest.env_for("/", multipart_fixture(:binary))
  68. params = Rack::Multipart.parse_multipart(env)
  69. params["submit-name"].should.equal "Larry"
  70. params["files"][:type].should.equal "image/png"
  71. params["files"][:filename].should.equal "rack-logo.png"
  72. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  73. "name=\"files\"; filename=\"rack-logo.png\"\r\n" +
  74. "Content-Type: image/png\r\n"
  75. params["files"][:name].should.equal "files"
  76. params["files"][:tempfile].read.length.should.equal 26473
  77. end
  78. should "parse multipart upload with empty file" do
  79. env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
  80. params = Rack::Multipart.parse_multipart(env)
  81. params["submit-name"].should.equal "Larry"
  82. params["files"][:type].should.equal "text/plain"
  83. params["files"][:filename].should.equal "file1.txt"
  84. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  85. "name=\"files\"; filename=\"file1.txt\"\r\n" +
  86. "Content-Type: text/plain\r\n"
  87. params["files"][:name].should.equal "files"
  88. params["files"][:tempfile].read.should.equal ""
  89. end
  90. should "parse multipart upload with filename with semicolons" do
  91. env = Rack::MockRequest.env_for("/", multipart_fixture(:semicolon))
  92. params = Rack::Multipart.parse_multipart(env)
  93. params["files"][:type].should.equal "text/plain"
  94. params["files"][:filename].should.equal "fi;le1.txt"
  95. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  96. "name=\"files\"; filename=\"fi;le1.txt\"\r\n" +
  97. "Content-Type: text/plain\r\n"
  98. params["files"][:name].should.equal "files"
  99. params["files"][:tempfile].read.should.equal "contents"
  100. end
  101. should "not include file params if no file was selected" do
  102. env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
  103. params = Rack::Multipart.parse_multipart(env)
  104. params["submit-name"].should.equal "Larry"
  105. params["files"].should.equal nil
  106. params.keys.should.not.include "files"
  107. end
  108. should "parse multipart/mixed" do
  109. env = Rack::MockRequest.env_for("/", multipart_fixture(:mixed_files))
  110. params = Rack::Utils::Multipart.parse_multipart(env)
  111. params["foo"].should.equal "bar"
  112. params["files"].should.be.instance_of String
  113. params["files"].size.should.equal 252
  114. end
  115. should "parse IE multipart upload and clean up filename" do
  116. env = Rack::MockRequest.env_for("/", multipart_fixture(:ie))
  117. params = Rack::Multipart.parse_multipart(env)
  118. params["files"][:type].should.equal "text/plain"
  119. params["files"][:filename].should.equal "file1.txt"
  120. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  121. "name=\"files\"; " +
  122. 'filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"' +
  123. "\r\nContent-Type: text/plain\r\n"
  124. params["files"][:name].should.equal "files"
  125. params["files"][:tempfile].read.should.equal "contents"
  126. end
  127. should "parse filename and modification param" do
  128. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_modification_param))
  129. params = Rack::Multipart.parse_multipart(env)
  130. params["files"][:type].should.equal "image/jpeg"
  131. params["files"][:filename].should.equal "genome.jpeg"
  132. params["files"][:head].should.equal "Content-Type: image/jpeg\r\n" +
  133. "Content-Disposition: attachment; " +
  134. "name=\"files\"; " +
  135. "filename=genome.jpeg; " +
  136. "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
  137. "Content-Description: a complete map of the human genome\r\n"
  138. params["files"][:name].should.equal "files"
  139. params["files"][:tempfile].read.should.equal "contents"
  140. end
  141. should "parse filename with escaped quotes" do
  142. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes))
  143. params = Rack::Multipart.parse_multipart(env)
  144. params["files"][:type].should.equal "application/octet-stream"
  145. params["files"][:filename].should.equal "escape \"quotes"
  146. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  147. "name=\"files\"; " +
  148. "filename=\"escape \\\"quotes\"\r\n" +
  149. "Content-Type: application/octet-stream\r\n"
  150. params["files"][:name].should.equal "files"
  151. params["files"][:tempfile].read.should.equal "contents"
  152. end
  153. should "parse filename with percent escaped quotes" do
  154. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_percent_escaped_quotes))
  155. params = Rack::Multipart.parse_multipart(env)
  156. params["files"][:type].should.equal "application/octet-stream"
  157. params["files"][:filename].should.equal "escape \"quotes"
  158. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  159. "name=\"files\"; " +
  160. "filename=\"escape %22quotes\"\r\n" +
  161. "Content-Type: application/octet-stream\r\n"
  162. params["files"][:name].should.equal "files"
  163. params["files"][:tempfile].read.should.equal "contents"
  164. end
  165. should "parse filename with unescaped quotes" do
  166. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_quotes))
  167. params = Rack::Multipart.parse_multipart(env)
  168. params["files"][:type].should.equal "application/octet-stream"
  169. params["files"][:filename].should.equal "escape \"quotes"
  170. params["files"][:head].should.equal "Content-Disposition: form-data; " +
  171. "name=\"files\"; " +
  172. "filename=\"escape \"quotes\"\r\n" +
  173. "Content-Type: application/octet-stream\r\n"
  174. params["files"][:name].should.equal "files"
  175. params["files"][:tempfile].read.should.equal "contents"
  176. end
  177. should "parse filename with escaped quotes and modification param" do
  178. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes_and_modification_param))
  179. params = Rack::Multipart.parse_multipart(env)
  180. params["files"][:type].should.equal "image/jpeg"
  181. params["files"][:filename].should.equal "\"human\" genome.jpeg"
  182. params["files"][:head].should.equal "Content-Type: image/jpeg\r\n" +
  183. "Content-Disposition: attachment; " +
  184. "name=\"files\"; " +
  185. "filename=\"\"human\" genome.jpeg\"; " +
  186. "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
  187. "Content-Description: a complete map of the human genome\r\n"
  188. params["files"][:name].should.equal "files"
  189. params["files"][:tempfile].read.should.equal "contents"
  190. end
  191. should "parse filename with unescaped percentage characters" do
  192. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
  193. params = Rack::Multipart.parse_multipart(env)
  194. files = params["document"]["attachment"]
  195. files[:type].should.equal "image/jpeg"
  196. files[:filename].should.equal "100% of a photo.jpeg"
  197. files[:head].should.equal <<-MULTIPART
  198. Content-Disposition: form-data; name="document[attachment]"; filename="100% of a photo.jpeg"\r
  199. Content-Type: image/jpeg\r
  200. MULTIPART
  201. files[:name].should.equal "document[attachment]"
  202. files[:tempfile].read.should.equal "contents"
  203. end
  204. should "parse filename with unescaped percentage characters that look like partial hex escapes" do
  205. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages2, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
  206. params = Rack::Multipart.parse_multipart(env)
  207. files = params["document"]["attachment"]
  208. files[:type].should.equal "image/jpeg"
  209. files[:filename].should.equal "100%a"
  210. files[:head].should.equal <<-MULTIPART
  211. Content-Disposition: form-data; name="document[attachment]"; filename="100%a"\r
  212. Content-Type: image/jpeg\r
  213. MULTIPART
  214. files[:name].should.equal "document[attachment]"
  215. files[:tempfile].read.should.equal "contents"
  216. end
  217. should "parse filename with unescaped percentage characters that look like partial hex escapes" do
  218. env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages3, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
  219. params = Rack::Multipart.parse_multipart(env)
  220. files = params["document"]["attachment"]
  221. files[:type].should.equal "image/jpeg"
  222. files[:filename].should.equal "100%"
  223. files[:head].should.equal <<-MULTIPART
  224. Content-Disposition: form-data; name="document[attachment]"; filename="100%"\r
  225. Content-Type: image/jpeg\r
  226. MULTIPART
  227. files[:name].should.equal "document[attachment]"
  228. files[:tempfile].read.should.equal "contents"
  229. end
  230. it "rewinds input after parsing upload" do
  231. options = multipart_fixture(:text)
  232. input = options[:input]
  233. env = Rack::MockRequest.env_for("/", options)
  234. params = Rack::Multipart.parse_multipart(env)
  235. params["submit-name"].should.equal "Larry"
  236. params["files"][:filename].should.equal "file1.txt"
  237. input.read.length.should.equal 307
  238. end
  239. it "builds multipart body" do
  240. files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
  241. data = Rack::Multipart.build_multipart("submit-name" => "Larry", "files" => files)
  242. options = {
  243. "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
  244. "CONTENT_LENGTH" => data.length.to_s,
  245. :input => StringIO.new(data)
  246. }
  247. env = Rack::MockRequest.env_for("/", options)
  248. params = Rack::Multipart.parse_multipart(env)
  249. params["submit-name"].should.equal "Larry"
  250. params["files"][:filename].should.equal "file1.txt"
  251. params["files"][:tempfile].read.should.equal "contents"
  252. end
  253. it "builds nested multipart body" do
  254. files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
  255. data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => files}])
  256. options = {
  257. "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
  258. "CONTENT_LENGTH" => data.length.to_s,
  259. :input => StringIO.new(data)
  260. }
  261. env = Rack::MockRequest.env_for("/", options)
  262. params = Rack::Multipart.parse_multipart(env)
  263. params["people"][0]["submit-name"].should.equal "Larry"
  264. params["people"][0]["files"][:filename].should.equal "file1.txt"
  265. params["people"][0]["files"][:tempfile].read.should.equal "contents"
  266. end
  267. it "can parse fields that end at the end of the buffer" do
  268. input = File.read(multipart_file("bad_robots"))
  269. req = Rack::Request.new Rack::MockRequest.env_for("/",
  270. "CONTENT_TYPE" => "multipart/form-data, boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
  271. "CONTENT_LENGTH" => input.size,
  272. :input => input)
  273. req.POST['file.path'].should.equal "/var/tmp/uploads/4/0001728414"
  274. req.POST['addresses'].should.not.equal nil
  275. end
  276. it "builds complete params with the chunk size of 16384 slicing exactly on boundary" do
  277. data = File.open(multipart_file("fail_16384_nofile")) { |f| f.read }.gsub(/\n/, "\r\n")
  278. options = {
  279. "CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryWsY0GnpbI5U7ztzo",
  280. "CONTENT_LENGTH" => data.length.to_s,
  281. :input => StringIO.new(data)
  282. }
  283. env = Rack::MockRequest.env_for("/", options)
  284. params = Rack::Multipart.parse_multipart(env)
  285. params.should.not.equal nil
  286. params.keys.should.include "AAAAAAAAAAAAAAAAAAA"
  287. params["AAAAAAAAAAAAAAAAAAA"].keys.should.include "PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"
  288. params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"].keys.should.include "new"
  289. params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"].keys.should.include "-2"
  290. params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"].keys.should.include "ba_unit_id"
  291. params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"]["ba_unit_id"].should.equal "1017"
  292. end
  293. should "return nil if no UploadedFiles were used" do
  294. data = Rack::Multipart.build_multipart("people" => [{"submit-name" => "Larry", "files" => "contents"}])
  295. data.should.equal nil
  296. end
  297. should "raise ArgumentError if params is not a Hash" do
  298. lambda { Rack::Multipart.build_multipart("foo=bar") }.
  299. should.raise(ArgumentError).
  300. message.should.equal "value must be a Hash"
  301. end
  302. it "can parse fields with a content type" do
  303. data = <<-EOF
  304. --1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon\r
  305. Content-Disposition: form-data; name="description"\r
  306. Content-Type: text/plain"\r
  307. \r
  308. Very very blue\r
  309. --1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon--\r
  310. EOF
  311. options = {
  312. "CONTENT_TYPE" => "multipart/form-data; boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
  313. "CONTENT_LENGTH" => data.length.to_s,
  314. :input => StringIO.new(data)
  315. }
  316. env = Rack::MockRequest.env_for("/", options)
  317. params = Rack::Utils::Multipart.parse_multipart(env)
  318. params.should.equal({"description"=>"Very very blue"})
  319. end
  320. end