metainspector 5.21.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/README.md +215 -48
  4. data/lib/meta_inspector/attempt_timeout.rb +40 -0
  5. data/lib/meta_inspector/document.rb +101 -68
  6. data/lib/meta_inspector/errors.rb +5 -1
  7. data/lib/meta_inspector/html_encoding.rb +183 -0
  8. data/lib/meta_inspector/http_session.rb +74 -45
  9. data/lib/meta_inspector/image_downloader.rb +37 -24
  10. data/lib/meta_inspector/parser.rb +144 -34
  11. data/lib/meta_inspector/parsers/base.rb +7 -2
  12. data/lib/meta_inspector/parsers/head_links.rb +20 -28
  13. data/lib/meta_inspector/parsers/iframes.rb +2 -3
  14. data/lib/meta_inspector/parsers/images.rb +119 -70
  15. data/lib/meta_inspector/parsers/links.rb +19 -9
  16. data/lib/meta_inspector/parsers/meta_tags.rb +22 -31
  17. data/lib/meta_inspector/parsers/texts.rb +43 -33
  18. data/lib/meta_inspector/request.rb +67 -55
  19. data/lib/meta_inspector/url.rb +43 -19
  20. data/lib/meta_inspector/url_policy.rb +18 -11
  21. data/lib/meta_inspector/version.rb +1 -1
  22. data/lib/meta_inspector.rb +14 -13
  23. data/lib/metainspector.rb +1 -1
  24. data/meta_inspector.gemspec +1 -1
  25. data/spec/document_spec.rb +154 -56
  26. data/spec/meta_inspector/content_type_spec.rb +47 -0
  27. data/spec/meta_inspector/head_links_spec.rb +3 -3
  28. data/spec/meta_inspector/images_spec.rb +82 -1
  29. data/spec/meta_inspector/links_spec.rb +98 -2
  30. data/spec/meta_inspector/meta_inspector_spec.rb +3 -25
  31. data/spec/meta_inspector/meta_tags_spec.rb +53 -0
  32. data/spec/meta_inspector/texts_spec.rb +140 -3
  33. data/spec/meta_inspector/timeouts_spec.rb +107 -0
  34. data/spec/noscript_spec.rb +179 -0
  35. data/spec/parser_spec.rb +380 -10
  36. data/spec/query_string_spec.rb +125 -0
  37. data/spec/request_errors_spec.rb +91 -0
  38. data/spec/request_spec.rb +28 -10
  39. data/spec/response_encoding_spec.rb +487 -0
  40. data/spec/url_filter_spec.rb +2 -1
  41. data/spec/url_spec.rb +37 -0
  42. metadata +10 -2
@@ -0,0 +1,487 @@
1
+ require 'spec_helper'
2
+ require 'zlib'
3
+
4
+ RSpec.describe 'conflicting page charset declarations' do
5
+ let(:url) { 'https://encoding.example/page' }
6
+ let(:html) do
7
+ '<html><head><meta charset=utf-8><title>中国</title></head>' \
8
+ '<body><p>新闻</p><meta charset=gbk><a href="/a">a</a></body></html>'
9
+ end
10
+
11
+ it 'uses the first meta charset when Content-Type has no charset' do
12
+ body = html.b
13
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
14
+
15
+ page = MetaInspector.new(url)
16
+
17
+ expect(page.title).to eq('中国')
18
+ expect(page.links.raw).to eq(['/a'])
19
+ expect(page.response.body).to eq(body)
20
+ expect(page.to_s.encoding).to eq(Encoding::BINARY)
21
+ end
22
+
23
+ it 'preserves the HTTP charset over conflicting meta declarations' do
24
+ body = html.encode('GBK').b
25
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html; charset=GBK' })
26
+
27
+ page = MetaInspector.new(url)
28
+
29
+ expect(page.title).to eq('中国')
30
+ expect(page.links.raw).to eq(['/a'])
31
+ expect(page.to_s.encoding).to eq(Encoding.find('GBK'))
32
+ end
33
+
34
+ it 'preserves a forced encoding over conflicting meta declarations' do
35
+ stub_request(:get, url).to_return(body: html.b, headers: { 'Content-Type' => 'text/html; charset=GBK' })
36
+
37
+ page = MetaInspector.new(url, encoding: 'UTF-8')
38
+
39
+ expect(page.title).to eq('中国')
40
+ expect(page.links.raw).to eq(['/a'])
41
+ end
42
+
43
+ it 'honors a UTF-8 BOM when Content-Type has no charset' do
44
+ body = "\xEF\xBB\xBF".b + html.sub('charset=utf-8', 'charset=gbk').b
45
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
46
+
47
+ page = MetaInspector.new(url)
48
+
49
+ expect(page.title).to eq('中国')
50
+ expect(page.links.raw).to eq(['/a'])
51
+ expect(page.response.body).to eq(body)
52
+ expect(page.to_s.encoding).to eq(Encoding::BINARY)
53
+ end
54
+ end
55
+
56
+ RSpec.describe 'byte-order-mark response encoding' do
57
+ let(:url) { 'https://encoding.example/page' }
58
+
59
+ {
60
+ 'UTF-16LE' => "\xFF\xFE".b,
61
+ 'UTF-16BE' => "\xFE\xFF".b,
62
+ 'UTF-32LE' => "\xFF\xFE\x00\x00".b,
63
+ 'UTF-32BE' => "\x00\x00\xFE\xFF".b
64
+ }.each do |encoding, mark|
65
+ ['', '<meta charset=ISO-8859-1>'].each do |meta|
66
+ it "parses #{encoding} without an HTTP charset and with #{meta.empty? ? 'no' : 'a conflicting'} meta charset" do
67
+ html = "<html><head>#{meta}<title>Título 日本語</title></head>" \
68
+ '<body><h1>Heading</h1><a href="/one">one</a><img src="/image.png"></body></html>'
69
+ body = (mark + html.encode(encoding).b).freeze
70
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
71
+
72
+ page = MetaInspector.new(url, download_images: false)
73
+ supplied = MetaInspector.new(url, document: body, download_images: false)
74
+
75
+ expect(page.title).to eq('Título 日本語')
76
+ expect(page.h1).to eq(['Heading'])
77
+ expect(page.links.raw).to eq(['/one'])
78
+ expect(page.images.to_a).to eq(['https://encoding.example/image.png'])
79
+ expect([page.title, page.h1, page.links.raw, page.images.to_a])
80
+ .to eq([supplied.title, supplied.h1, supplied.links.raw, supplied.images.to_a])
81
+ expect(page.to_s.encoding).to eq(Encoding::UTF_8)
82
+ expect(page.response.body).to eq(body)
83
+ expect(page.response.body.encoding).to eq(Encoding::BINARY)
84
+ end
85
+ end
86
+
87
+ it "removes NUL characters after decoding a #{encoding} byte-order mark" do
88
+ html = "<title>Hel\u0000lo 日本語</title><a href=\"/a\u0000fter\">after</a>"
89
+ body = mark + html.encode(encoding).b
90
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
91
+
92
+ page = MetaInspector.new(url)
93
+
94
+ expect(page.title).to eq('Hello 日本語')
95
+ expect(page.links.raw).to eq(['/after'])
96
+ expect(page.to_s).not_to include("\u0000")
97
+ expect(page.response.body).to eq(body)
98
+ end
99
+ end
100
+
101
+ [false, true].each do |forced|
102
+ it "preserves #{forced ? 'a forced' : 'the HTTP'} charset over a byte-order mark" do
103
+ body = "\xFF\xFE".b + '<title>Hello</title>'.encode('UTF-16LE').b
104
+ stub_request(:get, url).to_return(body: body, headers: {
105
+ 'Content-Type' => forced ? 'text/html' : 'text/html; charset=ISO-8859-1'
106
+ })
107
+
108
+ page = MetaInspector.new(url, **(forced ? { encoding: 'ISO-8859-1' } : {}))
109
+
110
+ expect(page.to_s).to eq('ÿþ<title>Hello</title>')
111
+ expect(page.response.body.b).to eq(body)
112
+ end
113
+ end
114
+
115
+ it 'decodes a byte-order mark after gzip decompression' do
116
+ body = "\xFF\xFE".b + '<title>Título</title>'.encode('UTF-16LE').b
117
+ stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
118
+ 'Content-Type' => 'text/html', 'Content-Encoding' => 'gzip'
119
+ })
120
+
121
+ page = MetaInspector.new(url)
122
+
123
+ expect(page.title).to eq('Título')
124
+ expect(page.response.body).to eq(body)
125
+ end
126
+
127
+ it 'replaces invalid BOM-declared UTF-16 bytes' do
128
+ body = "\xFF\xFE".b + '<title>Hello</title>'.encode('UTF-16LE').b + "\x00".b
129
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
130
+
131
+ page = MetaInspector.new(url)
132
+
133
+ expect(page.title).to eq('Hello')
134
+ expect(page.to_s).to end_with("\u{FFFD}")
135
+ end
136
+ end
137
+
138
+ RSpec.describe 'legacy HTML charset labels' do
139
+ let(:url) { 'https://encoding.example/page' }
140
+
141
+ [
142
+ ['gb2312', 'GBK', '朱镕基'],
143
+ ['shift_jis', 'Windows-31J', '①㈱髙'],
144
+ ['euc-kr', 'CP949', '똠'],
145
+ ['big5', 'Big5-HKSCS', '嗰'],
146
+ ['iso-8859-1', 'Windows-1252', 'It’s €5']
147
+ ].each do |label, encoding, title|
148
+ context "with #{label} containing #{encoding} characters" do
149
+ let(:html) do
150
+ %(<html><head><meta charset="#{label}"><title>#{title}</title></head>) +
151
+ %(<body><h1><p>#{title}</p></h1><a href="/after">after</a></body></html>)
152
+ end
153
+ let(:bytes) { html.encode(encoding).b }
154
+
155
+ def expect_complete_page(page, title)
156
+ expect(page.title).to eq(title)
157
+ expect(page.h1).to eq([title])
158
+ expect(page.links.raw).to eq(['/after'])
159
+ expect(page.parsed.errors.select(&:fatal?)).to be_empty
160
+ end
161
+
162
+ it 'parses a supplied binary document without changing its bytes or declared charset' do
163
+ original = bytes.dup
164
+ page = MetaInspector.new(url, document: bytes.freeze)
165
+
166
+ expect_complete_page(page, title)
167
+ expect(page.charset).to eq(label)
168
+ expect(page.to_s).to equal(bytes)
169
+ expect(bytes).to eq(original)
170
+ end
171
+
172
+ it 'parses a supplied string tagged with the strict encoding before scrubbing' do
173
+ tagged = bytes.dup.force_encoding(label).freeze
174
+ page = MetaInspector.new(url, document: tagged)
175
+
176
+ expect_complete_page(page, title)
177
+ expect(page.to_s).to equal(tagged)
178
+ end
179
+
180
+ it 'parses a response declaring its charset only in HTML' do
181
+ stub_request(:get, url).to_return(body: bytes, headers: { 'Content-Type' => 'text/html' })
182
+
183
+ expect_complete_page(MetaInspector.new(url), title)
184
+ end
185
+
186
+ it 'preserves the first legacy declaration when a later meta declares UTF-8' do
187
+ body = html.sub('</head>', '<meta charset="UTF-8"></head>').encode(encoding).b
188
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
189
+
190
+ expect_complete_page(MetaInspector.new(url, document: body.freeze), title)
191
+ expect_complete_page(MetaInspector.new(url), title)
192
+ end
193
+
194
+ it 'parses an http-equiv declaration with mixed case and reordered attributes' do
195
+ body = html.sub(%(<meta charset="#{label}">),
196
+ %(<META content="text/html; charset=#{label.upcase}" HTTP-EQUIV="Content-Type">))
197
+ .encode(encoding).b
198
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
199
+
200
+ expect_complete_page(MetaInspector.new(url), title)
201
+ end
202
+
203
+ it 'parses a charset declared only in the HTTP header, preserving the raw body' do
204
+ body = html.sub(%(<meta charset="#{label}">), '').encode(encoding).b
205
+ stub_request(:get, url).to_return(body: body, headers: {
206
+ 'Content-Type' => "text/html; charset=#{label}"
207
+ })
208
+ page = MetaInspector.new(url)
209
+
210
+ expect_complete_page(page, title)
211
+ expect(page.response.body.b).to eq(body)
212
+ expect(page.response.body.encoding).to eq(Encoding.find(label))
213
+ end
214
+
215
+ it 'parses the HTTP charset after gzip decompression' do
216
+ stub_request(:get, url).to_return(body: Zlib.gzip(bytes), headers: {
217
+ 'Content-Type' => "text/html; charset=#{label}", 'Content-Encoding' => 'gzip'
218
+ })
219
+
220
+ expect_complete_page(MetaInspector.new(url), title)
221
+ end
222
+
223
+ it 'supports a forced legacy encoding without discarding superset characters' do
224
+ stub_request(:get, url).to_return(body: bytes, headers: { 'Content-Type' => 'text/html' })
225
+
226
+ expect_complete_page(MetaInspector.new(url, encoding: label), title)
227
+ end
228
+ end
229
+ end
230
+
231
+ it 'keeps a UTF-8 string authoritative over a legacy meta declaration' do
232
+ html = '<meta charset="gb2312"><title>朱镕基</title><a href="/after">after</a>'
233
+
234
+ page = MetaInspector.new(url, document: html)
235
+
236
+ expect(page.title).to eq('朱镕基')
237
+ expect(page.links.raw).to eq(['/after'])
238
+ end
239
+
240
+ it 'uses Windows-31J character mappings for both binary and tagged strings' do
241
+ title = '~∥-¢£¬'
242
+ html = %(<meta charset="shift_jis"><title>#{title}</title><a href="/after">after</a>)
243
+ bytes = html.encode('Windows-31J')
244
+
245
+ [bytes.b, bytes.dup.force_encoding('Shift_JIS')].each do |document|
246
+ page = MetaInspector.new(url, document: document)
247
+
248
+ expect(page.title).to eq(title)
249
+ expect(page.links.raw).to eq(['/after'])
250
+ end
251
+ end
252
+
253
+ it 'keeps a forced UTF-8 encoding authoritative over the HTTP charset' do
254
+ stub_request(:get, url).to_return(body: '<title>朱镕基</title>'.b, headers: {
255
+ 'Content-Type' => 'text/html; charset=gb2312'
256
+ })
257
+
258
+ expect(MetaInspector.new(url, encoding: 'UTF-8').title).to eq('朱镕基')
259
+ end
260
+ end
261
+
262
+ # https://encoding.spec.whatwg.org/#names-and-labels
263
+ RSpec.describe 'Encoding Standard charset labels' do
264
+ let(:url) { 'https://encoding.example/page' }
265
+
266
+ def page_with(title_bytes, meta: nil, header: nil)
267
+ declaration = meta ? %(<meta http-equiv="Content-Type" content="text/html; charset=#{meta}">) : ''
268
+ body = %(<html><head>#{declaration}<title>).b + title_bytes.b +
269
+ '</title></head><body><a href="/one">one</a></body></html>'.b
270
+ content_type = header ? "text/html; charset=#{header}" : 'text/html'
271
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => content_type })
272
+ MetaInspector.new(url)
273
+ end
274
+
275
+ [
276
+ ['ks_c_5601-1987', "\xC7\xD1\xB1\xDB", '한글'],
277
+ ['korean', "\xC7\xD1\xB1\xDB", '한글'],
278
+ ['windows-949', "\xC7\xD1\xB1\xDB", '한글'],
279
+ ['x-sjis', "\x93\xFA\x96\x7B", '日本'],
280
+ ['ms932', "\x93\xFA\x96\x7B", '日本'],
281
+ ['x-euc-jp', "\xC6\xFC\xCB\xDC", '日本'],
282
+ ['x-gbk', "\xD6\xD0", '中'],
283
+ ['chinese', "\xD6\xD0", '中'],
284
+ ['us-ascii', "caf\xE9 \x80", 'café €'],
285
+ ['ascii', "caf\xE9", 'café'],
286
+ ['iso-8859-9', "\x80 \xFD", '€ ı'],
287
+ ['x-cp1251', "\xC4\xE0", 'Да'],
288
+ ['unicode-1-1-utf-8', "caf\xC3\xA9", 'café']
289
+ ].each do |label, bytes, title|
290
+ it "decodes #{label} declared in a meta tag" do
291
+ page = page_with(bytes, meta: label)
292
+
293
+ expect(page.title).to eq(title)
294
+ expect(page.links.raw).to eq(['/one'])
295
+ end
296
+
297
+ it "decodes #{label} declared in the Content-Type header" do
298
+ page = page_with(bytes, header: label)
299
+
300
+ expect(page.title).to eq(title)
301
+ expect(page.links.raw).to eq(['/one'])
302
+ end
303
+ end
304
+
305
+ %w[utf-16 utf-16le utf-16be unicode].each do |label|
306
+ it "reads a #{label} meta declaration as UTF-8" do
307
+ page = page_with("caf\xC3\xA9", meta: label)
308
+
309
+ expect(page.title).to eq('café')
310
+ expect(page.links.raw).to eq(['/one'])
311
+ end
312
+ end
313
+
314
+ it 'ignores an unknown meta label instead of truncating the document' do
315
+ page = page_with("caf\xE9", meta: 'no-such-charset')
316
+
317
+ expect(page.title).to eq('café')
318
+ expect(page.links.raw).to eq(['/one'])
319
+ end
320
+
321
+ it 'uses a later meta declaration when the first label is unknown' do
322
+ body = '<meta charset="no-such-charset"><meta charset="x-sjis"><title>'.b + "\x93\xFA\x96\x7B".b +
323
+ '</title><a href="/one">one</a>'.b
324
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html' })
325
+
326
+ expect(MetaInspector.new(url).title).to eq('日本')
327
+ end
328
+ end
329
+
330
+ RSpec.describe 'compressed page encoding' do
331
+ let(:url) { 'https://encoding.example/page' }
332
+ let(:title) { 'Café – £5' }
333
+ let(:description) { 'Gonçalves – café' }
334
+ let(:html) do
335
+ %(<html><head><title>#{title}</title>) +
336
+ %(<meta name="description" content="#{description}"></head></html>)
337
+ end
338
+
339
+ %w[gzip deflate].each do |compression|
340
+ context "with #{compression} responses" do
341
+ %w[UTF-8 Windows-1252].each do |charset|
342
+ context "declaring #{charset} only in Content-Type" do
343
+ before do
344
+ body = html.encode(charset)
345
+ compressed = compression == 'gzip' ? Zlib.gzip(body) : Zlib::Deflate.deflate(body)
346
+ stub_request(:get, url).to_return(body: compressed, headers: {
347
+ 'Content-Type' => "text/html; charset=#{charset}",
348
+ 'Content-Encoding' => compression
349
+ })
350
+ end
351
+
352
+ it 'preserves the charset when the server compresses despite the default headers' do
353
+ page = MetaInspector.new(url)
354
+
355
+ expect(page.title).to eq(title)
356
+ expect(page.description).to eq(description)
357
+ expect(page.to_s.encoding).to eq(Encoding.find(charset))
358
+ expect(a_request(:get, url).with(headers: { 'Accept-Encoding' => 'identity' })).to have_been_made.once
359
+ end
360
+
361
+ it 'preserves the charset with a custom User-Agent' do
362
+ page = MetaInspector.new(url, headers: { 'User-Agent' => 'MyApp/1.0' })
363
+
364
+ expect(page.title).to eq(title)
365
+ expect(page.description).to eq(description)
366
+ expect(page.to_s.encoding).to eq(Encoding.find(charset))
367
+ expect(a_request(:get, url).with(headers: { 'User-Agent' => 'MyApp/1.0' })).to have_been_made.once
368
+ end
369
+ end
370
+ end
371
+ end
372
+ end
373
+ end
374
+
375
+ RSpec.describe 'non-ASCII-compatible page encoding' do
376
+ let(:url) { 'https://encoding.example/page' }
377
+ let(:html) do
378
+ "<html><head><title>Hel\u0000lo 日本語</title></head>" \
379
+ "<body><a href=\"/a\u0000fter\">after</a></body></html>"
380
+ end
381
+
382
+ %w[UTF-16LE UTF-16BE UTF-16 UTF-32LE UTF-32BE UTF-32 ISO-2022-JP].each do |charset|
383
+ it "parses #{charset} responses and removes NUL characters without changing the raw body" do
384
+ body = html.encode(charset).b
385
+ stub_request(:get, url).to_return(body: body, headers: {
386
+ 'Content-Type' => "text/html; charset=#{charset}"
387
+ })
388
+
389
+ page = MetaInspector.new(url)
390
+
391
+ expect(page.title).to eq('Hello 日本語')
392
+ expect(page.links.raw).to eq(['/after'])
393
+ expect(page.to_s.encoding).to eq(Encoding::UTF_8)
394
+ expect(page.to_s).not_to include("\u0000")
395
+ expect(page.response.body.b).to eq(body)
396
+ expect(page.response.body.encoding).to eq(Encoding.find(charset))
397
+ end
398
+ end
399
+
400
+ it 'normalizes a forced UTF-16LE encoding before removing NUL characters' do
401
+ stub_request(:get, url).to_return(body: html.encode('UTF-16LE').b, headers: {
402
+ 'Content-Type' => 'text/html'
403
+ })
404
+
405
+ page = MetaInspector.new(url, encoding: 'UTF-16LE')
406
+
407
+ expect(page.title).to eq('Hello 日本語')
408
+ expect(page.links.raw).to eq(['/after'])
409
+ end
410
+
411
+ it 'normalizes a gzip-compressed UTF-16LE response after decompression' do
412
+ body = html.encode('UTF-16LE').b
413
+ stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
414
+ 'Content-Type' => 'text/html; charset=UTF-16LE', 'Content-Encoding' => 'gzip'
415
+ })
416
+
417
+ page = MetaInspector.new(url)
418
+
419
+ expect(page.title).to eq('Hello 日本語')
420
+ expect(page.links.raw).to eq(['/after'])
421
+ expect(page.response.body.b).to eq(body)
422
+ end
423
+
424
+ it 'replaces invalid UTF-16LE bytes' do
425
+ body = html.encode('UTF-16LE').b + "\x00".b
426
+ stub_request(:get, url).to_return(body: body, headers: {
427
+ 'Content-Type' => 'text/html; charset=UTF-16LE'
428
+ })
429
+
430
+ page = MetaInspector.new(url)
431
+
432
+ expect(page.links.raw).to eq(['/after'])
433
+ expect(page.to_s).to end_with("\u{FFFD}")
434
+ end
435
+
436
+ it 'wraps unsupported UTF-7 conversion in RequestError' do
437
+ stub_request(:get, url).to_return(body: '<title>Hello</title>', headers: {
438
+ 'Content-Type' => 'text/html; charset=UTF-7'
439
+ })
440
+
441
+ expect { MetaInspector.new(url) }.to raise_error(MetaInspector::RequestError) { |error|
442
+ expect(error.cause).to be_a(Encoding::ConverterNotFoundError)
443
+ expect(error.message).to eq(error.cause.message)
444
+ }
445
+ end
446
+ end
447
+
448
+ RSpec.describe 'invalid bytes in a fetched page' do
449
+ let(:url) { 'https://encoding.example/page' }
450
+ let(:body) do
451
+ "<html><head><title>caf\xC3\xA9</title></head>" \
452
+ "<body><p>one bad byte: \xE9</p><a href='/a'>a</a></body></html>".b
453
+ end
454
+
455
+ it 'replaces invalid bytes when the charset comes from the Content-Type header' do
456
+ stub_request(:get, url).to_return(body: body, headers: { 'Content-Type' => 'text/html; charset=utf-8' })
457
+
458
+ page = MetaInspector.new(url)
459
+
460
+ expect(page.title).to eq('café')
461
+ expect(page.links.raw).to eq(['/a'])
462
+ expect(page.parsed.at('p').text).to eq("one bad byte: \u{FFFD}")
463
+ expect(page.to_s).to be_valid_encoding
464
+ expect(page.response.body.b).to eq(body)
465
+ end
466
+
467
+ it 'replaces invalid bytes in a compressed page' do
468
+ stub_request(:get, url).to_return(body: Zlib.gzip(body), headers: {
469
+ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Encoding' => 'gzip'
470
+ })
471
+
472
+ page = MetaInspector.new(url)
473
+
474
+ expect(page.title).to eq('café')
475
+ expect(page.links.raw).to eq(['/a'])
476
+ end
477
+
478
+ it 'still lets a forced encoding override a wrong HTTP charset' do
479
+ stub_request(:get, url).to_return(body: "<title>caf\xE9</title>".b, headers: {
480
+ 'Content-Type' => 'text/html; charset=utf-8'
481
+ })
482
+
483
+ page = MetaInspector.new(url, encoding: 'Windows-1252')
484
+
485
+ expect(page.title).to eq('café')
486
+ end
487
+ end
@@ -32,7 +32,8 @@ RSpec.describe 'outbound URL policy' do
32
32
  expect(a_request(:get, 'https://internal.example/page')).not_to have_been_made
33
33
  end
34
34
 
35
- [RuntimeError, Faraday::TimeoutError, Faraday::ConnectionFailed, URI::InvalidURIError, Timeout::Error].each do |error_class|
35
+ [RuntimeError, Faraday::TimeoutError, Faraday::ConnectionFailed, URI::InvalidURIError,
36
+ URI::InvalidComponentError, Zlib::DataError, Timeout::Error].each do |error_class|
36
37
  [:page, :image].each do |target|
37
38
  it "preserves a filter's #{error_class} on a #{target}" do
38
39
  error = error_class.new('policy failed')
data/spec/url_spec.rb CHANGED
@@ -76,6 +76,43 @@ describe MetaInspector::URL do
76
76
  expect(url.url).to eq('http://example.com/foo')
77
77
  end
78
78
 
79
+ [
80
+ 'http://example.com/foo?b=2&a=1',
81
+ 'http://example.com/foo?tag=x&tag=y',
82
+ 'http://example.com/foo?q=a+b'
83
+ ].each do |untracked|
84
+ it "should leave the query of an untracked url unchanged (#{untracked})" do
85
+ expect(MetaInspector::URL.new(untracked).untracked_url).to eq(untracked)
86
+ end
87
+ end
88
+
89
+ it "should keep the order, repeated keys and encoding of non-tracking parameters" do
90
+ url = MetaInspector::URL.new('http://example.com/foo?tag=x&utm_source=news&tag=y&q=a+b&a=1')
91
+ expect(url.untracked_url).to eq('http://example.com/foo?tag=x&tag=y&q=a+b&a=1')
92
+ end
93
+
94
+ it "should remove every occurrence of a repeated tracking parameter" do
95
+ url = MetaInspector::URL.new('http://example.com/foo?utm_source=a&x=1&utm_source=b')
96
+ expect(url.untracked_url).to eq('http://example.com/foo?x=1')
97
+ end
98
+
99
+ it "should keep the fragment when removing tracking parameters" do
100
+ url = MetaInspector::URL.new('http://example.com/foo?a=1&utm_source=x#top')
101
+ expect(url.untracked_url).to eq('http://example.com/foo?a=1#top')
102
+ end
103
+
104
+ it "should remove percent-encoded tracking parameters" do
105
+ url = MetaInspector::URL.new('http://example.com/foo?a=1&utm%5Fsource=x', normalize: false)
106
+ expect(url.tracked?).to be true
107
+ expect(url.untracked_url).to eq('http://example.com/foo?a=1')
108
+ end
109
+
110
+ it "should keep the order and repeated keys of non-tracking parameters when untracking" do
111
+ url = MetaInspector::URL.new('http://example.com/foo?b=2&tag=x&utm_campaign=sale&tag=y&a=1')
112
+ url.untrack!
113
+ expect(url.url).to eq('http://example.com/foo?b=2&tag=x&tag=y&a=1')
114
+ end
115
+
79
116
  it "should say if the url is tracked" do
80
117
  expect(MetaInspector::URL.new('http://example.com/foo?not_utm_thing=bar&utm_source=1234').tracked?).to be true
81
118
  expect(MetaInspector::URL.new('http://example.com/foo?not_utm_thing=bar&utm_medium=1234').tracked?).to be true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metainspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.21.2
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Iniesta
@@ -308,8 +308,10 @@ files:
308
308
  - examples/responsive_images.rb
309
309
  - examples/spider.rb
310
310
  - lib/meta_inspector.rb
311
+ - lib/meta_inspector/attempt_timeout.rb
311
312
  - lib/meta_inspector/document.rb
312
313
  - lib/meta_inspector/errors.rb
314
+ - lib/meta_inspector/html_encoding.rb
313
315
  - lib/meta_inspector/http_session.rb
314
316
  - lib/meta_inspector/image_downloader.rb
315
317
  - lib/meta_inspector/limited_response.rb
@@ -400,6 +402,7 @@ files:
400
402
  - spec/image_metadata_spec.rb
401
403
  - spec/image_validity_spec.rb
402
404
  - spec/inline_images_spec.rb
405
+ - spec/meta_inspector/content_type_spec.rb
403
406
  - spec/meta_inspector/head_first_spec.rb
404
407
  - spec/meta_inspector/head_links_spec.rb
405
408
  - spec/meta_inspector/iframes_spec.rb
@@ -409,9 +412,14 @@ files:
409
412
  - spec/meta_inspector/meta_tags_spec.rb
410
413
  - spec/meta_inspector/redirections_spec.rb
411
414
  - spec/meta_inspector/texts_spec.rb
415
+ - spec/meta_inspector/timeouts_spec.rb
416
+ - spec/noscript_spec.rb
412
417
  - spec/parser_spec.rb
418
+ - spec/query_string_spec.rb
413
419
  - spec/redirect_headers_spec.rb
420
+ - spec/request_errors_spec.rb
414
421
  - spec/request_spec.rb
422
+ - spec/response_encoding_spec.rb
415
423
  - spec/response_size_spec.rb
416
424
  - spec/spec_helper.rb
417
425
  - spec/unicode_image_urls_spec.rb
@@ -435,7 +443,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
435
443
  - !ruby/object:Gem::Version
436
444
  version: '0'
437
445
  requirements: []
438
- rubygems_version: 4.0.15
446
+ rubygems_version: 4.0.3
439
447
  specification_version: 4
440
448
  summary: MetaInspector is a ruby gem for web scraping purposes, that returns metadata
441
449
  from a given URL