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
data/spec/document_spec.rb
CHANGED
|
@@ -1,6 +1,144 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
describe MetaInspector::Document do
|
|
4
|
+
describe 'public API' do
|
|
5
|
+
let(:html) do
|
|
6
|
+
<<~HTML
|
|
7
|
+
<html><head>
|
|
8
|
+
<link rel="icon" href="/icon.png">
|
|
9
|
+
<link rel="alternate" type="application/rss+xml" title="News" href="/feed.xml">
|
|
10
|
+
</head><body></body></html>
|
|
11
|
+
HTML
|
|
12
|
+
end
|
|
13
|
+
let(:page) { MetaInspector.new('https://example.com/', document: html) }
|
|
14
|
+
|
|
15
|
+
it 'retains the deprecated feed and favicon methods' do
|
|
16
|
+
expect(page).to respond_to(:feed, :favicon)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'exposes feeds through the plural API' do
|
|
20
|
+
expect(page.feeds).to eq([
|
|
21
|
+
{ title: 'News', href: 'https://example.com/feed.xml', type: 'application/rss+xml' }
|
|
22
|
+
])
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'exposes the favicon through the images API' do
|
|
26
|
+
expect(page.images.favicon).to eq('https://example.com/icon.png')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
{ feed: 'feeds', favicon: 'images.favicon' }.each do |method, replacement|
|
|
30
|
+
it "warns on each call to ##{method}, naming its replacement and removal version" do
|
|
31
|
+
2.times do
|
|
32
|
+
expect { page.public_send(method) }.to output(
|
|
33
|
+
"DEPRECATION WARNING: page.#{method} is deprecated and will be removed in 6.0. Use page.#{replacement} instead.\n"
|
|
34
|
+
).to_stderr
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "returns nil from ##{method} when the document has no matching metadata" do
|
|
39
|
+
empty_page = MetaInspector.new('https://example.com/', document: '<html></html>')
|
|
40
|
+
|
|
41
|
+
expect { expect(empty_page.public_send(method)).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'returns the favicon URL from the deprecated shortcut' do
|
|
46
|
+
expect { expect(page.favicon).to eq(page.images.favicon) }.to output(/DEPRECATION/).to_stderr
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'keeps the supported APIs and serialization free of deprecation warnings' do
|
|
50
|
+
stub_request(:get, 'https://example.com/').to_return(body: html)
|
|
51
|
+
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
52
|
+
|
|
53
|
+
expect do
|
|
54
|
+
fetched_page.feeds
|
|
55
|
+
fetched_page.images.favicon
|
|
56
|
+
fetched_page.to_hash
|
|
57
|
+
end.not_to output.to_stderr
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe '#feed' do
|
|
61
|
+
let(:html) do
|
|
62
|
+
<<~HTML
|
|
63
|
+
<html><head>
|
|
64
|
+
<base href="https://example.com/blog/">
|
|
65
|
+
<link rel="alternate" type="application/json" href="feed.json">
|
|
66
|
+
<link rel="alternate" type="#{first_type}" href="first.xml">
|
|
67
|
+
<link rel="alternate" type="application/rss+xml" href="second.xml">
|
|
68
|
+
</head></html>
|
|
69
|
+
HTML
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
['application/rss+xml', 'application/atom+xml'].each do |type|
|
|
73
|
+
context "with #{type} first" do
|
|
74
|
+
let(:first_type) { type }
|
|
75
|
+
|
|
76
|
+
it 'returns the first RSS or Atom URL, skipping JSON and resolving the base URL' do
|
|
77
|
+
expect { expect(page.feed).to eq('https://example.com/blog/first.xml') }
|
|
78
|
+
.to output(/DEPRECATION/).to_stderr
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context 'with only JSON feeds' do
|
|
84
|
+
let(:html) { '<link rel="alternate" type="application/json" href="/feed.json">' }
|
|
85
|
+
|
|
86
|
+
it 'returns nil' do
|
|
87
|
+
expect { expect(page.feed).to be_nil }.to output(/DEPRECATION/).to_stderr
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
['<html></html>', nil].each do |document|
|
|
93
|
+
context "with #{document ? 'empty' : 'populated'} HTML" do
|
|
94
|
+
before do
|
|
95
|
+
stub_request(:get, 'https://example.com/').to_return(
|
|
96
|
+
body: document || html, headers: { 'Content-Type' => 'text/html' }
|
|
97
|
+
)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
MetaInspector::Document.public_instance_methods(false).sort.each do |method|
|
|
101
|
+
it "can call ##{method}" do
|
|
102
|
+
fetched_page = MetaInspector.new('https://example.com/', download_images: false)
|
|
103
|
+
|
|
104
|
+
if [:feed, :favicon].include?(method)
|
|
105
|
+
expect { fetched_page.public_send(method) }.to output(/DEPRECATION/).to_stderr
|
|
106
|
+
else
|
|
107
|
+
expect { fetched_page.public_send(method) }.not_to raise_error
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
describe 'max_html_bytes option' do
|
|
116
|
+
it 'rejects oversized supplied HTML before parsing' do
|
|
117
|
+
expect do
|
|
118
|
+
MetaInspector.new('http://example.com/', document: '<p>é</p>', max_html_bytes: 8)
|
|
119
|
+
end.to raise_error(MetaInspector::ParserError, /HTML.*9 bytes.*8 bytes/)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it 'accepts HTML at the byte limit and without a limit' do
|
|
123
|
+
html = '<title>é</title>'
|
|
124
|
+
expect(MetaInspector.new('http://example.com/', document: html, max_html_bytes: html.bytesize).title).to eq('é')
|
|
125
|
+
expect(MetaInspector.new('http://example.com/', document: html).title).to eq('é')
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it 'rejects oversized fetched HTML' do
|
|
129
|
+
stub_request(:get, 'http://example.com/').to_return(body: '<p>hello</p>', headers: { 'Content-Type' => 'text/html' })
|
|
130
|
+
expect { MetaInspector.new('http://example.com/', max_html_bytes: 5) }.to raise_error(MetaInspector::ParserError)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
[-1, 1.5, '1', true, false].each do |limit|
|
|
134
|
+
it "rejects invalid limit #{limit.inspect} before fetching" do
|
|
135
|
+
expect { MetaInspector.new('http://example.com/', max_html_bytes: limit) }
|
|
136
|
+
.to raise_error(ArgumentError, 'max_html_bytes must be a non-negative Integer or nil')
|
|
137
|
+
expect(WebMock).not_to have_requested(:get, 'http://example.com/')
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
4
142
|
describe 'max_image_downloads option' do
|
|
5
143
|
[-1, 1.5, '1', true, false].each do |limit|
|
|
6
144
|
it "should reject #{limit.inspect} before fetching the document" do
|
|
@@ -62,6 +200,7 @@ describe MetaInspector::Document do
|
|
|
62
200
|
'non_http' => ["mailto:pagerankalert@gmail.com"]
|
|
63
201
|
},
|
|
64
202
|
"images" => ["http://pagerankalert.com/images/pagerank_alert.png?1305794559"],
|
|
203
|
+
"iframes" => [],
|
|
65
204
|
"charset" => "utf-8",
|
|
66
205
|
"feeds" => [{href: "http://feeds.feedburner.com/PageRankAlert", title: "PageRankAlert.com blog", type: "application/rss+xml"}],
|
|
67
206
|
"h1" => [],
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'stringio'
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
RSpec.describe 'runnable examples' do
|
|
6
|
+
def run_example(name, *args)
|
|
7
|
+
stub_const('ARGV', args)
|
|
8
|
+
original_stdout = $stdout
|
|
9
|
+
$stdout = StringIO.new
|
|
10
|
+
load(File.expand_path("../examples/#{name}.rb", __dir__), true)
|
|
11
|
+
$stdout.string
|
|
12
|
+
ensure
|
|
13
|
+
$stdout = original_stdout
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def stub_page(path, html, status: 200)
|
|
17
|
+
stub_request(:get, "http://site.example#{path}").to_return(
|
|
18
|
+
status: status, headers: { 'Content-Type' => 'text/html' }, body: html
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def stub_image(path, fixture)
|
|
23
|
+
stub_request(:get, "http://site.example#{path}").to_return(fixture_file(fixture))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe 'basic_scraping' do
|
|
27
|
+
it 'prints title and links' do
|
|
28
|
+
output = run_example('basic_scraping', 'http://example.com')
|
|
29
|
+
expect(output).to include('TITLE: An example page', 'internal links found')
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'runs from the examples directory' do
|
|
33
|
+
Dir.chdir(File.expand_path('../examples', __dir__)) do
|
|
34
|
+
expect(run_example('basic_scraping', 'http://example.com')).to include('TITLE: An example page')
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it 'prints the page hash as JSON' do
|
|
39
|
+
output = Dir.chdir(File.expand_path('../examples', __dir__)) do
|
|
40
|
+
run_example('basic_scraping', 'http://example.com')
|
|
41
|
+
end
|
|
42
|
+
expect(JSON.parse(output.split("to_hash...\n", 2).last)).to include('title' => 'An example page')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe 'spider' do
|
|
47
|
+
it 'visits every internal page once' do
|
|
48
|
+
stub_page('/', '<a href="/a">A</a><a href="/b">B</a>')
|
|
49
|
+
stub_page('/a', '<a href="/">Home</a>')
|
|
50
|
+
stub_page('/b', '<title>B</title>')
|
|
51
|
+
|
|
52
|
+
output = run_example('spider', 'http://site.example/')
|
|
53
|
+
expect(output.lines.grep(/^VISITED: /).map(&:strip)).to contain_exactly(
|
|
54
|
+
'VISITED: http://site.example/', 'VISITED: http://site.example/a', 'VISITED: http://site.example/b'
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
describe 'link_checker' do
|
|
60
|
+
it 'reports broken links with their origin' do
|
|
61
|
+
stub_page('/', '<a href="/ok">OK</a><a href="/missing">Missing</a>')
|
|
62
|
+
stub_page('/ok', '<title>OK</title>')
|
|
63
|
+
stub_page('/missing', '<title>Missing</title>', status: 404)
|
|
64
|
+
|
|
65
|
+
output = run_example('link_checker', 'http://site.example/')
|
|
66
|
+
expect(output).to include('1 broken links found.', "http://site.example/missing linked from\n - http://site.example/")
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
describe 'faraday_redirect_options' do
|
|
71
|
+
it 'rejects an untrusted initial URL before making a request' do
|
|
72
|
+
target = 'http://127.0.0.1/'
|
|
73
|
+
stub_request(:get, target).to_return(status: 200)
|
|
74
|
+
|
|
75
|
+
expect { run_example('faraday_redirect_options', target) }
|
|
76
|
+
.to output(/Request blocked by url_filter/).to_stderr
|
|
77
|
+
expect(a_request(:get, target)).not_to have_been_made
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'rejects an untrusted redirect before making a request to its destination' do
|
|
81
|
+
target = 'http://127.0.0.1/'
|
|
82
|
+
stub_request(:get, 'https://example.com/').to_return(status: 302, headers: { 'Location' => target })
|
|
83
|
+
stub_request(:get, target).to_return(status: 200)
|
|
84
|
+
stdout = nil
|
|
85
|
+
|
|
86
|
+
expect { stdout = run_example('faraday_redirect_options', 'https://example.com/') }
|
|
87
|
+
.to output(/Request blocked by url_filter/).to_stderr
|
|
88
|
+
expect(stdout).to include('Redirect:')
|
|
89
|
+
expect(a_request(:get, target)).not_to have_been_made
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'follows redirects to allowed HTTPS hosts' do
|
|
93
|
+
stub_request(:get, 'https://example.com/').to_return(status: 302, headers: { 'Location' => 'https://www.example.com/' })
|
|
94
|
+
stub_request(:get, 'https://www.example.com/').to_return(status: 200,
|
|
95
|
+
headers: { 'Content-Type' => 'text/html' }, body: '<title>Allowed page</title>')
|
|
96
|
+
|
|
97
|
+
stdout = run_example('faraday_redirect_options', 'https://example.com/')
|
|
98
|
+
expect(stdout).to match(/Redirect:.*Allowed page/m)
|
|
99
|
+
expect(a_request(:get, 'https://www.example.com/')).to have_been_made.once
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
describe 'responsive_images' do
|
|
104
|
+
it 'lists src and srcset candidates in document order' do
|
|
105
|
+
stub_page('/', <<~HTML)
|
|
106
|
+
<meta property="og:image" content="/og.png">
|
|
107
|
+
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACw=" srcset="/medium.png 2x, /large.png 1000w">
|
|
108
|
+
<picture><source srcset="/wide.webp"><img src="/fallback.png"></picture>
|
|
109
|
+
HTML
|
|
110
|
+
|
|
111
|
+
stdout = run_example('responsive_images', 'http://site.example/')
|
|
112
|
+
expect(stdout.lines.grep(/^ ==> /).map(&:strip)).to eq([
|
|
113
|
+
'==> data:image/gif;base64',
|
|
114
|
+
'==> http://site.example/medium.png',
|
|
115
|
+
'==> http://site.example/large.png',
|
|
116
|
+
'==> http://site.example/wide.webp',
|
|
117
|
+
'==> http://site.example/fallback.png'
|
|
118
|
+
])
|
|
119
|
+
expect(stdout).to include('5 image candidates found', 'BEST: http://site.example/og.png')
|
|
120
|
+
expect(stdout).not_to include('R0lGOD')
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
describe 'image_details' do
|
|
125
|
+
before do
|
|
126
|
+
stub_page('/', '<img src="/100x100.jpg"><img src="/10x10.jpg">')
|
|
127
|
+
stub_image('/100x100.jpg', '100x100.jpg.response')
|
|
128
|
+
stub_image('/10x10.jpg', '10x10.jpg.response')
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
it 'prints dimensions, type and file size, largest first' do
|
|
132
|
+
stub_page('/', '<img src="/10x10.jpg"><img src="/100x100.jpg">')
|
|
133
|
+
stdout = run_example('image_details', 'http://site.example/')
|
|
134
|
+
expect(stdout).to match(%r{100x100\.jpg 100x100 png, \d+ bytes.*10x10\.jpg 10x10 jpeg, \d+ bytes}m)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
it 'respects the download limit' do
|
|
138
|
+
stdout = run_example('image_details', 'http://site.example/', '1')
|
|
139
|
+
expect(stdout).to include('http://site.example/100x100.jpg 100x100 png,',
|
|
140
|
+
'http://site.example/10x10.jpg 0x0 unknown type, unknown size')
|
|
141
|
+
expect(a_request(:get, 'http://site.example/10x10.jpg')).not_to have_been_made
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
describe 'image_validation' do
|
|
146
|
+
before do
|
|
147
|
+
stub_page('/', <<~HTML)
|
|
148
|
+
<meta property="og:image" content="/missing.png">
|
|
149
|
+
<img src="/10x10.jpg"><img src="/broken.png">
|
|
150
|
+
HTML
|
|
151
|
+
stub_image('/10x10.jpg', '10x10.jpg.response')
|
|
152
|
+
stub_page('/broken.png', '<title>Not an image</title>')
|
|
153
|
+
stub_page('/missing.png', '<title>Missing</title>', status: 404)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
it 'reports validity and skips invalid images' do
|
|
157
|
+
stdout = run_example('image_validation', 'http://site.example/')
|
|
158
|
+
expect(stdout).to include('http://site.example/10x10.jpg: valid',
|
|
159
|
+
'http://site.example/broken.png: invalid',
|
|
160
|
+
'BEST: http://site.example/10x10.jpg')
|
|
161
|
+
filtered = stdout.split("images remain after validation...\n", 2).last
|
|
162
|
+
expect(filtered).to include(' ==> http://site.example/10x10.jpg')
|
|
163
|
+
expect(filtered).not_to include('broken.png', 'missing.png')
|
|
164
|
+
expect(a_request(:get, 'http://site.example/missing.png')).to have_been_made.once
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
it 'reports images beyond the download limit as not checked' do
|
|
168
|
+
stdout = run_example('image_validation', 'http://site.example/', '1')
|
|
169
|
+
expect(stdout).to include('http://site.example/10x10.jpg: valid',
|
|
170
|
+
'http://site.example/broken.png: not checked')
|
|
171
|
+
expect(a_request(:get, 'http://site.example/broken.png')).not_to have_been_made
|
|
172
|
+
expect(a_request(:get, 'http://site.example/missing.png')).not_to have_been_made
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
it 'can run twice without warnings' do
|
|
176
|
+
expect {
|
|
177
|
+
2.times { run_example('image_validation', 'http://site.example/') }
|
|
178
|
+
}.not_to output.to_stderr
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
describe 'iframes' do
|
|
183
|
+
it 'prints iframe attributes without downloading embedded documents' do
|
|
184
|
+
stub_page('/', '<iframe src=" /embed " title="Video" allowfullscreen></iframe><iframe></iframe>')
|
|
185
|
+
|
|
186
|
+
stdout = run_example('iframes', 'http://site.example/')
|
|
187
|
+
expect(stdout).to include('2 iframes found', ' ==> http://site.example/embed',
|
|
188
|
+
'title="Video"', 'allowfullscreen=""', ' ==> (no source)')
|
|
189
|
+
expect(a_request(:get, 'http://site.example/embed')).not_to have_been_made
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
describe 'html_size_limit' do
|
|
194
|
+
before do
|
|
195
|
+
stub_page('/', '<title>Small page</title>'.ljust(200))
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'parses pages within the limit' do
|
|
199
|
+
expect(run_example('html_size_limit', 'http://site.example/')).to include('TITLE: Small page')
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
it 'skips pages over the limit' do
|
|
203
|
+
expect(run_example('html_size_limit', 'http://site.example/', '50')).to include('Skipped:')
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
data/spec/image_download_spec.rb
CHANGED
|
@@ -60,6 +60,17 @@ RSpec.describe 'bounded image downloads' do
|
|
|
60
60
|
end
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
+
it 'counts a fully received chunked image without a content length' do
|
|
64
|
+
serve do |socket|
|
|
65
|
+
socket.write("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n")
|
|
66
|
+
socket.write("#{png.bytesize.to_s(16)}\r\n")
|
|
67
|
+
socket.write(png)
|
|
68
|
+
socket.write("\r\n0\r\n\r\n")
|
|
69
|
+
end
|
|
70
|
+
page = MetaInspector.new('http://page.example/', document: %(<img src="#{@url}">))
|
|
71
|
+
expect(page.images.with_details.first).to include(type: :png, file_size: png.bytesize)
|
|
72
|
+
end
|
|
73
|
+
|
|
63
74
|
it 'bounds binary IO and stops a server that ignores Range before reading its full body' do
|
|
64
75
|
sent = 0
|
|
65
76
|
serve do |socket|
|
|
@@ -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
|