File: test_mechanize_http_content_disposition_parser.rb

package info (click to toggle)
ruby-mechanize 2.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,416 kB
  • sloc: ruby: 11,645; makefile: 7; sh: 4
file content (167 lines) | stat: -rw-r--r-- 4,577 bytes parent folder | download | duplicates (2)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# frozen_string_literal: true
require 'mechanize/test_case'

class TestMechanizeHttpContentDispositionParser < Mechanize::TestCase

  def setup
    super

    @parser = Mechanize::HTTP::ContentDispositionParser.new
  end

  def test_parse
    now = Time.at Time.now.to_i

    content_disposition = @parser.parse \
      'attachment;' \
      'filename=value;' \
      "creation-date=\"#{now.rfc822}\";" \
      "modification-date=\"#{(now + 1).rfc822}\";" \
      "read-date=\"#{(now + 2).rfc822}\";" \
      'size=5;' \
      'arbitrary=value'

    assert_equal 'attachment', content_disposition.type
    assert_equal 'value',      content_disposition.filename
    assert_equal now,          content_disposition.creation_date
    assert_equal((now + 1),    content_disposition.modification_date)
    assert_equal((now + 2),    content_disposition.read_date)
    assert_equal 5,            content_disposition.size
    expected = { 'arbitrary' => 'value' }
    assert_equal expected,     content_disposition.parameters
  end

  def test_parse_date_iso8601_fallback
    now = Time.at Time.now.to_i

    content_disposition = @parser.parse \
      'attachment;' \
      'filename=value;' \
      "creation-date=\"#{now.iso8601}\";" \
      "modification-date=\"#{(now + 1).iso8601}\""

    assert_equal 'attachment', content_disposition.type
    assert_equal 'value',      content_disposition.filename
    assert_equal now,          content_disposition.creation_date
    assert_equal((now + 1),    content_disposition.modification_date)
  end

  def test_parse_date_invalid
    now = Time.at Time.now.to_i

    content_disposition = @parser.parse \
      'attachment;' \
      'filename=value;' \
      "creation-date=\"#{now.to_s}\";" \
      "modification-date=\"#{(now + 1).to_s}\""

    assert_nil content_disposition
  end

  def test_parse_header
    content_disposition = @parser.parse \
      'content-disposition: attachment;filename=value', true

    assert_equal 'attachment', content_disposition.type
    assert_equal 'value',      content_disposition.filename
  end

  def test_parse_no_type
    content_disposition = @parser.parse 'filename=value'

    assert_nil            content_disposition.type
    assert_equal 'value', content_disposition.filename
  end

  def test_parse_semicolons
    content_disposition = @parser.parse 'attachment;;filename=value'

    assert_equal 'attachment', content_disposition.type
    assert_equal 'value',      content_disposition.filename
  end

  def test_parse_quoted_size
    content_disposition = @parser.parse 'size="5"'

    assert_equal 5, content_disposition.size
  end

  def test_rfc_2045_quoted_string
    @parser.scanner = StringScanner.new '"text"'

    string = @parser.rfc_2045_quoted_string

    assert_equal 'text', string
  end

  def test_rfc_2045_quoted_string_bad
    @parser.scanner = StringScanner.new '"text'

    assert_nil @parser.rfc_2045_quoted_string
  end

  def test_rfc_2045_quoted_string_crlf
    @parser.scanner = StringScanner.new "\"multiline\\\r\n\ttext\""

    string = @parser.rfc_2045_quoted_string

    assert_equal "multiline\r\n\ttext", string
  end

  def test_rfc_2045_quoted_string_escape
    @parser.scanner = StringScanner.new "\"escape\\ text\""

    string = @parser.rfc_2045_quoted_string

    assert_equal 'escape text', string
  end

  def test_rfc_2045_quoted_string_escape_bad
    @parser.scanner = StringScanner.new '"escape\\'

    string = @parser.rfc_2045_quoted_string

    assert_nil string
  end

  def test_rfc_2045_quoted_string_folded
    @parser.scanner = StringScanner.new "\"multiline\r\n\ttext\""

    string = @parser.rfc_2045_quoted_string

    assert_equal 'multiline text', string
  end

  def test_rfc_2045_quoted_string_quote
    @parser.scanner = StringScanner.new '"escaped \\" here"'

    string = @parser.rfc_2045_quoted_string

    assert_equal 'escaped " here', string
  end

  def test_rfc_2045_quoted_string_quote_end
    @parser.scanner = StringScanner.new '"end \\""'

    string = @parser.rfc_2045_quoted_string

    assert_equal 'end "', string
  end

  def test_parse_uppercase
    content_disposition = @parser.parse \
      'content-disposition: attachment; Filename=value', true

    assert_equal 'attachment', content_disposition.type
    assert_equal 'value',      content_disposition.filename
  end

  def test_parse_filename_starting_with_escaped_quote
    content_disposition = @parser.parse \
      'content-disposition: attachment; Filename="\"value\""', true

    assert_equal '"value"', content_disposition.filename
  end

end