Permalink
Browse files

Merge pull request #2 from paneq/master

Thanks!
  • Loading branch information...
2 parents 9ca4631 + 46a2959 commit 3ae7c27d8783fc41903cbabe3b523f7eac0bda79 @danabr committed Jun 25, 2012
Showing with 67 additions and 6 deletions.
  1. +32 −6 test/fixtures/multipart.rb
  2. +35 −0 test/multipart_parser/{reader.rb → reader_test.rb}
View
@@ -10,7 +10,7 @@ class Rfc1867
def boundary
'AaB03x'
end
-
+
def expect_error
false
end
@@ -22,7 +22,7 @@ def parts
part2[:headers] = {}
part2[:headers]['content-disposition'] = 'form-data; name="pics"; ' +
'filename="file1.txt"'
- part2[:headers]['Content-Type'] = 'text/plain'
+ part2[:headers]['Content-Type'] = 'text/plain'
part2[:data] = "... contents of file1.txt ...\r"
[part1, part2]
end
@@ -59,11 +59,11 @@ def parts
part2[:headers] = {}
part2[:headers]['content-disposition'] = 'form-data; name="pics"; ' +
'filename="file1.txt"'
- part2[:headers]['Content-Type'] = 'text/plain'
+ part2[:headers]['Content-Type'] = 'text/plain'
part2[:data] = "... contents of file1.txt ...\r"
[part1, part2]
end
-
+
def raw
['--AaB03x',
'content-disposition: form-data; name="field1"',
@@ -79,15 +79,41 @@ def raw
end
end
+ class LongBoundary
+ def boundary
+ '----------------------------5c4dc587f69f'
+ end
+
+ def expect_error
+ false
+ end
+
+ def parts
+ part1 = {}
+ part1[:headers] = {'content-disposition' => 'form-data; name="field1"'}
+ part1[:data] = "Joe Blow\r\nalmost tricked you!"
+ [part1]
+ end
+
+ def raw
+ ['----------------------------5c4dc587f69f',
+ 'content-disposition: form-data; name="field1"',
+ '',
+ "Joe Blow\r\nalmost tricked you!",
+ '----------------------------5c4dc587f69f--'
+ ].join("\r\n")
+ end
+ end
+
class EmptyHeader
def boundary
'AaB03x'
end
-
+
def expect_error
true
end
-
+
def parts
[] # Should never be called
end
@@ -70,5 +70,40 @@ def test_success_scenario
assert_equal 'file1.txt', file[:part].filename
assert_equal fixture.parts.last[:data], file[:data]
end
+
+ def test_long
+ fixture = Fixtures::LongBoundary.new
+ reader = Reader.new(fixture.boundary)
+ on_error_called = false
+ parts = {}
+
+ reader.on_error do |err|
+ on_error_called = true
+ end
+
+ reader.on_part do |part|
+ part_entry = {:part => part, :data => '', :ended => false}
+ parts[part.name] = part_entry
+ part.on_data do |data|
+ part_entry[:data] << data
+ end
+ part.on_end do
+ part_entry[:ended] = true
+ end
+ end
+
+ reader.write(fixture.raw)
+
+ assert !on_error_called
+ assert reader.ended?
+
+ assert_equal parts.size, fixture.parts.size
+ assert parts.all? {|k, v| v[:ended]}
+
+ field = parts['field1']
+ assert !field.nil?
+ assert_equal 'field1', field[:part].name
+ assert_equal fixture.parts.first[:data], field[:data]
+ end
end
end

0 comments on commit 3ae7c27

Please sign in to comment.