metainspector 5.19.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.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/Gemfile +1 -1
  4. data/README.md +214 -9
  5. data/examples/basic_scraping.rb +4 -3
  6. data/examples/faraday_redirect_options.rb +23 -31
  7. data/examples/html_size_limit.rb +17 -0
  8. data/examples/iframes.rb +15 -0
  9. data/examples/image_details.rb +17 -0
  10. data/examples/image_validation.rb +25 -0
  11. data/examples/link_checker.rb +2 -2
  12. data/examples/responsive_images.rb +15 -0
  13. data/examples/spider.rb +2 -2
  14. data/lib/meta_inspector/document.rb +23 -4
  15. data/lib/meta_inspector/http_session.rb +75 -0
  16. data/lib/meta_inspector/image_downloader.rb +98 -0
  17. data/lib/meta_inspector/parser.rb +22 -2
  18. data/lib/meta_inspector/parsers/head_links.rb +18 -3
  19. data/lib/meta_inspector/parsers/iframes.rb +21 -0
  20. data/lib/meta_inspector/parsers/images.rb +196 -27
  21. data/lib/meta_inspector/parsers/texts.rb +2 -2
  22. data/lib/meta_inspector/request.rb +17 -39
  23. data/lib/meta_inspector/url_policy.rb +68 -0
  24. data/lib/meta_inspector/version.rb +1 -1
  25. data/lib/meta_inspector.rb +1 -0
  26. data/meta_inspector.gemspec +1 -2
  27. data/spec/document_spec.rb +139 -0
  28. data/spec/examples_spec.rb +206 -0
  29. data/spec/image_download_spec.rb +125 -0
  30. data/spec/image_metadata_spec.rb +110 -0
  31. data/spec/image_validity_spec.rb +195 -0
  32. data/spec/inline_images_spec.rb +75 -0
  33. data/spec/meta_inspector/head_links_spec.rb +69 -0
  34. data/spec/meta_inspector/iframes_spec.rb +50 -0
  35. data/spec/meta_inspector/images_spec.rb +130 -0
  36. data/spec/meta_inspector/meta_inspector_spec.rb +58 -7
  37. data/spec/meta_inspector/texts_spec.rb +50 -0
  38. data/spec/parser_spec.rb +10 -0
  39. data/spec/request_spec.rb +1 -1
  40. data/spec/unicode_image_urls_spec.rb +74 -0
  41. data/spec/url_filter_spec.rb +206 -0
  42. metadata +32 -36
  43. data/examples/redirect_web_server.rb +0 -5
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'image metadata' do
4
+ let(:url) { 'https://example.com/image.png' }
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(:html) { %(<img src="#{url}" width="10" height="20">) }
7
+
8
+ it 'returns metadata without changing the existing size tuples' do
9
+ stub_request(:get, url).to_return(body: png, headers: { 'Content-Length' => '1234' })
10
+ page = MetaInspector.new('https://example.com/', document: html)
11
+ expect(page.images.with_details).to eq([{ url: url, width: 10, height: 20, type: :png, file_size: 1234 }])
12
+ expect(page.images.with_size).to eq([[url, 10, 20]])
13
+ expect(a_request(:get, url)).to have_been_made.once
14
+ end
15
+
16
+ it 'uses the total from a partial response rather than its content length' do
17
+ stub_request(:get, url).to_return(status: 206, body: png,
18
+ headers: { 'Content-Length' => png.bytesize.to_s, 'Content-Range' => 'bytes 0-28/5000' })
19
+ page = MetaInspector.new('https://example.com/', document: %(<img src="#{url}">))
20
+ expect(page.images.with_details).to eq([{ url: url, width: 37, height: 29, type: :png, file_size: 5000 }])
21
+ end
22
+
23
+ it 'returns nil metadata for failed and disabled downloads' do
24
+ stub_request(:get, url).to_timeout
25
+ page = MetaInspector.new('https://example.com/', document: html)
26
+ expect(page.images.with_details.first).to eq(url: url, width: 10, height: 20, type: nil, file_size: nil)
27
+ disabled = MetaInspector.new('https://example.com/', document: html, download_images: false)
28
+ expect(disabled.images.with_details.first).to eq(url: url, width: 10, height: 20, type: nil, file_size: nil)
29
+ expect(a_request(:get, url)).to have_been_made.once
30
+ end
31
+
32
+ it 'preserves each HTML dimension when the other is missing' do
33
+ stub_request(:get, url).to_return(body: png)
34
+ page = MetaInspector.new('https://example.com/', document: %(<img src="#{url}" width="640">))
35
+ expect(page.images.with_details.first).to include(width: 640, height: 29)
36
+ disabled = MetaInspector.new('https://example.com/', download_images: false,
37
+ document: %(<img src="#{url}" height="480">))
38
+ expect(disabled.images.with_details.first).to include(width: 0, height: 480)
39
+ end
40
+
41
+ it 'counts the bytes of a complete response without a reported length' do
42
+ stub_request(:get, url).to_return(body: png)
43
+ page = MetaInspector.new('https://example.com/', document: html)
44
+ expect(page.images.with_details.first[:file_size]).to eq(png.bytesize)
45
+ end
46
+
47
+ it 'does not treat a stopped download as a complete file' do
48
+ stub_request(:get, url).to_return(body: png + ('x' * (256 * 1024)))
49
+ page = MetaInspector.new('https://example.com/', document: html)
50
+ expect(page.images.with_details.first[:file_size]).to be_nil
51
+ end
52
+
53
+ it 'does not mistake a partial or compressed response length for the file size' do
54
+ stub_request(:get, url).to_return(status: 206, body: png, headers: { 'Content-Length' => '29' })
55
+ page = MetaInspector.new('https://example.com/', document: html)
56
+ expect(page.images.with_details.first).to include(type: :png, file_size: nil)
57
+ end
58
+
59
+ it 'respects the download limit and sorts by area' do
60
+ other = 'https://example.com/other.png'
61
+ stub_request(:get, url).to_return(body: png)
62
+ page = MetaInspector.new('https://example.com/', max_image_downloads: 1,
63
+ document: %(<img src="#{url}"><img src="#{url}"><img src="#{other}" width="100" height="100">))
64
+ expect(page.images.with_details).to eq([
65
+ { url: other, width: 100, height: 100, type: nil, file_size: nil },
66
+ { url: url, width: 37, height: 29, type: :png, file_size: png.bytesize }
67
+ ])
68
+ expect(page.images.with_details.length).to eq(2)
69
+ expect(a_request(:get, url)).to have_been_made.once
70
+ expect(a_request(:get, other)).not_to have_been_made
71
+ end
72
+
73
+ it 'propagates URL policy failures' do
74
+ page = MetaInspector.new('https://example.com/', document: html, url_filter: ->(_) { false })
75
+ expect { page.images.with_details }.to raise_error(MetaInspector::RequestError)
76
+ end
77
+
78
+ it 'returns unknown metadata for an invalid inline image among responsive candidates' do
79
+ stub_request(:get, url).to_return(body: png)
80
+ invalid_png = 'data:image/png;base64,AAAA' # Decodes to three zero bytes, not a PNG.
81
+ html = %(<picture><source srcset="#{url} 2x, #{invalid_png} 1x"></picture>)
82
+ page = MetaInspector.new('https://example.com/', document: html)
83
+ expect(page.images.with_details).to include(include(url: url, type: :png, file_size: png.bytesize))
84
+ expect(page.images.with_details).to include(include(url: 'data:image/png;base64', type: nil, file_size: nil))
85
+ end
86
+
87
+ it 'sends only the page origin as referer to a cross-origin image server' do
88
+ image = 'https://assets.example/image.png'
89
+ stub_request(:get, image).with(headers: { 'Referer' => 'https://example.com/' })
90
+ .to_return(body: png, headers: { 'Content-Length' => '29' })
91
+ page = MetaInspector.new('https://example.com/private?token=secret', document: %(<img src="#{image}">),
92
+ headers: { 'Referer' => 'https://private.example/secret' })
93
+ expect(page.images.with_details.first).to include(type: :png, file_size: 29)
94
+ end
95
+
96
+ it 'does not send a secure page origin to an insecure image server' do
97
+ image = 'http://assets.example/image.png'
98
+ stub_request(:get, image).with { |request| !request.headers.key?('Referer') }.to_return(body: png)
99
+ page = MetaInspector.new('https://example.com/private', document: %(<img src="#{image}">))
100
+ expect(page.images.with_details.first).to include(type: :png)
101
+ end
102
+
103
+ it 'fetches Unicode image URLs while preserving their original representation' do
104
+ original = "https://example.com/image\u202fname.png?resize=800%2C425"
105
+ encoded = 'https://example.com/image%E2%80%AFname.png?resize=800%2C425'
106
+ stub_request(:get, encoded).to_return(body: png)
107
+ page = MetaInspector.new('https://example.com/', document: %(<img src="#{original}">))
108
+ expect(page.images.with_details.first).to include(url: original, type: :png, file_size: png.bytesize)
109
+ end
110
+ end
@@ -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&#160;#{keyword}", "nofollow&#11;#{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="&lt;p&gt;Hi&lt;/p&gt;"></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&amp;a=1 "></iframe>' \
22
+ '<iframe src="//player.example.com/video"></iframe><iframe src="embed?b=2&amp;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