metainspector 5.20.0 → 5.21.1
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 +20 -0
- data/Gemfile +1 -1
- data/README.md +157 -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 +15 -2
- 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 +23 -6
- 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/redirect_headers_spec.rb +143 -0
- data/spec/unicode_image_urls_spec.rb +74 -0
- data/spec/url_filter_spec.rb +1 -1
- metadata +29 -37
- data/examples/redirect_web_server.rb +0 -5
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe 'image validity' do
|
|
4
|
+
let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
|
|
5
|
+
let(:options) { { download_image_options: { reject_invalid_images: true } } }
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
stub_request(:get, 'http://example.com/missing').to_return(status: 404)
|
|
9
|
+
stub_request(:get, 'http://example.com/not-image').to_return(body: 'not an image')
|
|
10
|
+
stub_request(:get, 'http://example.com/failure').to_timeout
|
|
11
|
+
stub_request(:get, 'http://example.com/good').to_return(body: png)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'skips invalid suggested images and falls back to a valid suggested image' do
|
|
15
|
+
html = '<meta property="og:image" content="/missing"><meta name="twitter:image" content="/good">'
|
|
16
|
+
page = MetaInspector.new('http://example.com/', **options, document: html)
|
|
17
|
+
expect(page.images.owner_suggested).to eq('http://example.com/good')
|
|
18
|
+
expect(page.images.best).to eq('http://example.com/good')
|
|
19
|
+
expect(a_request(:get, 'http://example.com/good')).to have_been_made.once
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'filters invalid images even when HTML dimensions are present and falls back to a valid image' do
|
|
23
|
+
html = '<meta property="og:image" content="/missing"><img src="/not-image" width="100" height="100"><img src="/failure"><img src="/good" width="10" height="10">'
|
|
24
|
+
page = MetaInspector.new('http://example.com/', **options, document: html)
|
|
25
|
+
expect(page.images.to_a).to eq(['http://example.com/good'])
|
|
26
|
+
expect(page.images.with_size).to eq([['http://example.com/good', 10, 10]])
|
|
27
|
+
expect(page.images.best).to eq('http://example.com/good')
|
|
28
|
+
expect(a_request(:get, 'http://example.com/good')).to have_been_made.once
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'exposes validity for attempted images and nil for images beyond the limit' do
|
|
32
|
+
html = '<img src="/missing"><img src="/good" width="10" height="10"><img src="/not-image">'
|
|
33
|
+
page = MetaInspector.new('http://example.com/', document: html, fetch_all_image_meta: true, max_image_downloads: 2)
|
|
34
|
+
expect(page.images.with_metadata).to eq([
|
|
35
|
+
{ url: 'http://example.com/missing', valid: false },
|
|
36
|
+
{ url: 'http://example.com/good', valid: true },
|
|
37
|
+
{ url: 'http://example.com/not-image', valid: nil }
|
|
38
|
+
])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'does not reject or download unverified images when downloads are disabled or exhausted' do
|
|
42
|
+
html = '<meta property="og:image" content="/missing"><img src="/not-image" width="10" height="10">'
|
|
43
|
+
[ { download_images: false }, { max_image_downloads: 0 } ].each do |limits|
|
|
44
|
+
page = MetaInspector.new('http://example.com/', **options, **limits, document: html)
|
|
45
|
+
expect(page.images.best).to eq('http://example.com/missing')
|
|
46
|
+
expect(page.images.to_a).to eq(['http://example.com/not-image'])
|
|
47
|
+
expect(page.images.with_details).to eq([
|
|
48
|
+
{ url: 'http://example.com/not-image', width: 10, height: 10, type: nil, file_size: nil }
|
|
49
|
+
])
|
|
50
|
+
end
|
|
51
|
+
expect(a_request(:get, 'http://example.com/missing')).not_to have_been_made
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'preserves default suggested-image and collection behavior without downloading' do
|
|
55
|
+
html = '<meta property="og:image" content="/missing"><img src="/not-image" width="10" height="10">'
|
|
56
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
57
|
+
expect(page.images.best).to eq('http://example.com/missing')
|
|
58
|
+
expect(page.images.to_a).to eq(['http://example.com/not-image'])
|
|
59
|
+
expect(a_request(:get, 'http://example.com/missing')).not_to have_been_made
|
|
60
|
+
expect(a_request(:get, 'http://example.com/not-image')).not_to have_been_made
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'returns nil when all suggested and collection images are invalid' do
|
|
64
|
+
html = '<meta property="og:image" content="/missing"><img src="/not-image" width="10" height="10">'
|
|
65
|
+
page = MetaInspector.new('http://example.com/', **options, document: html)
|
|
66
|
+
expect(page.images.owner_suggested).to be_nil
|
|
67
|
+
expect(page.images.best).to be_nil
|
|
68
|
+
expect(page.images.to_a).to be_empty
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it 'treats unsupported image URL schemes as invalid' do
|
|
72
|
+
html = '<meta property="og:image" content="data:image/png;base64,AAAA"><img src="ftp://example.com/image.png"><img src="/good">'
|
|
73
|
+
page = MetaInspector.new('http://example.com/', **options, document: html, fetch_all_image_meta: true)
|
|
74
|
+
expect(page.images.best).to eq('http://example.com/good')
|
|
75
|
+
expect(page.images.to_a).to eq(['http://example.com/good'])
|
|
76
|
+
expect(page.images.with_metadata.first).to eq(url: 'ftp://example.com/image.png', valid: false)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'raises for redirects to unsupported schemes during validation' do
|
|
80
|
+
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => 'ftp://example.com/image.png' })
|
|
81
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/redirect">')
|
|
82
|
+
expect { page.images.to_a }.to raise_error(MetaInspector::URLPolicy::InvalidURL)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'propagates errors from the URL filter during validation' do
|
|
86
|
+
error = MetaInspector::RequestError.new('filter failure')
|
|
87
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/good">',
|
|
88
|
+
url_filter: ->(_) { raise error })
|
|
89
|
+
expect { page.images.to_a }.to raise_error { |raised| expect(raised).to equal(error) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'propagates errors raised by the redirect callback during validation' do
|
|
93
|
+
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => '/good' })
|
|
94
|
+
[MetaInspector::RequestError, MetaInspector::URLPolicy::InvalidURL, Faraday::ConnectionFailed].each do |error_class|
|
|
95
|
+
error = error_class.new('callback failure')
|
|
96
|
+
callback = ->(_, _) { raise error }
|
|
97
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/redirect">',
|
|
98
|
+
faraday_options: { redirect: { callback: callback } })
|
|
99
|
+
expect { page.images.to_a }.to raise_error { |raised| expect(raised).to equal(error) }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it 'raises for unsupported redirects in metadata-only mode' do
|
|
104
|
+
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => 'ftp://example.com/image.png' })
|
|
105
|
+
page = MetaInspector.new('http://example.com/', document: '<img src="/redirect">', fetch_all_image_meta: true)
|
|
106
|
+
expect { page.images.with_metadata }.to raise_error(MetaInspector::URLPolicy::InvalidURL)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it 'preserves default behavior for redirects to unsupported schemes' do
|
|
110
|
+
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => 'ftp://example.com/image.png' })
|
|
111
|
+
page = MetaInspector.new('http://example.com/', document: '<img src="/redirect">')
|
|
112
|
+
expect { page.images.with_size }.to raise_error(MetaInspector::RequestError, /HTTP or HTTPS/)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
describe 'image details' do
|
|
116
|
+
it 'omits images that failed validation instead of raising for unsupported URLs' do
|
|
117
|
+
html = '<img src="data:image/png;base64,AAAA"><img src="ftp://example.com/image.png"><img src="/missing">' \
|
|
118
|
+
'<img src="/good" width="10" height="10">'
|
|
119
|
+
page = MetaInspector.new('http://example.com/', **options, document: html)
|
|
120
|
+
expect(page.images.with_details).to eq([
|
|
121
|
+
{ url: 'http://example.com/good', width: 10, height: 10, type: :png, file_size: png.bytesize }
|
|
122
|
+
])
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
it 'reuses validation lookups instead of downloading images again' do
|
|
126
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/good">')
|
|
127
|
+
expect(page.images.to_a).to eq(['http://example.com/good'])
|
|
128
|
+
expect(page.images.with_details).to eq([
|
|
129
|
+
{ url: 'http://example.com/good', width: 37, height: 29, type: :png, file_size: png.bytesize }
|
|
130
|
+
])
|
|
131
|
+
expect(a_request(:get, 'http://example.com/good')).to have_been_made.once
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it 'shares the validation budget and keeps unchecked images without metadata' do
|
|
135
|
+
html = '<meta property="og:image" content="/missing"><img src="/good" width="10" height="10">'
|
|
136
|
+
page = MetaInspector.new('http://example.com/', **options, document: html, max_image_downloads: 1)
|
|
137
|
+
expect(page.images.owner_suggested).to be_nil
|
|
138
|
+
expect(page.images.with_details).to eq([
|
|
139
|
+
{ url: 'http://example.com/good', width: 10, height: 10, type: nil, file_size: nil }
|
|
140
|
+
])
|
|
141
|
+
expect(a_request(:get, 'http://example.com/good')).not_to have_been_made
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it 'does not spend the budget on unsupported URLs, which need no request' do
|
|
145
|
+
html = '<picture><source srcset="data:image/png;base64,AAAA 1x"></picture><img src="/good">'
|
|
146
|
+
page = MetaInspector.new('http://example.com/', **options, document: html, max_image_downloads: 1)
|
|
147
|
+
expect(page.images.with_details).to eq([
|
|
148
|
+
{ url: 'http://example.com/good', width: 37, height: 29, type: :png, file_size: png.bytesize }
|
|
149
|
+
])
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
it 'propagates URL filter rejections' do
|
|
153
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/good">',
|
|
154
|
+
url_filter: ->(_) { false })
|
|
155
|
+
expect { page.images.with_details }.to raise_error(MetaInspector::RequestError, /url_filter/)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it 'propagates invalid URL errors raised by the redirect callback' do
|
|
159
|
+
stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => '/good' })
|
|
160
|
+
error = MetaInspector::URLPolicy::InvalidURL.new('callback failure')
|
|
161
|
+
page = MetaInspector.new('http://example.com/', **options, document: '<img src="/redirect">',
|
|
162
|
+
faraday_options: { redirect: { callback: ->(_, _) { raise error } } })
|
|
163
|
+
expect { page.images.with_details }.to raise_error { |raised| expect(raised).to equal(error) }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it 'keeps unsupported URLs and failed lookups with unknown metadata in metadata-only mode' do
|
|
167
|
+
html = '<img src="data:image/png;base64,AAAA"><img src="/missing" width="5" height="5"><img src="/good">'
|
|
168
|
+
page = MetaInspector.new('http://example.com/', document: html, fetch_all_image_meta: true)
|
|
169
|
+
expect(page.images.with_details).to eq([
|
|
170
|
+
{ url: 'http://example.com/good', width: 37, height: 29, type: :png, file_size: png.bytesize },
|
|
171
|
+
{ url: 'http://example.com/missing', width: 5, height: 5, type: nil, file_size: nil },
|
|
172
|
+
{ url: 'data:image/png;base64', width: 0, height: 0, type: nil, file_size: nil }
|
|
173
|
+
])
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
it 'treats Unicode image URLs that cannot be normalized as invalid' do
|
|
177
|
+
html = %(<img src="http://%C3/ñ.png"><img src="/good" width="10" height="10">)
|
|
178
|
+
page = MetaInspector.new('http://example.com/', **options, document: html)
|
|
179
|
+
expect(page.images.to_a).to eq(['http://example.com/good'])
|
|
180
|
+
expect(page.images.with_details).to eq([
|
|
181
|
+
{ url: 'http://example.com/good', width: 10, height: 10, type: :png, file_size: png.bytesize }
|
|
182
|
+
])
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
it 'validates Unicode image URLs in their encoded form, preserving the original URL' do
|
|
186
|
+
original = "http://example.com/image\u202fname.png"
|
|
187
|
+
stub_request(:get, 'http://example.com/image%E2%80%AFname.png').to_return(body: png)
|
|
188
|
+
page = MetaInspector.new('http://example.com/', **options, document: %(<img src="#{original}">))
|
|
189
|
+
expect(page.images.to_a).to eq([original])
|
|
190
|
+
expect(page.images.with_details).to eq([
|
|
191
|
+
{ url: original, width: 37, height: 29, type: :png, file_size: png.bytesize }
|
|
192
|
+
])
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'base64'
|
|
3
|
+
|
|
4
|
+
RSpec.describe 'inline images' do
|
|
5
|
+
let(:png) { "\x89PNG\r\n\x1a\n".b + [13].pack('N') + 'IHDR' + [37, 29].pack('NN') + "\x08\x02\x00\x00\x00".b }
|
|
6
|
+
let(:data_url) { "data:image/png;base64,#{Base64.strict_encode64(png)}" }
|
|
7
|
+
let(:encoded_url) { 'data:image/png,' + png.bytes.map { |byte| format('%%%02X', byte) }.join }
|
|
8
|
+
|
|
9
|
+
it 'returns PNG type and decoded byte size for base64 and percent-encoded images without spending the download budget' do
|
|
10
|
+
html = %(<img src="#{data_url}"><img src="#{encoded_url}"><img src="ftp://example.com/a"><img src="/good">)
|
|
11
|
+
stub_request(:get, 'http://example.com/good').to_return(body: png)
|
|
12
|
+
page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1, fetch_all_image_meta: true, inline_image_data: true)
|
|
13
|
+
expect(page.images.with_metadata.map { |entry| entry[:valid] }).to eq([true, true, false, true])
|
|
14
|
+
expect(page.images.with_details).to include({ url: data_url, width: 37, height: 29, type: :png, file_size: png.bytesize },
|
|
15
|
+
{ url: encoded_url, width: 37, height: 29, type: :png, file_size: png.bytesize })
|
|
16
|
+
expect(a_request(:get, 'http://example.com/good')).to have_been_made.once
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'returns unknown details for invalid data and unsupported schemes, and rejects them when validating' do
|
|
20
|
+
html = '<img src="data:image/png;base64,!!!!"><img src="file:///bad">'
|
|
21
|
+
page = MetaInspector.new('http://example.com/', document: html)
|
|
22
|
+
expect(page.images.with_size.map { |_, w, h| [w, h] }).to eq([[0, 0], [0, 0]])
|
|
23
|
+
expect(page.images.with_details.map { |item| [item[:type], item[:file_size]] }).to eq([[nil, nil], [nil, nil]])
|
|
24
|
+
validating = MetaInspector.new('http://example.com/', document: html, download_image_options: { reject_invalid_images: true })
|
|
25
|
+
expect(validating.images.to_a).to be_empty
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'hides payloads in every image result while retaining distinct candidates' do
|
|
29
|
+
second = 'data:image/png;base64,' + Base64.strict_encode64(png.sub([37, 29].pack('NN'), [2, 3].pack('NN')))
|
|
30
|
+
html = %(<meta property="og:image" content="#{data_url}"><link rel="icon" href="#{data_url}"><img src="#{data_url}"><img src="#{second}"><img src="/photo" width="800" height="600">)
|
|
31
|
+
page = MetaInspector.new('http://example.com/', document: html, fetch_all_image_meta: true)
|
|
32
|
+
stub_request(:get, 'http://example.com/photo').to_return(body: png)
|
|
33
|
+
expect(page.images.to_a).to eq(['data:image/png;base64', 'data:image/png;base64', 'http://example.com/photo'])
|
|
34
|
+
expect(page.images[0]).to eq('data:image/png;base64')
|
|
35
|
+
expect(page.images.with_size).to eq([['http://example.com/photo', 800, 600], ['data:image/png;base64', 37, 29], ['data:image/png;base64', 2, 3]])
|
|
36
|
+
expect(page.images.with_details.map { |item| item[:url] }).to eq(['http://example.com/photo', 'data:image/png;base64', 'data:image/png;base64'])
|
|
37
|
+
expect(page.images.with_metadata.map { |item| item[:url] }).to eq(page.images.to_a)
|
|
38
|
+
expect(page.images.owner_suggested).to eq('data:image/png;base64')
|
|
39
|
+
expect(page.images.favicon).to eq('data:image/png;base64')
|
|
40
|
+
expect(page.images.best).to eq('http://example.com/photo')
|
|
41
|
+
expect(page.images.largest).to eq('http://example.com/photo')
|
|
42
|
+
expect(page.meta['og:image']).to eq(data_url)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'returns and ranks full data URLs when opted in' do
|
|
46
|
+
html = %(<meta property="og:image" content="#{data_url}"><img src="#{data_url}">)
|
|
47
|
+
page = MetaInspector.new('http://example.com/', document: html, inline_image_data: true)
|
|
48
|
+
expect(page.images.to_a).to eq([data_url])
|
|
49
|
+
expect(page.images.with_size).to eq([[data_url, 37, 29]])
|
|
50
|
+
expect(page.images.largest).to eq(data_url)
|
|
51
|
+
expect(page.images.best).to eq(data_url)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'decodes local images after the HTTP download budget is exhausted' do
|
|
55
|
+
page = MetaInspector.new('http://example.com/', document: %(<img src="#{data_url}">),
|
|
56
|
+
max_image_downloads: 0, fetch_all_image_meta: true, inline_image_data: true)
|
|
57
|
+
expect(page.images.with_metadata).to eq([{ url: data_url, valid: true }])
|
|
58
|
+
expect(page.images.with_details.first).to include(width: 37, height: 29, type: :png, file_size: png.bytesize)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'skips inline suggested images for best and keeps raw metadata in to_hash' do
|
|
62
|
+
html = %(<meta property="og:image" content="#{data_url}"><meta name="twitter:image" content="/photo"><img src="#{data_url}">)
|
|
63
|
+
stub_request(:get, 'http://example.com/page').to_return(body: html, headers: { 'Content-Type' => 'text/html' })
|
|
64
|
+
page = MetaInspector.new('http://example.com/page')
|
|
65
|
+
expect(page.images.best).to eq('http://example.com/photo')
|
|
66
|
+
expect(page.to_hash['images']).to eq(['data:image/png;base64'])
|
|
67
|
+
expect(page.meta['og:image']).to eq(data_url)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it 'returns no largest or best image when all images are inline placeholders' do
|
|
71
|
+
page = MetaInspector.new('http://example.com/', document: %(<img src="#{data_url}">))
|
|
72
|
+
expect(page.images.largest).to be_nil
|
|
73
|
+
expect(page.images.best).to be_nil
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -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
|