File: test_mechanize_parser.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (315 lines) | stat: -rw-r--r-- 6,890 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
require 'mechanize/test_case'

class TestMechanizeParser < Mechanize::TestCase

  class P
    include Mechanize::Parser

    attr_accessor :filename
    attr_accessor :response
    attr_accessor :uri

    def initialize
      @uri = URI 'http://example'
      @full_path = false
    end
  end

  def setup
    super

    @parser = P.new
  end

  def test_extract_filename
    @parser.response = {}

    assert_equal 'index.html', @parser.extract_filename
  end

  def test_extract_filename_content_disposition
    @parser.uri = URI 'http://example/foo'

    @parser.response = {
      'content-disposition' => 'attachment; filename=genome.jpeg'
    }

    assert_equal 'genome.jpeg', @parser.extract_filename
  end

  def test_extract_filename_content_disposition_bad
    @parser.uri = URI 'http://example/foo'

    @parser.response = {
      'content-disposition' => "inline; filename*=UTF-8''X%20Y.jpg"
    }

    assert_equal 'foo.html', @parser.extract_filename

    @parser.response = {
      'content-disposition' => "inline; filename=\"\""
    }

    assert_equal 'foo.html', @parser.extract_filename
  end

  def test_extract_filename_content_disposition_path
    @parser.uri = URI 'http://example'

    @parser.response = {
      'content-disposition' => 'attachment; filename="../genome.jpeg"'
    }

    assert_equal 'example/genome.jpeg', @parser.extract_filename(true)

    @parser.response = {
      'content-disposition' => 'attachment; filename="foo/genome.jpeg"'
    }

    assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
  end

  def test_extract_filename_content_disposition_path_windows
    @parser.uri = URI 'http://example'

    @parser.response = {
      'content-disposition' => 'attachment; filename="..\\\\genome.jpeg"'
    }

    assert_equal 'example/genome.jpeg', @parser.extract_filename(true)

    @parser.response = {
      'content-disposition' => 'attachment; filename="foo\\\\genome.jpeg"'
    }

    assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
  end

  def test_extract_filename_content_disposition_full_path
    @parser.uri = URI 'http://example/foo'

    @parser.response = {
      'content-disposition' => 'attachment; filename=genome.jpeg'
    }

    assert_equal 'example/genome.jpeg', @parser.extract_filename(true)
  end

  def test_extract_filename_content_disposition_quoted
    @parser.uri = URI 'http://example'

    @parser.response = {
      'content-disposition' => 'attachment; filename="\"some \"file\""'
    }

    assert_equal '_some__file_', @parser.extract_filename
  end

  def test_extract_filename_content_disposition_special
    @parser.uri = URI 'http://example/foo'

    @parser.response = {
      'content-disposition' => 'attachment; filename="/\\\\<>:\\"|?*"'
    }

    assert_equal '_______', @parser.extract_filename

    chars = (0..12).map { |c| c.chr }.join
    chars += "\\\r"
    chars += (14..31).map { |c| c.chr }.join

    @parser.response = {
      'content-disposition' => "attachment; filename=\"#{chars}\""
    }

    assert_equal '_' * 32, @parser.extract_filename
  end

  def test_extract_filename_content_disposition_windows_special
    @parser.uri = URI 'http://example'

    windows_special = %w[
      AUX
      COM1
      COM2
      COM3
      COM4
      COM5
      COM6
      COM7
      COM8
      COM9
      CON
      LPT1
      LPT2
      LPT3
      LPT4
      LPT5
      LPT6
      LPT7
      LPT8
      LPT9
      NUL
      PRN
    ]

    windows_special.each do |special|
      @parser.response = {
        'content-disposition' => "attachment; filename=#{special}"
      }

      assert_equal "_#{special}", @parser.extract_filename
    end
  end

  def test_extract_filename_content_disposition_empty
    @parser.uri = URI 'http://example'

    @parser.response = {
      'content-disposition' => 'inline; filename="/"'
    }

    assert_equal '', @parser.extract_filename
  end

  def test_extract_filename_host
    @parser.response = {}
    @parser.uri = URI 'http://example'

    assert_equal 'example/index.html', @parser.extract_filename(true)
  end

  def test_extract_filename_special_character
    @parser.response = {}

    invisible = "\t\n\v\f\r"

    invisible.chars.each do |char|
      begin
        @parser.uri = URI "http://example/#{char}"

        assert_equal 'index.html', @parser.extract_filename, char.inspect
      rescue URI::InvalidURIError
        # ignore
      end
    end

    escaped = "<>\"\\|"

    escaped.chars.each do |char|
      escaped_char = CGI.escape char

      @parser.uri = URI "http://example/#{escaped_char}"

      assert_equal "#{escaped_char}.html", @parser.extract_filename, char
    end

    @parser.uri = URI "http://example/?"

    assert_equal 'index.html_', @parser.extract_filename, 'empty query'

    @parser.uri = URI "http://example/:"

    assert_equal '_.html', @parser.extract_filename, 'colon'

    @parser.uri = URI "http://example/*"

    assert_equal '_.html', @parser.extract_filename, 'asterisk'
  end

  def test_extract_filename_uri
    @parser.response = {}
    @parser.uri = URI 'http://example/foo'

    assert_equal 'foo.html', @parser.extract_filename

    @parser.uri += '/foo.jpg'

    assert_equal 'foo.jpg', @parser.extract_filename
  end

  def test_extract_filename_uri_full_path
    @parser.response = {}
    @parser.uri = URI 'http://example/foo'

    assert_equal 'example/foo.html', @parser.extract_filename(true)

    @parser.uri += '/foo.jpg'

    assert_equal 'example/foo.jpg', @parser.extract_filename(true)
  end

  def test_extract_filename_uri_query
    @parser.response = {}
    @parser.uri = URI 'http://example/?id=5'

    assert_equal 'index.html_id=5', @parser.extract_filename

    @parser.uri += '/foo.html?id=5'

    assert_equal 'foo.html_id=5', @parser.extract_filename
  end

  def test_extract_filename_uri_slash
    @parser.response = {}
    @parser.uri = URI 'http://example/foo/'

    assert_equal 'example/foo/index.html', @parser.extract_filename(true)

    @parser.uri += '/foo///'

    assert_equal 'example/foo/index.html', @parser.extract_filename(true)
  end

  def test_extract_filename_windows_special
    @parser.uri = URI 'http://example'
    @parser.response = {}

    windows_special = %w[
      AUX
      COM1
      COM2
      COM3
      COM4
      COM5
      COM6
      COM7
      COM8
      COM9
      CON
      LPT1
      LPT2
      LPT3
      LPT4
      LPT5
      LPT6
      LPT7
      LPT8
      LPT9
      NUL
      PRN
    ]

    windows_special.each do |special|
      @parser.uri += "/#{special}"

      assert_equal "_#{special}.html", @parser.extract_filename
    end
  end

  def test_fill_header
    @parser.fill_header 'a' => 'b'

    expected = { 'a' => 'b' }

    assert_equal expected, @parser.response
  end

  def test_fill_header_nil
    @parser.fill_header nil

    assert_empty @parser.response
  end

end