summaryrefslogtreecommitdiff
path: root/test/spec_multipart.rb
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2022-05-25 10:11:53 -0700
committerJeremy Evans <code@jeremyevans.net>2022-05-27 09:48:14 -0700
commitfa68d9df5f3e9c40f8d40674305a94a0cdf63042 (patch)
tree6aa6c0f726a4bd227f6bb4cec488e0b8a0e51571 /test/spec_multipart.rb
parentb426cc224908ec6ed6eb8729325392b048215d88 (diff)
downloadrack-fa68d9df5f3e9c40f8d40674305a94a0cdf63042.tar.gz
Refactor multipart boundary parsing
Remove the use of BOUNDARY_REGEX, as well as the @boundary, @full_boundary, and @end_boundary instance variables. Replace them with the existing @body_regex, which already contains the boundary. Tweak body_regex slightly so the preceding EOF is required if we aren't at the start of the buffer, since boundaries must be on their own lines. When looking for the boundary, scan until using @body_regex (i.e. until you found the boundary). Differentiate the full_boundary from the end_boundary by seeing if the scanned data ends with \r\n (full boundary will, end_boundary must end with --). If no boundary is found, clear the buffer and try again, for behavior similar to previous. For the fast forward state, if the first boundary found is the end boundary, ignore it and keep scanning. We could alternatively raise an exception, not sure if that is better. The previous behavior of treating the end boundary as the starting boundary was definitely wrong, though. Add some comments describing this boundary parsing behavior (which was my initial intent when working on this). Speeds up test/spec_multipart.rb more than 6x in my testing.
Diffstat (limited to 'test/spec_multipart.rb')
-rw-r--r--test/spec_multipart.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/test/spec_multipart.rb b/test/spec_multipart.rb
index 53787cb7..a1e0a7e4 100644
--- a/test/spec_multipart.rb
+++ b/test/spec_multipart.rb
@@ -65,6 +65,19 @@ describe Rack::Multipart do
params["text"].must_equal "contents"
end
+ it "raises for invalid data preceding the boundary" do
+ env = Rack::MockRequest.env_for '/', multipart_fixture(:preceding_boundary)
+ lambda {
+ Rack::Multipart.parse_multipart(env)
+ }.must_raise Rack::Multipart::EmptyContentError
+ end
+
+ it "ignores initial end boundaries" do
+ env = Rack::MockRequest.env_for '/', multipart_fixture(:end_boundary_first)
+ params = Rack::Multipart.parse_multipart(env)
+ params["files"][:filename].must_equal "foo"
+ end
+
it "parse multipart content with different filename and filename*" do
env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_multi)
params = Rack::Multipart.parse_multipart(env)