metainspector 5.21.2 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -0
  3. data/README.md +215 -48
  4. data/lib/meta_inspector/attempt_timeout.rb +40 -0
  5. data/lib/meta_inspector/document.rb +101 -68
  6. data/lib/meta_inspector/errors.rb +5 -1
  7. data/lib/meta_inspector/html_encoding.rb +183 -0
  8. data/lib/meta_inspector/http_session.rb +74 -45
  9. data/lib/meta_inspector/image_downloader.rb +37 -24
  10. data/lib/meta_inspector/parser.rb +144 -34
  11. data/lib/meta_inspector/parsers/base.rb +7 -2
  12. data/lib/meta_inspector/parsers/head_links.rb +20 -28
  13. data/lib/meta_inspector/parsers/iframes.rb +2 -3
  14. data/lib/meta_inspector/parsers/images.rb +119 -70
  15. data/lib/meta_inspector/parsers/links.rb +19 -9
  16. data/lib/meta_inspector/parsers/meta_tags.rb +22 -31
  17. data/lib/meta_inspector/parsers/texts.rb +43 -33
  18. data/lib/meta_inspector/request.rb +67 -55
  19. data/lib/meta_inspector/url.rb +43 -19
  20. data/lib/meta_inspector/url_policy.rb +18 -11
  21. data/lib/meta_inspector/version.rb +1 -1
  22. data/lib/meta_inspector.rb +14 -13
  23. data/lib/metainspector.rb +1 -1
  24. data/meta_inspector.gemspec +1 -1
  25. data/spec/document_spec.rb +154 -56
  26. data/spec/meta_inspector/content_type_spec.rb +47 -0
  27. data/spec/meta_inspector/head_links_spec.rb +3 -3
  28. data/spec/meta_inspector/images_spec.rb +82 -1
  29. data/spec/meta_inspector/links_spec.rb +98 -2
  30. data/spec/meta_inspector/meta_inspector_spec.rb +3 -25
  31. data/spec/meta_inspector/meta_tags_spec.rb +53 -0
  32. data/spec/meta_inspector/texts_spec.rb +140 -3
  33. data/spec/meta_inspector/timeouts_spec.rb +107 -0
  34. data/spec/noscript_spec.rb +179 -0
  35. data/spec/parser_spec.rb +380 -10
  36. data/spec/query_string_spec.rb +125 -0
  37. data/spec/request_errors_spec.rb +91 -0
  38. data/spec/request_spec.rb +28 -10
  39. data/spec/response_encoding_spec.rb +487 -0
  40. data/spec/url_filter_spec.rb +2 -1
  41. data/spec/url_spec.rb +37 -0
  42. metadata +10 -2
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.homepage = "https://github.com/jaimeiniesta/metainspector"
9
9
  gem.license = "MIT"
10
10
 
11
- gem.files = `git ls-files`.split("\n") - ["Gemfile.lock"]
11
+ gem.files = (`git ls-files`.split("\n") - ["Gemfile.lock"]).reject { |f| f.start_with?("assets/") }
12
12
  gem.test_files = `git ls-files -- {spec}/*`.split("\n")
13
13
  gem.name = "metainspector"
14
14
  gem.require_paths = ["lib"]
@@ -1,6 +1,76 @@
1
1
  require 'spec_helper'
2
+ require 'json'
2
3
 
3
4
  describe MetaInspector::Document do
5
+ describe '#to_hash response headers' do
6
+ let(:url) { 'https://headers.example/' }
7
+ let(:page) { MetaInspector.new(url, download_images: false) }
8
+
9
+ before do
10
+ stub_request(:get, url).to_return(
11
+ body: '<title>Example</title>',
12
+ headers: { 'Content-Type' => 'text/html', 'X-Olaf' => header_value }
13
+ )
14
+ end
15
+
16
+ {
17
+ 'ASCII text' => ['plain text'.b, 'plain text'],
18
+ 'UTF-8 text' => ["\u2603", "\u2603"],
19
+ 'text with a known encoding' => ["caf\xE9".force_encoding(Encoding::ISO_8859_1), 'café'],
20
+ 'UTF-8 bytes in a binary string' => ["\u2603".b, "\u2603"],
21
+ 'invalid UTF-8 bytes in a binary string' => ["snowman \xFF".b, "snowman \uFFFD"],
22
+ 'invalid bytes in a UTF-8 string' => ["snowman \xFF", "snowman \uFFFD"]
23
+ }.each do |description, (value, expected)|
24
+ context "with #{description}" do
25
+ let(:header_value) { value }
26
+
27
+ it 'serializes the headers as valid UTF-8 in JSON' do
28
+ serialized = JSON.pretty_generate(page.to_hash)
29
+
30
+ expect(JSON.parse(serialized)['response']['headers']['x-olaf']).to eq(expected)
31
+ expect(page.to_hash['response']['headers']['x-olaf'].encoding).to eq(Encoding::UTF_8)
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'when preserving the original response' do
37
+ let(:header_value) { "\u2603".b }
38
+
39
+ it 'leaves the raw headers and their encoding unchanged' do
40
+ raw_headers = page.response.headers
41
+ raw_value = raw_headers['x-olaf']
42
+ raw_value.freeze
43
+ raw_headers.freeze
44
+
45
+ page.to_hash
46
+
47
+ expect(page.response.headers).to equal(raw_headers)
48
+ expect(raw_headers['x-olaf']).to equal(raw_value)
49
+ expect(raw_value.bytes).to eq([0xE2, 0x98, 0x83])
50
+ expect(raw_value.encoding).to eq(Encoding::BINARY)
51
+ end
52
+ end
53
+
54
+ context 'when the response was compressed' do
55
+ let(:header_value) { 'unused' }
56
+ let(:html) { '<html><head><title>Hello</title></head></html>' }
57
+
58
+ before do
59
+ stub_request(:get, url).to_return(
60
+ body: Zlib.gzip(html),
61
+ headers: { 'Content-Type' => 'text/html', 'Content-Encoding' => 'gzip' }
62
+ )
63
+ end
64
+
65
+ it 'keeps header values that are not strings, like the decompressed Content-Length' do
66
+ headers = page.to_hash['response']['headers']
67
+
68
+ expect(headers['content-length']).to eq(html.bytesize)
69
+ expect(JSON.parse(JSON.generate(page.to_hash))['response']['headers']['content-length']).to eq(html.bytesize)
70
+ end
71
+ end
72
+ end
73
+
4
74
  describe 'public API' do
5
75
  let(:html) do
6
76
  <<~HTML
@@ -12,8 +82,9 @@ describe MetaInspector::Document do
12
82
  end
13
83
  let(:page) { MetaInspector.new('https://example.com/', document: html) }
14
84
 
15
- it 'retains the deprecated feed and favicon methods' do
16
- expect(page).to respond_to(:feed, :favicon)
85
+ it 'no longer has the feed and favicon methods removed in 6.0' do
86
+ expect(page).not_to respond_to(:feed)
87
+ expect(page).not_to respond_to(:favicon)
17
88
  end
18
89
 
19
90
  it 'exposes feeds through the plural API' do
@@ -22,28 +93,33 @@ describe MetaInspector::Document do
22
93
  ])
23
94
  end
24
95
 
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
96
+ describe 'feed types' do
97
+ let(:html) do
98
+ <<~HTML
99
+ <html><head>
100
+ <link rel="alternate" type="application/rss+xml" title="RSS" href="/feed.xml">
101
+ <link rel="alternate" type="application/feed+json" title="JSON Feed" href="/feed.json">
102
+ <link rel="alternate" type="Application/Atom+XML" title="Atom" href="/atom.xml">
103
+ <link rel="alternate" type=" application/rss+xml ; charset=utf-8" title="RSS with parameter" href="/rss2.xml">
104
+ <link rel="alternate" type="application/rss+xml-ish" title="Unknown" href="/other.xml">
105
+ <link rel="alternate" type="text/html" title="Translation" href="/es/">
106
+ </head></html>
107
+ HTML
36
108
  end
37
109
 
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
110
+ it 'matches JSON Feed 1.1 and types with any case, whitespace or parameters, keeping the type as written' do
111
+ expect(page.feeds).to eq([
112
+ { title: 'RSS', href: 'https://example.com/feed.xml', type: 'application/rss+xml' },
113
+ { title: 'JSON Feed', href: 'https://example.com/feed.json', type: 'application/feed+json' },
114
+ { title: 'Atom', href: 'https://example.com/atom.xml', type: 'Application/Atom+XML' },
115
+ { title: 'RSS with parameter', href: 'https://example.com/rss2.xml',
116
+ type: ' application/rss+xml ; charset=utf-8' }
117
+ ])
42
118
  end
43
119
  end
44
120
 
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
121
+ it 'exposes the favicon through the images API' do
122
+ expect(page.images.favicon).to eq('https://example.com/icon.png')
47
123
  end
48
124
 
49
125
  it 'keeps the supported APIs and serialization free of deprecation warnings' do
@@ -57,38 +133,6 @@ describe MetaInspector::Document do
57
133
  end.not_to output.to_stderr
58
134
  end
59
135
 
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
136
  ['<html></html>', nil].each do |document|
93
137
  context "with #{document ? 'empty' : 'populated'} HTML" do
94
138
  before do
@@ -101,17 +145,57 @@ describe MetaInspector::Document do
101
145
  it "can call ##{method}" do
102
146
  fetched_page = MetaInspector.new('https://example.com/', download_images: false)
103
147
 
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
148
+ expect { fetched_page.public_send(method) }.not_to raise_error
109
149
  end
110
150
  end
111
151
  end
112
152
  end
113
153
  end
114
154
 
155
+ describe 'unknown options' do
156
+ let(:url) { 'http://unknown-options.example/' }
157
+
158
+ it 'raises ArgumentError naming the unknown options, before any request' do
159
+ expect { MetaInspector.new(url, timeout: 5, allow_redirection: false) }
160
+ .to raise_error(ArgumentError, 'Unknown options: :timeout, :allow_redirection')
161
+ expect(a_request(:any, url)).not_to have_been_made
162
+ end
163
+
164
+ it 'uses the singular for a single unknown option' do
165
+ expect { MetaInspector.new(url, timeout: 5) }.to raise_error(ArgumentError, 'Unknown option: :timeout')
166
+ end
167
+
168
+ it 'rejects String keys, which would otherwise be ignored and fetch the page' do
169
+ expect { MetaInspector.new(url, 'document' => '<title>Hi</title>') }
170
+ .to raise_error(ArgumentError, 'Unknown option: "document" (option keys must be Symbols)')
171
+ expect(a_request(:any, url)).not_to have_been_made
172
+ end
173
+
174
+ it 'rejects the top-level faraday_http_cache option removed in 6.0, pointing to faraday_options' do
175
+ expect { MetaInspector.new(url, faraday_http_cache: { store: {} }) }
176
+ .to raise_error(ArgumentError, 'Unknown option: :faraday_http_cache ' \
177
+ '(pass it inside faraday_options: { faraday_http_cache: ... })')
178
+ end
179
+
180
+ it 'accepts every documented option' do
181
+ options = {
182
+ connection_timeout: 1, read_timeout: 1, retries: 0, allow_redirections: false, head_first: false,
183
+ document: '<title>Hi</title>', headers: {}, encoding: nil, normalize_url: true,
184
+ allow_non_html_content: false, download_images: false, max_image_downloads: 0,
185
+ download_image_options: {}, fetch_all_image_meta: false, inline_image_data: false,
186
+ max_html_bytes: 100, url_filter: ->(_) { true }, faraday_options: {}
187
+ }
188
+
189
+ expect(MetaInspector.new(url, options).title).to eq('Hi')
190
+ end
191
+
192
+ it 'leaves the options hash it was given unchanged' do
193
+ options = { document: '<title>Hi</title>' }.freeze
194
+
195
+ expect { MetaInspector.new(url, options) }.not_to raise_error
196
+ end
197
+ end
198
+
115
199
  describe 'max_html_bytes option' do
116
200
  it 'rejects oversized supplied HTML before parsing' do
117
201
  expect do
@@ -161,6 +245,20 @@ describe MetaInspector::Document do
161
245
  it "should get the title" do
162
246
  expect(doc.title).to eq("Hello From Passed Html")
163
247
  end
248
+
249
+ it 'returns nil content type and response, since no request was made' do
250
+ expect do
251
+ expect(doc.content_type).to be_nil
252
+ expect(doc.response).to be_nil
253
+ end.not_to output.to_stderr
254
+ end
255
+
256
+ it 'serializes to_hash with nil content type and response' do
257
+ hash = doc.to_hash
258
+
259
+ expect(hash).to include('title' => 'Hello From Passed Html', 'content_type' => nil, 'response' => nil)
260
+ expect(hash['links']['internal']).to eq(['http://cnn.com/hello'])
261
+ end
164
262
  end
165
263
 
166
264
  it "should return a String as to_s" do
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe MetaInspector, 'content type normalization' do
4
+ let(:page_url) { 'http://example.com/content-type' }
5
+ let(:html) { '<html><head><title>Downloaded page</title></head></html>' }
6
+
7
+ [false, true].each do |head_first|
8
+ context "with head_first: #{head_first}" do
9
+ ['Text/HTML; charset=utf-8', 'text/html ; charset=utf-8', " \tTEXT/HTML\t; charset=utf-8"].each do |content_type|
10
+ it "parses HTML served as #{content_type.inspect}" do
11
+ stub_request(:head, page_url).to_return(headers: { 'Content-Type' => content_type }) if head_first
12
+ stub_request(:get, page_url).to_return(body: html, headers: { 'Content-Type' => content_type })
13
+
14
+ page = MetaInspector.new(page_url, head_first: head_first)
15
+
16
+ expect(page.title).to eq('Downloaded page')
17
+ expect(page.content_type).to eq('text/html')
18
+ expect(page.to_hash['content_type']).to eq('text/html')
19
+ expect(page.response.headers['content-type']).to eq(content_type)
20
+ end
21
+ end
22
+
23
+ it 'still rejects non-HTML content after normalization' do
24
+ stub_request(:head, page_url).to_return(headers: { 'Content-Type' => 'Application/PDF ; version=1.7' }) if head_first
25
+ get_request = stub_request(:get, page_url)
26
+ .to_return(body: 'PDF data', headers: { 'Content-Type' => 'Application/PDF ; version=1.7' })
27
+
28
+ expect do
29
+ MetaInspector.new(page_url, head_first: head_first)
30
+ end.to raise_error(MetaInspector::NonHtmlError,
31
+ 'The url provided contains application/pdf content instead of text/html content')
32
+
33
+ expect(get_request).not_to have_been_requested if head_first
34
+ end
35
+
36
+ it 'normalizes non-HTML content when explicitly allowed' do
37
+ stub_request(:head, page_url).to_return(headers: { 'Content-Type' => 'Text/Plain ; charset=utf-8' }) if head_first
38
+ stub_request(:get, page_url).to_return(body: 'Plain text', headers: { 'Content-Type' => 'Text/Plain ; charset=utf-8' })
39
+
40
+ page = MetaInspector.new(page_url, head_first: head_first, allow_non_html_content: true)
41
+
42
+ expect(page.to_s).to eq('Plain text')
43
+ expect(page.content_type).to eq('text/plain')
44
+ end
45
+ end
46
+ end
47
+ end
@@ -56,7 +56,7 @@ describe MetaInspector do
56
56
  let(:page) { MetaInspector.new('https://example.com/page', document: document) }
57
57
  let(:expected_link) do
58
58
  attributes = { title: 'Example', href: 'https://example.com/resource', type: 'application/rss+xml' }
59
- attributes[:rel] = rel unless method == :feeds
59
+ attributes[:rel] = rel.gsub(/\r\n?/, "\n") unless method == :feeds
60
60
  attributes
61
61
  end
62
62
 
@@ -68,9 +68,9 @@ describe MetaInspector do
68
68
  let(:rel) { value }
69
69
  let(:rel_attribute) { %(rel="#{rel}") }
70
70
 
71
- it "matches the complete token and preserves the link attributes" do
71
+ it "matches the complete token and preserves the parsed link attributes" do
72
72
  expect(page.public_send(method)).to eq([expected_link])
73
- expect(page.head_links.first[:rel]).to eq(rel)
73
+ expect(page.head_links.first[:rel]).to eq(rel.gsub(/\r\n?/, "\n"))
74
74
  end
75
75
  end
76
76
  end
@@ -78,7 +78,10 @@ describe MetaInspector do
78
78
  end
79
79
 
80
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>'
81
+ html = <<~HTML
82
+ <picture><source srcset="/10x10 2x"><img src="/fallback.jpg" width="20" height="20"></picture>
83
+ <img srcset="/100x100 1x">
84
+ HTML
82
85
  page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 1)
83
86
 
84
87
  expect(page.images.with_size).to eq([
@@ -89,6 +92,62 @@ describe MetaInspector do
89
92
  expect(WebMock).not_to have_requested(:get, 'http://example.com/100x100')
90
93
  end
91
94
 
95
+ describe 'ranking responsive images' do
96
+ before do
97
+ %w[small medium fallback].each do |name|
98
+ stub_request(:get, "http://example.com/#{name}.jpg").to_return(fixture_file('10x10.jpg.response'))
99
+ end
100
+ end
101
+
102
+ it 'downloads only the candidate with the largest width descriptor' do
103
+ html = '<img srcset="/small.jpg 400w, /100x100 1200w, /medium.jpg 800w">'
104
+ page = MetaInspector.new('http://example.com/', document: html)
105
+
106
+ expect(page.images.best).to eq('http://example.com/100x100')
107
+ expect(page.images.with_size).to eq([['http://example.com/100x100', 100, 100]])
108
+ expect(page.images.to_a.size).to eq(3)
109
+ expect(WebMock).to have_requested(:get, 'http://example.com/100x100').once
110
+ expect(WebMock).not_to have_requested(:get, 'http://example.com/small.jpg')
111
+ expect(WebMock).not_to have_requested(:get, 'http://example.com/medium.jpg')
112
+ end
113
+
114
+ it 'treats a picture as one image' do
115
+ html = <<~HTML
116
+ <picture>
117
+ <source media="(min-width: 800px)" srcset="/small.jpg 400w, /100x100 800w">
118
+ <img src="/fallback.jpg" srcset="/medium.jpg 2x">
119
+ </picture>
120
+ <img src="/10x10">
121
+ HTML
122
+ page = MetaInspector.new('http://example.com/', document: html)
123
+
124
+ expect(page.images.with_size).to eq([
125
+ ['http://example.com/100x100', 100, 100],
126
+ ['http://example.com/10x10', 10, 10]
127
+ ])
128
+ %w[small.jpg fallback.jpg medium.jpg].each do |path|
129
+ expect(WebMock).not_to have_requested(:get, "http://example.com/#{path}")
130
+ end
131
+ end
132
+
133
+ it 'downloads only the highest density candidate, counting src as 1x' do
134
+ html = '<img src="/small.jpg" srcset="/medium.jpg 1.5x, /100x100 2x">'
135
+ page = MetaInspector.new('http://example.com/', document: html)
136
+
137
+ expect(page.images.best).to eq('http://example.com/100x100')
138
+ expect(WebMock).not_to have_requested(:get, 'http://example.com/small.jpg')
139
+ expect(WebMock).not_to have_requested(:get, 'http://example.com/medium.jpg')
140
+ end
141
+
142
+ it 'keeps HTML dimensions when src is the ranked candidate' do
143
+ html = '<img src="/fallback.jpg" width="300" height="200" srcset="/fallback.jpg 1x, /small.jpg 0.5x">'
144
+ page = MetaInspector.new('http://example.com/', document: html)
145
+
146
+ expect(page.images.with_size).to eq([['http://example.com/fallback.jpg', 300, 200]])
147
+ expect(WebMock).not_to have_requested(:get, %r{\Ahttp://example\.com/.+\.jpg\z})
148
+ end
149
+ end
150
+
92
151
  describe "returns an Enumerable" do
93
152
  let(:page) { MetaInspector.new('https://twitter.com/markupvalidator') }
94
153
 
@@ -294,6 +353,28 @@ describe MetaInspector do
294
353
  stub_request(:get, 'http://example.com/remaining.jpg').to_return(status: 404)
295
354
  end
296
355
 
356
+ describe "default" do
357
+ let(:many_images) { (1..25).map { |i| %(<img src="/many/#{i}.jpg">) }.join }
358
+
359
+ before { stub_request(:get, %r{\Ahttp://example\.com/many/}).to_return(status: 404) }
360
+
361
+ it "should download at most 20 images" do
362
+ page = MetaInspector.new('http://example.com/', document: many_images)
363
+
364
+ expect(page.images.with_size.size).to eq(25)
365
+ expect(WebMock).to have_requested(:get, %r{\Ahttp://example\.com/many/}).times(20)
366
+ expect(WebMock).to have_requested(:get, 'http://example.com/many/20.jpg').once
367
+ expect(WebMock).not_to have_requested(:get, 'http://example.com/many/21.jpg')
368
+ end
369
+
370
+ it "should download every image when nil is passed explicitly" do
371
+ page = MetaInspector.new('http://example.com/', document: many_images, max_image_downloads: nil)
372
+
373
+ page.images.with_size
374
+ expect(WebMock).to have_requested(:get, %r{\Ahttp://example\.com/many/}).times(25)
375
+ end
376
+ end
377
+
297
378
  it "should download only the first images needing dimensions in document order" do
298
379
  page = MetaInspector.new('http://example.com/', document: html, max_image_downloads: 2)
299
380
 
@@ -58,7 +58,7 @@ describe MetaInspector do
58
58
  "http://international.com/faqs#cami%C3%83%C2%B3n",
59
59
  "http://international.com/search?q=cami%C3%83%C2%B3n",
60
60
  "http://international.com/search?q=espa%C3%83%C2%B1a#top",
61
- "http://international.com/index.php?q=espa%C3%83%C2%B1a&url=aHR0zZQ==&cntnt01pageid=21"
61
+ "http://international.com/index.php?q=espa%C3%83%C2%B1a&url=aHR0zZQ%3D%3D&cntnt01pageid=21"
62
62
  ])
63
63
 
64
64
  expect(m.links.external).to eq([
@@ -79,7 +79,7 @@ describe MetaInspector do
79
79
  "http://international.com/faqs#cami%C3%83%C2%B3n",
80
80
  "http://international.com/search?q=cami%C3%83%C2%B3n",
81
81
  "http://international.com/search?q=espa%C3%83%C2%B1a#top",
82
- "http://international.com/index.php?q=espa%C3%83%C2%B1a&url=aHR0zZQ==&cntnt01pageid=21"
82
+ "http://international.com/index.php?q=espa%C3%83%C2%B1a&url=aHR0zZQ%3D%3D&cntnt01pageid=21"
83
83
  ])
84
84
  end
85
85
 
@@ -215,6 +215,57 @@ describe MetaInspector do
215
215
  end
216
216
  end
217
217
 
218
+ describe 'Base element lookup' do
219
+ # Searching the whole document for <base> once per link, image, iframe or
220
+ # <link> made these methods take quadratic time on large pages (#573).
221
+ callers = {
222
+ 'links.all' => ->(page) { page.links.all },
223
+ 'images.to_a' => ->(page) { page.images.to_a },
224
+ 'iframe_sources' => ->(page) { page.iframe_sources },
225
+ 'head_links' => ->(page) { page.head_links },
226
+ 'feeds' => ->(page) { page.feeds }
227
+ }
228
+
229
+ { 'without a base element' => '', 'with a base element' => '<base href="/dir/">' }.each do |name, base|
230
+ context name do
231
+ let(:html) do
232
+ <<~HTML
233
+ <html><head>#{base}
234
+ <link rel="stylesheet" href="a.css"><link rel="stylesheet" href="b.css">
235
+ <link rel="alternate" type="application/rss+xml" href="a.xml">
236
+ <link rel="alternate" type="application/atom+xml" href="b.xml">
237
+ </head><body>
238
+ <a href="a">a</a><a href="b">b</a><a href="c">c</a>
239
+ <img src="a.png"><img src="b.png"><img srcset="c.png 1x, d.png 2x">
240
+ <iframe src="a.html"></iframe><iframe src="b.html"></iframe>
241
+ </body></html>
242
+ HTML
243
+ end
244
+ let(:page) { MetaInspector.new('http://example.com/', document: html) }
245
+
246
+ callers.each do |method, call|
247
+ it "searches the document for it only once for #{method}" do
248
+ allow(page.parsed).to receive(:search).and_call_original
249
+
250
+ call.call(page)
251
+
252
+ expect(page.parsed).to have_received(:search).with('base').once
253
+ end
254
+ end
255
+ end
256
+ end
257
+
258
+ it 'keeps following the page URL when there is no base element' do
259
+ page = MetaInspector.new('http://example.com/?utm_source=feed', document: '<html><body><a href="a">a</a></body></html>')
260
+
261
+ expect(page.links.base_url).to eq('http://example.com/?utm_source=feed')
262
+
263
+ page.untrack!
264
+
265
+ expect(page.links.base_url).to eq('http://example.com/')
266
+ end
267
+ end
268
+
218
269
  describe 'Non-HTTP links' do
219
270
  before(:each) do
220
271
  @m = MetaInspector.new('http://example.com/nonhttp')
@@ -231,6 +282,51 @@ describe MetaInspector do
231
282
  end
232
283
  end
233
284
 
285
+ describe 'Links inside inline SVG and MathML' do
286
+ let(:page) do
287
+ MetaInspector.new('http://example.com/', document: <<~HTML)
288
+ <html><body>
289
+ <a href="/before">text</a>
290
+ <svg viewBox="0 0 10 10"><a href="/svg-link"><circle r="5"/></a></svg>
291
+ <math><a href="/math-link">x</a></math>
292
+ <a href="/after">text</a>
293
+ </body></html>
294
+ HTML
295
+ end
296
+
297
+ it 'returns them with the HTML links, in document order' do
298
+ expect(page.links.raw).to eq(['/before', '/svg-link', '/math-link', '/after'])
299
+ end
300
+
301
+ it 'resolves them like any other link' do
302
+ expect(page.links.internal).to eq(['http://example.com/before',
303
+ 'http://example.com/svg-link',
304
+ 'http://example.com/math-link',
305
+ 'http://example.com/after'])
306
+ end
307
+ end
308
+
309
+ describe 'Query strings in links' do
310
+ let(:page) do
311
+ MetaInspector.new('http://example.com/dir/', document: <<~HTML)
312
+ <a href="/a?x=1;y=2&b[]=1&c=a%2fb&%61=1">relative</a>
313
+ <a href="http://other.com/a?x=1;y=2&b[]=1&c=a%2fb&%61=1">absolute</a>
314
+ <a href="b?q=café&r=a|b&s=50%">needs escaping</a>
315
+ <link rel="alternate" type="application/rss+xml" href="/feed?x=1;y=2">
316
+ HTML
317
+ end
318
+
319
+ it 'keeps reserved characters and escapes as written (#613)' do
320
+ expect(page.links.all).to eq(['http://example.com/a?x=1;y=2&b[]=1&c=a%2fb&%61=1',
321
+ 'http://other.com/a?x=1;y=2&b[]=1&c=a%2fb&%61=1',
322
+ 'http://example.com/dir/b?q=caf%C3%A9&r=a%7Cb&s=50%25'])
323
+ end
324
+
325
+ it 'applies the same rule to head links' do
326
+ expect(page.feeds.map { |feed| feed[:href] }).to eq(['http://example.com/feed?x=1;y=2'])
327
+ end
328
+ end
329
+
234
330
  describe 'Protocol-relative URLs' do
235
331
  before(:each) do
236
332
  @m_http = MetaInspector.new('http://protocol-relative.com')
@@ -14,9 +14,6 @@ describe MetaInspector do
14
14
  store
15
15
  end
16
16
 
17
- # The deprecation warning prints once per process, so each example starts as a fresh process would
18
- before { MetaInspector::Request.instance_variable_set(:@faraday_http_cache_deprecation_warned, nil) }
19
-
20
17
  it "caches responses with faraday_options[:faraday_http_cache]" do
21
18
  MetaInspector.new('http://example.com', faraday_options: { faraday_http_cache: { store: cache } })
22
19
 
@@ -39,31 +36,12 @@ describe MetaInspector do
39
36
  expect(cache_options).to eq({ store: cache })
40
37
  end
41
38
 
42
- it "still caches responses with the deprecated top-level faraday_http_cache option" do
43
- expect do
44
- MetaInspector.new('http://example.com', faraday_http_cache: { store: cache })
45
- end.to output.to_stderr
46
-
47
- expect(cache.keys).not_to be_empty
48
- end
49
-
50
- it "warns that the top-level faraday_http_cache option is deprecated" do
39
+ it "raises ArgumentError for the top-level faraday_http_cache option removed in 6.0" do
51
40
  expect do
52
41
  MetaInspector.new('http://example.com', faraday_http_cache: { store: cache })
53
- end.to output(/DEPRECATION.*faraday_http_cache.*faraday_options/).to_stderr
54
- end
55
-
56
- it "prints the deprecation warning only once per process" do
57
- expect do
58
- 3.times { MetaInspector.new('http://example.com', faraday_http_cache: { store: cache }) }
59
- end.to output(/\ADEPRECATION WARNING: [^\n]*\n\z/).to_stderr
60
- end
42
+ end.to raise_error(ArgumentError, /faraday_http_cache.*faraday_options/)
61
43
 
62
- it "raises ArgumentError when the cache is configured in both places" do
63
- expect do
64
- MetaInspector.new('http://example.com', faraday_http_cache: { store: cache },
65
- faraday_options: { faraday_http_cache: { store: cache } })
66
- end.to raise_error(ArgumentError, /faraday_http_cache/)
44
+ expect(cache).to be_empty
67
45
  end
68
46
  end
69
47
  end