metainspector 5.21.2 → 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.
@@ -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