metainspector 5.20.0 → 5.21.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile +1 -1
- data/README.md +147 -6
- data/examples/basic_scraping.rb +4 -3
- data/examples/faraday_redirect_options.rb +23 -31
- data/examples/html_size_limit.rb +17 -0
- data/examples/iframes.rb +15 -0
- data/examples/image_details.rb +17 -0
- data/examples/image_validation.rb +25 -0
- data/examples/link_checker.rb +2 -2
- data/examples/responsive_images.rb +15 -0
- data/examples/spider.rb +2 -2
- data/lib/meta_inspector/document.rb +14 -1
- data/lib/meta_inspector/http_session.rb +11 -1
- data/lib/meta_inspector/image_downloader.rb +48 -3
- data/lib/meta_inspector/parser.rb +20 -1
- data/lib/meta_inspector/parsers/head_links.rb +18 -3
- data/lib/meta_inspector/parsers/iframes.rb +21 -0
- data/lib/meta_inspector/parsers/images.rb +194 -26
- data/lib/meta_inspector/parsers/texts.rb +2 -2
- data/lib/meta_inspector/url_policy.rb +12 -2
- data/lib/meta_inspector/version.rb +1 -1
- data/lib/meta_inspector.rb +1 -0
- data/meta_inspector.gemspec +1 -2
- data/spec/document_spec.rb +139 -0
- data/spec/examples_spec.rb +206 -0
- data/spec/image_download_spec.rb +11 -0
- data/spec/image_metadata_spec.rb +110 -0
- data/spec/image_validity_spec.rb +195 -0
- data/spec/inline_images_spec.rb +75 -0
- data/spec/meta_inspector/head_links_spec.rb +69 -0
- data/spec/meta_inspector/iframes_spec.rb +50 -0
- data/spec/meta_inspector/images_spec.rb +130 -0
- data/spec/meta_inspector/texts_spec.rb +50 -0
- data/spec/parser_spec.rb +10 -0
- data/spec/unicode_image_urls_spec.rb +74 -0
- data/spec/url_filter_spec.rb +1 -1
- metadata +27 -36
- data/examples/redirect_web_server.rb +0 -5
|
@@ -47,4 +47,73 @@ describe MetaInspector do
|
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
describe "rel token matching" do
|
|
51
|
+
{ feeds: 'alternate', stylesheets: 'stylesheet', canonicals: 'canonical' }.each do |method, keyword|
|
|
52
|
+
describe "##{method}" do
|
|
53
|
+
let(:document) do
|
|
54
|
+
%(<html><head><link #{rel_attribute} type="application/rss+xml" title="Example" href="/resource"></head></html>)
|
|
55
|
+
end
|
|
56
|
+
let(:page) { MetaInspector.new('https://example.com/page', document: document) }
|
|
57
|
+
let(:expected_link) do
|
|
58
|
+
attributes = { title: 'Example', href: 'https://example.com/resource', type: 'application/rss+xml' }
|
|
59
|
+
attributes[:rel] = rel unless method == :feeds
|
|
60
|
+
attributes
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
[keyword, "#{keyword} nofollow", "nofollow #{keyword}", keyword.upcase,
|
|
64
|
+
"nofollow #{keyword.capitalize}", " #{keyword} nofollow ",
|
|
65
|
+
"#{keyword} #{keyword}", "nofollow\t#{keyword}", "nofollow\n#{keyword}",
|
|
66
|
+
"nofollow\f#{keyword}", "nofollow\r#{keyword}"].each do |value|
|
|
67
|
+
context "with rel=#{value.inspect}" do
|
|
68
|
+
let(:rel) { value }
|
|
69
|
+
let(:rel_attribute) { %(rel="#{rel}") }
|
|
70
|
+
|
|
71
|
+
it "matches the complete token and preserves the link attributes" do
|
|
72
|
+
expect(page.public_send(method)).to eq([expected_link])
|
|
73
|
+
expect(page.head_links.first[:rel]).to eq(rel)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
[nil, '', 'nofollow', "not-#{keyword}", "#{keyword}-extra",
|
|
79
|
+
"nofollow,#{keyword}", "nofollow #{keyword}", "nofollow#{keyword}"].each do |value|
|
|
80
|
+
context "with rel=#{value.inspect}" do
|
|
81
|
+
let(:rel_attribute) { value.nil? ? '' : %(rel="#{value}") }
|
|
82
|
+
|
|
83
|
+
it "does not match a missing or incomplete token" do
|
|
84
|
+
expect(page.public_send(method)).to eq([])
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
describe "feeds with combined rel tokens" do
|
|
93
|
+
let(:page) { MetaInspector.new('https://example.com/page', document: document) }
|
|
94
|
+
let(:document) do
|
|
95
|
+
<<~HTML
|
|
96
|
+
<html><head>
|
|
97
|
+
<base href="https://feeds.example.com/blog/">
|
|
98
|
+
<link rel="alternate nofollow" type="text/html" href="page">
|
|
99
|
+
<link rel="alternate nofollow" href="missing-type">
|
|
100
|
+
<link rel="alternate nofollow" type="application/rss+xml">
|
|
101
|
+
<link rel="alternate nofollow" type="application/rss+xml" href="">
|
|
102
|
+
<link rel="alternate nofollow" type="application/rss+xml" href=" \t ">
|
|
103
|
+
<link rel="alternate nofollow" type="application/rss+xml" title="RSS" href="feed.rss">
|
|
104
|
+
<link rel="nofollow ALTERNATE" type="application/atom+xml" href="feed.atom">
|
|
105
|
+
<link rel="AlTeRnAtE nofollow" type="application/json" href="feed.json">
|
|
106
|
+
</head></html>
|
|
107
|
+
HTML
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it "keeps supported feeds in document order, skips invalid feeds and resolves the base URL" do
|
|
111
|
+
expect(page.feeds).to eq([
|
|
112
|
+
{ title: 'RSS', href: 'https://feeds.example.com/blog/feed.rss', type: 'application/rss+xml' },
|
|
113
|
+
{ title: nil, href: 'https://feeds.example.com/blog/feed.atom', type: 'application/atom+xml' },
|
|
114
|
+
{ title: nil, href: 'https://feeds.example.com/blog/feed.json', type: 'application/json' }
|
|
115
|
+
])
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
50
119
|
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe MetaInspector do
|
|
4
|
+
def page_with_iframes(html, url = 'https://example.com/articles/page')
|
|
5
|
+
MetaInspector.new(url, document: html)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it 'returns an array of original iframe attributes in document order' do
|
|
9
|
+
page = page_with_iframes('<iframe src=" /embed " title="First" allowfullscreen data-id="1"></iframe>' \
|
|
10
|
+
'<iframe src=" /embed "></iframe><iframe srcdoc="<p>Hi</p>"></iframe>')
|
|
11
|
+
|
|
12
|
+
expect(page.iframes).to eq([
|
|
13
|
+
{ src: ' /embed ', title: 'First', allowfullscreen: '', 'data-id': '1' },
|
|
14
|
+
{ src: ' /embed ' },
|
|
15
|
+
{ srcdoc: '<p>Hi</p>' }
|
|
16
|
+
])
|
|
17
|
+
expect(page.iframes).to be_an(Array)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'resolves sources against the base URL, preserving positions and duplicates' do
|
|
21
|
+
html = '<base href="/assets/"><iframe src=" embed?b=2&a=1 "></iframe>' \
|
|
22
|
+
'<iframe src="//player.example.com/video"></iframe><iframe src="embed?b=2&a=1"></iframe>' \
|
|
23
|
+
'<iframe></iframe><iframe src=" "></iframe><iframe src="http://a:bad/"></iframe>'
|
|
24
|
+
page = page_with_iframes(html)
|
|
25
|
+
|
|
26
|
+
expect(page.iframe_sources).to eq([
|
|
27
|
+
'https://example.com/assets/embed?b=2&a=1',
|
|
28
|
+
'https://player.example.com/video',
|
|
29
|
+
'https://example.com/assets/embed?b=2&a=1',
|
|
30
|
+
nil, nil, nil
|
|
31
|
+
])
|
|
32
|
+
expect(page.iframes.last[:src]).to eq('http://a:bad/')
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'returns empty arrays when there are no iframes' do
|
|
36
|
+
page = page_with_iframes('<p>No iframes</p>')
|
|
37
|
+
expect(page.iframes).to eq([])
|
|
38
|
+
expect(page.iframe_sources).to eq([])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'includes iframe attributes in to_hash' do
|
|
42
|
+
stub_request(:get, 'http://example.com/').to_return(
|
|
43
|
+
body: '<iframe src="/embed" width="560"></iframe>',
|
|
44
|
+
headers: { 'Content-Type' => 'text/html' }
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
expect(MetaInspector.new('http://example.com/').to_hash['iframes'])
|
|
48
|
+
.to eq([{ src: '/embed', width: '560' }])
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -2,6 +2,93 @@ require 'spec_helper'
|
|
|
2
2
|
|
|
3
3
|
describe MetaInspector do
|
|
4
4
|
describe "#images" do
|
|
5
|
+
it 'preserves fallback dimensions when an earlier picture source repeats its URL' do
|
|
6
|
+
html = '<picture><source srcset="/same.jpg"><img src="/same.jpg" width="200" height="100"></picture>'
|
|
7
|
+
page = MetaInspector.new('http://example.com/', document: html, download_images: false)
|
|
8
|
+
|
|
9
|
+
expect(page.images.with_size).to eq([['http://example.com/same.jpg', 200, 100]])
|
|
10
|
+
expect(page.images.best).to eq('http://example.com/same.jpg')
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'does not download inline srcset images or spend the download budget on them' do
|
|
14
|
+
html = '<img src="/fallback.jpg" width="20" height="20" srcset="data:image/png;base64,AAAA 1x, /100x100 2x">'
|
|
15
|
+
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
|
|
16
|
+
|
|
17
|
+
expect(page.images.to_a).to include('data:image/png;base64')
|
|
18
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
19
|
+
expect(WebMock).to have_requested(:get, 'http://example.com/100x100').once
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'skips non-HTTP responsive candidates when selecting the best image' do
|
|
23
|
+
html = '<picture><source srcset="blob:https://example.com/123 1x"><img src="/fallback.jpg" width="100" height="100"></picture>'
|
|
24
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
25
|
+
|
|
26
|
+
expect(page.images.to_a).to include('blob:https://example.com/123')
|
|
27
|
+
expect(page.images.best).to eq('http://example.com/fallback.jpg')
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'accepts positive width descriptors with leading zeroes' do
|
|
31
|
+
html = '<img srcset="/small.jpg 0400w, /large.jpg 0800w, /invalid.jpg 000w">'
|
|
32
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
33
|
+
|
|
34
|
+
expect(page.images.to_a).to eq(['http://example.com/small.jpg', 'http://example.com/large.jpg'])
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'collects responsive candidates in document order and resolves duplicates' do
|
|
38
|
+
html = <<~HTML
|
|
39
|
+
<base href="/assets/">
|
|
40
|
+
<picture><source srcset="small.webp 400w, large.webp 800w">
|
|
41
|
+
<img src="fallback.jpg" srcset="fallback.jpg 1x, large.jpg 2x">
|
|
42
|
+
</picture>
|
|
43
|
+
<img srcset="large.jpg 1x, other.jpg 2x">
|
|
44
|
+
<source srcset="outside.jpg 1x">
|
|
45
|
+
HTML
|
|
46
|
+
page = MetaInspector.new('http://example.com/page', document: html)
|
|
47
|
+
|
|
48
|
+
expect(page.images.to_a).to eq(%w[
|
|
49
|
+
http://example.com/assets/small.webp http://example.com/assets/large.webp
|
|
50
|
+
http://example.com/assets/fallback.jpg http://example.com/assets/large.jpg
|
|
51
|
+
http://example.com/assets/other.jpg
|
|
52
|
+
])
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'ranks responsive candidates by downloaded dimensions without assigning fallback dimensions to them' do
|
|
56
|
+
html = '<img src="/10x10" width="10" height="10" srcset="/100x100 2x">'
|
|
57
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
58
|
+
|
|
59
|
+
expect(page.images.with_size).to eq([
|
|
60
|
+
['http://example.com/100x100', 100, 100],
|
|
61
|
+
['http://example.com/10x10', 10, 10]
|
|
62
|
+
])
|
|
63
|
+
expect(page.images.best).to eq('http://example.com/100x100')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it 'keeps commas within candidate URLs and skips invalid descriptors' do
|
|
67
|
+
html = '<img srcset="/image?a=1,2 1x, /bad.jpg nope, /good.jpg 2x">'
|
|
68
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
69
|
+
|
|
70
|
+
expect(page.images.to_a).to eq(['http://example.com/image?a=1,2', 'http://example.com/good.jpg'])
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'does not treat commas inside invalid parenthesized descriptors as candidate separators' do
|
|
74
|
+
html = '<img srcset="bad.jpg foo(bar,baz), good.jpg 1x">'
|
|
75
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
76
|
+
|
|
77
|
+
expect(page.images.to_a).to eq(['http://example.com/good.jpg'])
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'applies the download limit to responsive candidates in document order' do
|
|
81
|
+
html = '<picture><source srcset="/10x10 1x, /100x100 2x"><img src="/fallback.jpg" width="20" height="20"></picture>'
|
|
82
|
+
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
|
|
83
|
+
|
|
84
|
+
expect(page.images.with_size).to eq([
|
|
85
|
+
['http://example.com/fallback.jpg', 20, 20],
|
|
86
|
+
['http://example.com/10x10', 10, 10],
|
|
87
|
+
['http://example.com/100x100', 0, 0]
|
|
88
|
+
])
|
|
89
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/100x100')
|
|
90
|
+
end
|
|
91
|
+
|
|
5
92
|
describe "returns an Enumerable" do
|
|
6
93
|
let(:page) { MetaInspector.new('https://twitter.com/markupvalidator') }
|
|
7
94
|
|
|
@@ -426,6 +513,49 @@ describe MetaInspector do
|
|
|
426
513
|
end
|
|
427
514
|
|
|
428
515
|
describe '#favicon' do
|
|
516
|
+
['alternate icon', 'icon alternate', 'alternate icon stylesheet', 'ICON', 'AlTeRnAtE IcOn'].each do |rel|
|
|
517
|
+
it "recognizes the icon token in #{rel.inspect}" do
|
|
518
|
+
html = %(<html><head><link rel="#{rel}" href="/favicon.png"></head></html>)
|
|
519
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
520
|
+
|
|
521
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/favicon.png')
|
|
522
|
+
end
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
[" ", "\t", "\n", "\f", "\r"].each do |whitespace|
|
|
526
|
+
it "splits rel tokens on ASCII whitespace #{whitespace.inspect}" do
|
|
527
|
+
html = %(<html><head><link rel="#{whitespace}alternate#{whitespace}ICON#{whitespace}" href="/favicon.png"></head></html>)
|
|
528
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
529
|
+
|
|
530
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/favicon.png')
|
|
531
|
+
end
|
|
532
|
+
end
|
|
533
|
+
|
|
534
|
+
['apple-touch-icon', 'mask-icon', 'iconic', 'not-shortcut', 'shortcut-other',
|
|
535
|
+
"alternate\u00a0icon", "alternate\vicon", '', nil].each do |rel|
|
|
536
|
+
it "does not treat #{rel.inspect} as a favicon relationship" do
|
|
537
|
+
attribute = rel.nil? ? '' : %(rel="#{rel}")
|
|
538
|
+
html = %(<html><head><link #{attribute} href="/other.png"></head></html>)
|
|
539
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
540
|
+
|
|
541
|
+
expect(page.images.favicon).to be_nil
|
|
542
|
+
end
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
it 'uses the first matching link in document order' do
|
|
546
|
+
html = <<~HTML
|
|
547
|
+
<html><head>
|
|
548
|
+
<link rel="apple-touch-icon" href="/touch.png">
|
|
549
|
+
<link rel="not-shortcut" href="/other.png">
|
|
550
|
+
<link rel="alternate ICON" href="/first.png">
|
|
551
|
+
<link rel="icon" href="/second.png">
|
|
552
|
+
</head></html>
|
|
553
|
+
HTML
|
|
554
|
+
page = MetaInspector.new('https://githubuniverse.com/', document: html)
|
|
555
|
+
|
|
556
|
+
expect(page.images.favicon).to eq('https://githubuniverse.com/first.png')
|
|
557
|
+
end
|
|
558
|
+
|
|
429
559
|
it "should get favicon link when marked as icon" do
|
|
430
560
|
page = MetaInspector.new('http://pagerankalert.com/')
|
|
431
561
|
|
|
@@ -287,6 +287,56 @@ describe MetaInspector do
|
|
|
287
287
|
expect(page.best_description).to eq("the twitter description")
|
|
288
288
|
end
|
|
289
289
|
|
|
290
|
+
['', ' ', "\n\t "].each do |blank|
|
|
291
|
+
it "should skip a blank standard description (#{blank.inspect})" do
|
|
292
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="Useful Open Graph description"></head></html>)
|
|
293
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
294
|
+
|
|
295
|
+
expect(page.best_description).to eq('Useful Open Graph description')
|
|
296
|
+
expect(page.description).to eq(blank)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
it "should skip a blank Open Graph description (#{blank.inspect})" do
|
|
300
|
+
html = %(<html><head><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="Useful Twitter description"></head></html>)
|
|
301
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
302
|
+
|
|
303
|
+
expect(page.best_description).to eq('Useful Twitter description')
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
it "should skip a blank Twitter description (#{blank.inspect})" do
|
|
307
|
+
paragraph = 'This paragraph provides a useful description when the Twitter description is blank and no other metadata descriptions exist.'
|
|
308
|
+
html = %(<html><head><meta name="twitter:description" content="#{blank}"></head><body><p>#{paragraph}</p></body></html>)
|
|
309
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
310
|
+
|
|
311
|
+
expect(page.best_description).to eq(paragraph)
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
it "should use a paragraph when all metadata descriptions are blank (#{blank.inspect})" do
|
|
315
|
+
paragraph = 'This paragraph provides a useful description when the standard, Open Graph, and Twitter metadata descriptions are all blank.'
|
|
316
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="#{blank}"></head><body><p>#{paragraph}</p></body></html>)
|
|
317
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
318
|
+
|
|
319
|
+
expect(page.best_description).to eq(paragraph)
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
it "should return nil when all metadata descriptions are blank and there is no paragraph (#{blank.inspect})" do
|
|
323
|
+
html = %(<html><head><meta name="description" content="#{blank}"><meta property="og:description" content="#{blank}"><meta name="twitter:description" content="#{blank}"></head></html>)
|
|
324
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
325
|
+
|
|
326
|
+
expect(page.best_description).to be_nil
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
['description', 'og:description', 'twitter:description'].each do |name|
|
|
331
|
+
it "should preserve whitespace in a useful #{name} value" do
|
|
332
|
+
description = " \n Useful description\t "
|
|
333
|
+
html = %(<html><head><meta name="#{name}" content="#{description}"></head></html>)
|
|
334
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
335
|
+
|
|
336
|
+
expect(page.best_description).to eq(description)
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
290
340
|
it "should return the secondary description if no meta tag is present" do
|
|
291
341
|
page = MetaInspector.new('http://theonion-no-description.com')
|
|
292
342
|
|
data/spec/parser_spec.rb
CHANGED
|
@@ -4,6 +4,16 @@ describe MetaInspector::Parser do
|
|
|
4
4
|
let(:doc) { MetaInspector::Document.new('http://pagerankalert.com') }
|
|
5
5
|
let(:parser) { MetaInspector::Parser.new(doc) }
|
|
6
6
|
|
|
7
|
+
it 'returns the first feed URL through the deprecated feed method' do
|
|
8
|
+
expect { expect(parser.feed).to eq('http://feeds.feedburner.com/PageRankAlert') }
|
|
9
|
+
.to output(/DEPRECATION.*page\.feed.*6\.0.*page\.feeds/).to_stderr
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'returns the favicon URL through the deprecated favicon method' do
|
|
13
|
+
expect { expect(parser.favicon).to eq('http://pagerankalert.com/src/favicon.ico') }
|
|
14
|
+
.to output(/DEPRECATION.*page\.favicon.*6\.0.*page\.images\.favicon/).to_stderr
|
|
15
|
+
end
|
|
16
|
+
|
|
7
17
|
it "should have a Nokogiri::HTML::Document as parsed" do
|
|
8
18
|
expect(parser.parsed.class).to eq(Nokogiri::HTML::Document)
|
|
9
19
|
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe 'Unicode image URLs' do
|
|
4
|
+
let(:original) { 'https://example.com/ñ.png' }
|
|
5
|
+
let(:encoded) { 'https://example.com/%C3%B1.png' }
|
|
6
|
+
let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
|
|
7
|
+
|
|
8
|
+
before { stub_request(:get, encoded).to_return(body: png) }
|
|
9
|
+
|
|
10
|
+
['src', 'srcset'].each do |attribute|
|
|
11
|
+
it "downloads a Unicode #{attribute} image while preserving its original URL" do
|
|
12
|
+
page = MetaInspector.new('https://example.com/', document: %(<img #{attribute}="#{original}">))
|
|
13
|
+
|
|
14
|
+
expect(page.images.with_size).to eq([[original, 37, 29]])
|
|
15
|
+
expect(a_request(:get, encoded)).to have_been_made.once
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'validates Unicode image and suggested URLs without rejecting them' do
|
|
20
|
+
html = %(<meta property="og:image" content="#{original}"><img src="#{original}">)
|
|
21
|
+
page = MetaInspector.new('https://example.com/', document: html,
|
|
22
|
+
download_image_options: { reject_invalid_images: true }, fetch_all_image_meta: true)
|
|
23
|
+
|
|
24
|
+
expect(page.images.owner_suggested).to eq(original)
|
|
25
|
+
expect(page.images.to_a).to eq([original])
|
|
26
|
+
expect(page.images.with_metadata).to eq([{ url: original, valid: true }])
|
|
27
|
+
expect(a_request(:get, encoded)).to have_been_made.once
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'applies URL filters to the encoded URL before making a request' do
|
|
31
|
+
seen = []
|
|
32
|
+
page = MetaInspector.new('https://example.com/', document: %(<img srcset="#{original} 1x">),
|
|
33
|
+
fetch_all_image_meta: true, url_filter: ->(uri) { seen << uri.to_s; false })
|
|
34
|
+
|
|
35
|
+
expect { page.images.with_metadata }.to raise_error(MetaInspector::RequestError, /blocked by url_filter/)
|
|
36
|
+
expect(seen).to eq([encoded])
|
|
37
|
+
expect(a_request(:get, encoded)).not_to have_been_made
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it 'still applies URL filters to redirects from Unicode image URLs' do
|
|
41
|
+
target = 'https://other.example/image.png'
|
|
42
|
+
stub_request(:get, encoded).to_return(status: 302, headers: { 'Location' => target })
|
|
43
|
+
stub_request(:get, target).to_return(body: png)
|
|
44
|
+
page = MetaInspector.new('https://example.com/', document: %(<img src="#{original}">),
|
|
45
|
+
url_filter: ->(uri) { uri.hostname == 'example.com' })
|
|
46
|
+
|
|
47
|
+
expect { page.images.with_size }.to raise_error(MetaInspector::RequestError, /blocked by url_filter/)
|
|
48
|
+
expect(a_request(:get, encoded)).to have_been_made.once
|
|
49
|
+
expect(a_request(:get, target)).not_to have_been_made
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
{
|
|
53
|
+
'an invalid UTF-8 host' => 'https://%C3/ñ.png',
|
|
54
|
+
'an oversized Unicode host label' => "https://#{'ñ' * 300}.com/a.png"
|
|
55
|
+
}.each do |description, malformed|
|
|
56
|
+
context "with #{description}" do
|
|
57
|
+
it 'skips a srcset candidate that cannot be normalized' do
|
|
58
|
+
page = MetaInspector.new('https://example.com/', document: %(<img srcset="#{malformed} 1x">))
|
|
59
|
+
|
|
60
|
+
expect(page.images.with_size).to eq([[malformed, 0, 0]])
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'rejects images that cannot be normalized when validating' do
|
|
64
|
+
html = %(<meta property="og:image" content="#{malformed}"><img src="#{malformed}">)
|
|
65
|
+
page = MetaInspector.new('https://example.com/', document: html,
|
|
66
|
+
download_image_options: { reject_invalid_images: true }, fetch_all_image_meta: true)
|
|
67
|
+
|
|
68
|
+
expect(page.images.owner_suggested).to be_nil
|
|
69
|
+
expect(page.images.to_a).to eq([])
|
|
70
|
+
expect(page.images.with_metadata).to eq([{ url: malformed, valid: false }])
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
data/spec/url_filter_spec.rb
CHANGED
|
@@ -124,7 +124,7 @@ RSpec.describe 'outbound URL policy' do
|
|
|
124
124
|
it "rejects non-HTTP network URL #{other}" do
|
|
125
125
|
expect { MetaInspector.new(other) }.to raise_error(MetaInspector::RequestError)
|
|
126
126
|
page = MetaInspector.new(url, document: %(<img src="#{other}">))
|
|
127
|
-
expect
|
|
127
|
+
expect(page.images.with_size.first.drop(1)).to eq([0, 0])
|
|
128
128
|
end
|
|
129
129
|
end
|
|
130
130
|
|
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.
|
|
4
|
+
version: 5.21.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jaime Iniesta
|
|
@@ -153,6 +153,20 @@ dependencies:
|
|
|
153
153
|
- - "~>"
|
|
154
154
|
- !ruby/object:Gem::Version
|
|
155
155
|
version: 2.9.1
|
|
156
|
+
- !ruby/object:Gem::Dependency
|
|
157
|
+
name: base64
|
|
158
|
+
requirement: !ruby/object:Gem::Requirement
|
|
159
|
+
requirements:
|
|
160
|
+
- - "~>"
|
|
161
|
+
- !ruby/object:Gem::Version
|
|
162
|
+
version: '0.3'
|
|
163
|
+
type: :runtime
|
|
164
|
+
prerelease: false
|
|
165
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
166
|
+
requirements:
|
|
167
|
+
- - "~>"
|
|
168
|
+
- !ruby/object:Gem::Version
|
|
169
|
+
version: '0.3'
|
|
156
170
|
- !ruby/object:Gem::Dependency
|
|
157
171
|
name: fastimage
|
|
158
172
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -237,26 +251,6 @@ dependencies:
|
|
|
237
251
|
- - "~>"
|
|
238
252
|
- !ruby/object:Gem::Version
|
|
239
253
|
version: '0.14'
|
|
240
|
-
- !ruby/object:Gem::Dependency
|
|
241
|
-
name: puma
|
|
242
|
-
requirement: !ruby/object:Gem::Requirement
|
|
243
|
-
requirements:
|
|
244
|
-
- - "~>"
|
|
245
|
-
- !ruby/object:Gem::Version
|
|
246
|
-
version: '8.0'
|
|
247
|
-
- - ">="
|
|
248
|
-
- !ruby/object:Gem::Version
|
|
249
|
-
version: 8.0.2
|
|
250
|
-
type: :development
|
|
251
|
-
prerelease: false
|
|
252
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
253
|
-
requirements:
|
|
254
|
-
- - "~>"
|
|
255
|
-
- !ruby/object:Gem::Version
|
|
256
|
-
version: '8.0'
|
|
257
|
-
- - ">="
|
|
258
|
-
- !ruby/object:Gem::Version
|
|
259
|
-
version: 8.0.2
|
|
260
254
|
- !ruby/object:Gem::Dependency
|
|
261
255
|
name: rubocop
|
|
262
256
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -285,20 +279,6 @@ dependencies:
|
|
|
285
279
|
- - "~>"
|
|
286
280
|
- !ruby/object:Gem::Version
|
|
287
281
|
version: '0.8'
|
|
288
|
-
- !ruby/object:Gem::Dependency
|
|
289
|
-
name: sinatra
|
|
290
|
-
requirement: !ruby/object:Gem::Requirement
|
|
291
|
-
requirements:
|
|
292
|
-
- - "~>"
|
|
293
|
-
- !ruby/object:Gem::Version
|
|
294
|
-
version: '4.2'
|
|
295
|
-
type: :development
|
|
296
|
-
prerelease: false
|
|
297
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
298
|
-
requirements:
|
|
299
|
-
- - "~>"
|
|
300
|
-
- !ruby/object:Gem::Version
|
|
301
|
-
version: '4.2'
|
|
302
282
|
description: MetaInspector lets you scrape a web page and get its links, images, texts,
|
|
303
283
|
meta tags...
|
|
304
284
|
email: jaimeiniesta@gmail.com
|
|
@@ -320,8 +300,12 @@ files:
|
|
|
320
300
|
- bin/console
|
|
321
301
|
- examples/basic_scraping.rb
|
|
322
302
|
- examples/faraday_redirect_options.rb
|
|
303
|
+
- examples/html_size_limit.rb
|
|
304
|
+
- examples/iframes.rb
|
|
305
|
+
- examples/image_details.rb
|
|
306
|
+
- examples/image_validation.rb
|
|
323
307
|
- examples/link_checker.rb
|
|
324
|
-
- examples/
|
|
308
|
+
- examples/responsive_images.rb
|
|
325
309
|
- examples/spider.rb
|
|
326
310
|
- lib/meta_inspector.rb
|
|
327
311
|
- lib/meta_inspector/document.rb
|
|
@@ -331,6 +315,7 @@ files:
|
|
|
331
315
|
- lib/meta_inspector/parser.rb
|
|
332
316
|
- lib/meta_inspector/parsers/base.rb
|
|
333
317
|
- lib/meta_inspector/parsers/head_links.rb
|
|
318
|
+
- lib/meta_inspector/parsers/iframes.rb
|
|
334
319
|
- lib/meta_inspector/parsers/images.rb
|
|
335
320
|
- lib/meta_inspector/parsers/links.rb
|
|
336
321
|
- lib/meta_inspector/parsers/meta_tags.rb
|
|
@@ -342,6 +327,7 @@ files:
|
|
|
342
327
|
- lib/metainspector.rb
|
|
343
328
|
- meta_inspector.gemspec
|
|
344
329
|
- spec/document_spec.rb
|
|
330
|
+
- spec/examples_spec.rb
|
|
345
331
|
- spec/fixtures/100x100.jpg.response
|
|
346
332
|
- spec/fixtures/10x10.jpg.response
|
|
347
333
|
- spec/fixtures/404.response
|
|
@@ -410,8 +396,12 @@ files:
|
|
|
410
396
|
- spec/fixtures/youtube.response
|
|
411
397
|
- spec/fixtures/youtube_short_title.response
|
|
412
398
|
- spec/image_download_spec.rb
|
|
399
|
+
- spec/image_metadata_spec.rb
|
|
400
|
+
- spec/image_validity_spec.rb
|
|
401
|
+
- spec/inline_images_spec.rb
|
|
413
402
|
- spec/meta_inspector/head_first_spec.rb
|
|
414
403
|
- spec/meta_inspector/head_links_spec.rb
|
|
404
|
+
- spec/meta_inspector/iframes_spec.rb
|
|
415
405
|
- spec/meta_inspector/images_spec.rb
|
|
416
406
|
- spec/meta_inspector/links_spec.rb
|
|
417
407
|
- spec/meta_inspector/meta_inspector_spec.rb
|
|
@@ -421,6 +411,7 @@ files:
|
|
|
421
411
|
- spec/parser_spec.rb
|
|
422
412
|
- spec/request_spec.rb
|
|
423
413
|
- spec/spec_helper.rb
|
|
414
|
+
- spec/unicode_image_urls_spec.rb
|
|
424
415
|
- spec/url_filter_spec.rb
|
|
425
416
|
- spec/url_spec.rb
|
|
426
417
|
homepage: https://github.com/jaimeiniesta/metainspector
|