File: parser_test.rb

package info (click to toggle)
ruby-multipart-parser 0.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: ruby: 602; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 2,695 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
require 'test/unit'
require "multipart_parser/parser"
require "fixtures/multipart"

module MultipartParser
  class ParserTest < Test::Unit::TestCase
    def test_init_with_boundary
      parser = Parser.new
      def parser.boundary; @boundary end
      def parser.boundary_chars; @boundary_chars end

      parser.init_with_boundary("abc")
      assert_equal "\r\n--abc", parser.boundary
      expected_bc = {"\r"=>true, "\n"=>true, "-"=>true, "a"=>true, "b"=>true, "c"=>true}
      assert_equal expected_bc, parser.boundary_chars
    end

    def test_parser_error
      parser = Parser.new
      parser.init_with_boundary("abc")
      assert_equal 3, parser.write("--ad")
    end

    def test_fixtures
      parser = Parser.new
      chunk_length = 10
      Fixtures.fixtures.each do |fixture|
        buffer = fixture.raw
        parts = []
        part, header_field, header_value = nil, nil, nil
        end_called = false
        got_error = false

        parser.init_with_boundary(fixture.boundary)

        parser.on(:part_begin) do
          part = {:headers => {}, :data => ''}
          parts.push(part)
          header_field = ''
          header_value = ''
        end

        parser.on(:header_field) do |b, start, the_end|
          header_field += b[start...the_end]
        end

        parser.on(:header_value) do |b, start, the_end|
          header_value += b[start...the_end]
        end

        parser.on(:header_end) do
          part[:headers][header_field] = header_value
          header_field = ''
          header_value = ''
        end

        parser.on(:part_data) do |b, start, the_end|
          part[:data] += b[start...the_end]
        end

        parser.on(:end) do
          end_called = true
        end

        offset = 0
        while offset < buffer.length
          if(offset + chunk_length < buffer.length)
            chunk = buffer[offset, chunk_length]
          else
            chunk = buffer[offset...buffer.length]
          end
          offset += chunk_length

          nparsed = parser.write(chunk)
          if nparsed != chunk.length
            unless fixture.expect_error
              puts "--ERROR--"
              puts chunk
              flunk "#{fixture.class.name}: #{chunk.length} bytes written, " +
                    "but only #{nparsed} bytes parsed!"
            else
              got_error = true
            end
          end
        end
        unless got_error
          assert_equal true, end_called
          assert_equal fixture.parts, parts
        else
          assert fixture.expect_error,
              "#{fixture.class.name}: Expected parse error did not happen"
        end
      end
    end
  end
end