metainspector 5.21.1 → 5.22.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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/README.md +207 -36
  4. data/lib/meta_inspector/attempt_timeout.rb +40 -0
  5. data/lib/meta_inspector/document.rb +27 -4
  6. data/lib/meta_inspector/html_encoding.rb +177 -0
  7. data/lib/meta_inspector/http_session.rb +33 -5
  8. data/lib/meta_inspector/image_downloader.rb +2 -0
  9. data/lib/meta_inspector/limited_response.rb +125 -0
  10. data/lib/meta_inspector/parser.rb +112 -1
  11. data/lib/meta_inspector/parsers/head_links.rb +10 -3
  12. data/lib/meta_inspector/parsers/links.rb +10 -4
  13. data/lib/meta_inspector/parsers/meta_tags.rb +6 -2
  14. data/lib/meta_inspector/parsers/texts.rb +2 -2
  15. data/lib/meta_inspector/request.rb +45 -30
  16. data/lib/meta_inspector/url.rb +32 -5
  17. data/lib/meta_inspector/url_policy.rb +1 -1
  18. data/lib/meta_inspector/version.rb +1 -1
  19. data/meta_inspector.gemspec +1 -1
  20. data/spec/document_spec.rb +118 -0
  21. data/spec/meta_inspector/content_type_spec.rb +47 -0
  22. data/spec/meta_inspector/head_links_spec.rb +3 -3
  23. data/spec/meta_inspector/links_spec.rb +98 -2
  24. data/spec/meta_inspector/meta_tags_spec.rb +53 -0
  25. data/spec/meta_inspector/texts_spec.rb +82 -0
  26. data/spec/meta_inspector/timeouts_spec.rb +107 -0
  27. data/spec/noscript_spec.rb +179 -0
  28. data/spec/parser_spec.rb +382 -2
  29. data/spec/query_string_spec.rb +125 -0
  30. data/spec/request_errors_spec.rb +91 -0
  31. data/spec/request_spec.rb +28 -10
  32. data/spec/response_encoding_spec.rb +487 -0
  33. data/spec/response_size_spec.rb +176 -0
  34. data/spec/url_filter_spec.rb +2 -1
  35. data/spec/url_spec.rb +37 -0
  36. metadata +12 -2
@@ -1,6 +1,38 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MetaInspector do
4
+ describe 'headings containing paragraphs' do
5
+ (1..6).each do |level|
6
+ {
7
+ '<p>Hello</p>' => 'Hello',
8
+ 'Hi <p>there</p>' => 'Hi there',
9
+ '<p>Hello <em>there</em></p> friend' => 'Hello there friend'
10
+ }.each do |content, text|
11
+ it "preserves #{content.inspect} inside h#{level}" do
12
+ html = "<h#{level}>#{content}</h#{level}><p>Outside</p>"
13
+ page = MetaInspector.new('http://example.com', document: html)
14
+
15
+ expect(page.public_send("h#{level}")).to eq([text])
16
+ end
17
+ end
18
+ end
19
+
20
+ it 'uses the complete first heading as the best title' do
21
+ page = MetaInspector.new('http://example.com', document: '<h1>Hi <p>there</p></h1><h1>Later</h1>')
22
+
23
+ expect(page.best_title).to eq('Hi there')
24
+ expect(page.to_hash['h1']).to eq(['Hi there', 'Later'])
25
+ end
26
+
27
+ it 'preserves paragraph content in downloaded headings' do
28
+ stub_request(:get, 'http://example.com/').to_return(
29
+ body: '<h1><p>Hello</p></h1>', headers: { 'Content-Type' => 'text/html' }
30
+ )
31
+
32
+ expect(MetaInspector.new('http://example.com').h1).to eq(['Hello'])
33
+ end
34
+ end
35
+
4
36
  describe '#language' do
5
37
  ['en', 'en-US', 'zh-Hant-TW'].each do |language|
6
38
  it "should return the declared language #{language.inspect}" do
@@ -50,6 +82,56 @@ describe MetaInspector do
50
82
  expect(page.title).to eq('An example page')
51
83
  end
52
84
 
85
+ describe '#title' do
86
+ let(:page) do
87
+ MetaInspector.new('http://example.com/', document: html)
88
+ end
89
+
90
+ context 'when the head contains multiple titles' do
91
+ let(:html) { '<html><head><title>One</title><title>Two</title></head></html>' }
92
+
93
+ it 'should return only the first title' do
94
+ expect(page.title).to eq('One')
95
+ end
96
+
97
+ it 'should include only the first title in to_hash' do
98
+ expect(page.to_hash['title']).to eq('One')
99
+ end
100
+ end
101
+
102
+ context 'when the first title is empty' do
103
+ let(:html) { '<html><head><title></title><title>Two</title></head></html>' }
104
+
105
+ it 'should return an empty string without falling back to a later title' do
106
+ expect(page.title).to eq('')
107
+ end
108
+ end
109
+
110
+ context 'when the title contains whitespace and entities' do
111
+ let(:html) { '<html><head><title> One &amp; Two </title></head></html>' }
112
+
113
+ it 'should preserve whitespace and decode entities' do
114
+ expect(page.title).to eq(' One & Two ')
115
+ end
116
+ end
117
+
118
+ context 'when there is no title' do
119
+ let(:html) { '<html><head></head><body></body></html>' }
120
+
121
+ it 'should return an empty string' do
122
+ expect(page.title).to eq('')
123
+ end
124
+ end
125
+
126
+ context 'when the title is only in the body' do
127
+ let(:html) { '<html><head></head><body><title>Body title</title></body></html>' }
128
+
129
+ it 'should return an empty string' do
130
+ expect(page.title).to eq('')
131
+ end
132
+ end
133
+ end
134
+
53
135
  describe "#h1" do
54
136
  it "should find h1 content" do
55
137
  page = MetaInspector.new('http://example.com/headings')
@@ -0,0 +1,107 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Timeouts and retries' do
4
+ let(:page_url) { 'http://timeouts.example.com/' }
5
+ let(:html) { { status: 200, headers: { 'Content-Type' => 'text/html' }, body: '<title>Hi</title>' } }
6
+
7
+ describe 'connection timeouts' do
8
+ it 'raises MetaInspector::TimeoutError, keeping the original error as the cause' do
9
+ stub_request(:get, page_url).to_raise(Net::OpenTimeout.new('Failed to open TCP connection'))
10
+
11
+ expect do
12
+ MetaInspector.new(page_url, retries: 0)
13
+ end.to raise_error(MetaInspector::TimeoutError, /Failed to open TCP connection/) { |error|
14
+ expect(error.cause).to be_a(Faraday::TimeoutError)
15
+ expect(error.cause.wrapped_exception).to be_a(Net::OpenTimeout)
16
+ }
17
+ end
18
+
19
+ it 'retries them' do
20
+ request = stub_request(:get, page_url).to_raise(Net::OpenTimeout).then.to_return(html)
21
+
22
+ page = MetaInspector.new(page_url, retries: 1)
23
+
24
+ expect(page.title).to eq('Hi')
25
+ expect(request).to have_been_requested.twice
26
+ end
27
+
28
+ it 'raises MetaInspector::TimeoutError after exhausting the retries' do
29
+ request = stub_request(:get, page_url).to_raise(Net::OpenTimeout)
30
+
31
+ expect do
32
+ MetaInspector.new(page_url, retries: 2)
33
+ end.to raise_error(MetaInspector::TimeoutError)
34
+
35
+ expect(request).to have_been_requested.times(3)
36
+ end
37
+
38
+ it 'applies to HEAD requests too' do
39
+ head_request = stub_request(:head, page_url).to_raise(Net::OpenTimeout)
40
+ get_request = stub_request(:get, page_url).to_return(html)
41
+
42
+ expect do
43
+ MetaInspector.new(page_url, head_first: true, retries: 1)
44
+ end.to raise_error(MetaInspector::TimeoutError)
45
+
46
+ expect(head_request).to have_been_requested.twice
47
+ expect(get_request).not_to have_been_requested
48
+ end
49
+ end
50
+
51
+ describe 'other connection failures' do
52
+ it 'still raise MetaInspector::RequestError without retrying' do
53
+ request = stub_request(:get, page_url).to_raise(Errno::ECONNREFUSED)
54
+
55
+ expect do
56
+ MetaInspector.new(page_url, retries: 2)
57
+ end.to raise_error(MetaInspector::RequestError)
58
+
59
+ expect(request).to have_been_requested.once
60
+ end
61
+ end
62
+
63
+ describe 'deadline for each attempt' do
64
+ # The deadline is connection_timeout + read_timeout + 1 seconds.
65
+ let(:options) { { connection_timeout: 0.05, read_timeout: 0.05 } }
66
+
67
+ it 'gives every retry its own deadline' do
68
+ calls = 0
69
+ request = stub_request(:get, page_url).to_return do
70
+ calls += 1
71
+ # Ends with a read timeout after more than half the deadline.
72
+ sleep 0.7
73
+ raise Net::ReadTimeout if calls == 1
74
+ html
75
+ end
76
+
77
+ page = MetaInspector.new(page_url, **options, retries: 1)
78
+
79
+ expect(page.title).to eq('Hi')
80
+ expect(request).to have_been_requested.twice
81
+ end
82
+
83
+ # A server that keeps an attempt busy until the deadline, such as one that
84
+ # streams forever, would hold the fetch for every retry (#188).
85
+ it 'raises MetaInspector::TimeoutError without retrying when an attempt runs out of time' do
86
+ request = stub_request(:get, page_url).to_return { sleep 1.5; html }
87
+
88
+ expect do
89
+ MetaInspector.new(page_url, **options, retries: 2)
90
+ end.to raise_error(MetaInspector::TimeoutError, /execution expired/)
91
+
92
+ expect(request).to have_been_requested.once
93
+ end
94
+
95
+ it 'applies to HEAD requests too' do
96
+ head_request = stub_request(:head, page_url).to_return { sleep 1.5; html }
97
+ get_request = stub_request(:get, page_url).to_return(html)
98
+
99
+ expect do
100
+ MetaInspector.new(page_url, **options, head_first: true, retries: 2)
101
+ end.to raise_error(MetaInspector::TimeoutError)
102
+
103
+ expect(head_request).to have_been_requested.once
104
+ expect(get_request).not_to have_been_requested
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,179 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'noscript content' do
4
+ let(:page) { MetaInspector.new('https://example.com/', document: html, download_images: false) }
5
+
6
+ describe 'in the head' do
7
+ [
8
+ '<img src="/pixel.gif">',
9
+ '<iframe src="/tracking"></iframe>',
10
+ '<a href="/fallback">Fallback</a>',
11
+ '<div>Enable JavaScript</div>',
12
+ 'Enable JavaScript'
13
+ ].each do |content|
14
+ context "containing #{content}" do
15
+ let(:html) do
16
+ <<~HTML
17
+ <html><head>
18
+ <noscript>#{content}</noscript>
19
+ <title>My page</title>
20
+ <link rel="canonical" href="https://example.com/">
21
+ <link rel="stylesheet" href="/style.css">
22
+ </head><body><h1>Heading</h1></body></html>
23
+ HTML
24
+ end
25
+
26
+ it 'preserves the title and head links after the fallback' do
27
+ expect(page.title).to eq('My page')
28
+ expect(page.canonicals).to eq([{ rel: 'canonical', href: 'https://example.com/' }])
29
+ expect(page.stylesheets).to eq([{ rel: 'stylesheet', href: 'https://example.com/style.css' }])
30
+ expect(page.head_links.size).to eq(2)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ describe 'with an unclosed iframe' do
37
+ ['<iframe src="/tracking">', '<iframe src="/tracking" />'].each do |iframe|
38
+ ['head', 'body'].each do |location|
39
+ context "in the #{location}, written as #{iframe}" do
40
+ let(:html) do
41
+ fallback = "<noscript>#{iframe}</noscript>"
42
+ <<~HTML
43
+ <html><head>
44
+ #{fallback if location == 'head'}
45
+ <title>My page</title>
46
+ </head><body>
47
+ #{fallback if location == 'body'}
48
+ <h1>Heading</h1>
49
+ <a href="/about">About</a>
50
+ <img src="/photo.jpg">
51
+ </body></html>
52
+ HTML
53
+ end
54
+
55
+ it 'preserves the rest of the page and the fallback iframe' do
56
+ expect(page.title).to eq('My page')
57
+ expect(page.h1).to eq(['Heading'])
58
+ expect(page.links.raw).to eq(['/about'])
59
+ expect(page.images.to_a).to eq(['https://example.com/photo.jpg'])
60
+ expect(page.iframes).to eq([{ src: '/tracking' }])
61
+ expect(page.iframe_sources).to eq(['https://example.com/tracking'])
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ describe 'fallback extraction' do
69
+ let(:html) do
70
+ <<~HTML
71
+ <html><head>
72
+ <base href="https://example.com/assets/">
73
+ <link rel="preload" href="style.css" as="style">
74
+ <noscript>
75
+ <link rel="stylesheet" href="style.css">
76
+ <link rel="alternate" type="application/rss+xml" href="feed.xml">
77
+ <link rel="icon" href="icon.png">
78
+ <img src="pixel.gif" width="1" height="1">
79
+ </noscript>
80
+ <title>Café &amp; news</title>
81
+ </head><body>
82
+ <a href="before">Before</a>
83
+ <noscript>
84
+ <h1>Café &amp; news</h1>
85
+ <a href="fallback?x=1&amp;y=2">Fallback</a>
86
+ <img src="photo.jpg" srcset="photo-2x.jpg 2x" width="640" height="480">
87
+ <iframe src="video?x=1&amp;y=2" title="Café &amp; news"></iframe>
88
+ <link rel="stylesheet" href="body.css">
89
+ </noscript>
90
+ <a href="after">After</a>
91
+ <img src="after.jpg" width="100" height="100">
92
+ <iframe src="after-video"></iframe>
93
+ </body></html>
94
+ HTML
95
+ end
96
+
97
+ it 'preserves head fallback stylesheets without including body stylesheets' do
98
+ expect(page.stylesheets).to eq([{ rel: 'stylesheet', href: 'https://example.com/assets/style.css' }])
99
+ expect(page.head_links.size).to eq(4)
100
+ expect(page.feeds).to eq([
101
+ { title: nil, href: 'https://example.com/assets/feed.xml', type: 'application/rss+xml' }
102
+ ])
103
+ expect(page.images.favicon).to eq('https://example.com/assets/icon.png')
104
+ end
105
+
106
+ it 'preserves fallback links and their order, resolving against the document base' do
107
+ expect(page.links.raw).to eq(['before', 'fallback?x=1&y=2', 'after'])
108
+ expect(page.links.all).to eq([
109
+ 'https://example.com/assets/before', 'https://example.com/assets/fallback?x=1&y=2',
110
+ 'https://example.com/assets/after'
111
+ ])
112
+ end
113
+
114
+ it 'preserves fallback images, responsive candidates and dimensions' do
115
+ expect(page.images.to_a).to eq(%w[pixel.gif photo.jpg photo-2x.jpg after.jpg].map do |name|
116
+ "https://example.com/assets/#{name}"
117
+ end)
118
+ expect(page.images.with_size.first).to eq(['https://example.com/assets/photo.jpg', 640, 480])
119
+ end
120
+
121
+ it 'preserves fallback iframes and decodes entities once' do
122
+ expect(page.iframes).to eq([
123
+ { src: 'video?x=1&y=2', title: 'Café & news' }, { src: 'after-video' }
124
+ ])
125
+ expect(page.iframe_sources).to eq([
126
+ 'https://example.com/assets/video?x=1&y=2', 'https://example.com/assets/after-video'
127
+ ])
128
+ expect(page.title).to eq('Café & news')
129
+ expect(page.h1).to eq(['Café & news'])
130
+ end
131
+
132
+ it 'keeps fallback nodes in the public DOM and preserves the frozen source' do
133
+ original = html.dup
134
+ html.freeze
135
+
136
+ expect(page.parsed.css('head noscript img').size).to eq(1)
137
+ expect(page.parsed.css('body noscript a').size).to eq(1)
138
+ expect(page.to_s).to equal(html)
139
+ expect(html).to eq(original)
140
+ end
141
+
142
+ it 'applies the same parsing to a downloaded document' do
143
+ stub_request(:get, 'https://example.com/').to_return(body: html, headers: { 'Content-Type' => 'text/html' })
144
+ downloaded = MetaInspector.new('https://example.com/', download_images: false)
145
+
146
+ expect(downloaded.title).to eq(page.title)
147
+ expect(downloaded.head_links).to eq(page.head_links)
148
+ expect(downloaded.to_hash['images']).to eq(page.images.to_a)
149
+ end
150
+ end
151
+
152
+ it 'does not treat noscript-like text in scripts or comments as fallback markup' do
153
+ html = <<~HTML
154
+ <head><script>var fallback = '<noscript><img src="/fake.jpg"></noscript>';</script>
155
+ <!-- <noscript><img src="/comment.jpg"></noscript> -->
156
+ <title>My page</title></head><body><img src="/photo.jpg"></body>
157
+ HTML
158
+ page = MetaInspector.new('https://example.com/', document: html, download_images: false)
159
+
160
+ expect(page.title).to eq('My page')
161
+ expect(page.images.to_a).to eq(['https://example.com/photo.jpg'])
162
+ end
163
+
164
+ it 'does not introduce an attribute count limit for fallback content' do
165
+ attributes = (1..500).map { |i| "data-#{i}=\"#{i}\"" }.join(' ')
166
+ html = "<noscript><a href=\"/fallback\" #{attributes}>Fallback</a></noscript>"
167
+ page = MetaInspector.new('https://example.com/', document: html)
168
+
169
+ expect(page.links.raw).to eq(['/fallback'])
170
+ end
171
+
172
+ it 'wraps HTML5 depth-limit errors in fallback fragments' do
173
+ html = '<noscript>' + '<div><h1><p>' * 220 + 'deep</noscript><h1>After</h1>'
174
+
175
+ expect do
176
+ MetaInspector.new('https://example.com/', document: html)
177
+ end.to raise_error(MetaInspector::ParserError, /tree depth limit/)
178
+ end
179
+ end