summaryrefslogtreecommitdiffstats
path: root/spec/lib/link_details_extractor_spec.rb
blob: 599bc4e6de2f60f03111ea4f335c738e5d1f84d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe LinkDetailsExtractor do
  subject { described_class.new(original_url, html, nil) }

  let(:original_url) { 'https://example.com/dog.html?tracking=123' }

  describe '#canonical_url' do
    let(:html) { "<!doctype html><link rel='canonical' href='#{url}'>" }

    context 'when canonical URL points to the same host' do
      let(:url) { 'https://example.com/dog.html' }

      it 'ignores the canonical URLs' do
        expect(subject.canonical_url).to eq 'https://example.com/dog.html'
      end
    end

    context 'when canonical URL points to another host' do
      let(:url) { 'https://different.example.net/dog.html' }

      it 'ignores the canonical URLs' do
        expect(subject.canonical_url).to eq original_url
      end
    end

    context 'when canonical URL is set to "null"' do
      let(:url) { 'null' }

      it 'ignores the canonical URLs' do
        expect(subject.canonical_url).to eq original_url
      end
    end
  end

  context 'when only basic metadata is present' do
    let(:html) { <<~HTML }
      <!doctype html>
      <html lang="en">
      <head>
        <title>Man bites dog</title>
        <meta name="description" content="A dog&#39;s tale">
      </head>
      </html>
    HTML

    describe '#title' do
      it 'returns the title from title tag' do
        expect(subject.title).to eq 'Man bites dog'
      end
    end

    describe '#description' do
      it 'returns the description from meta tag' do
        expect(subject.description).to eq "A dog's tale"
      end
    end

    describe '#language' do
      it 'returns the language from lang attribute' do
        expect(subject.language).to eq 'en'
      end
    end
  end

  context 'when structured data is present' do
    let(:ld_json) do
      {
        '@context' => 'https://schema.org',
        '@type' => 'NewsArticle',
        'headline' => 'Man bites dog',
        'description' => "A dog's tale",
        'datePublished' => '2022-01-31T19:53:00+00:00',
        'author' => {
          '@type' => 'Organization',
          'name' => 'Charlie Brown',
        },
        'publisher' => {
          '@type' => 'NewsMediaOrganization',
          'name' => 'Pet News',
          'url' => 'https://example.com',
        },
      }.to_json
    end

    shared_examples 'structured data' do
      describe '#title' do
        it 'returns the title from structured data' do
          expect(subject.title).to eq 'Man bites dog'
        end
      end

      describe '#description' do
        it 'returns the description from structured data' do
          expect(subject.description).to eq "A dog's tale"
        end
      end

      describe '#published_at' do
        it 'returns the publicaton time from structured data' do
          expect(subject.published_at).to eq '2022-01-31T19:53:00+00:00'
        end
      end

      describe '#author_name' do
        it 'returns the author name from structured data' do
          expect(subject.author_name).to eq 'Charlie Brown'
        end
      end

      describe '#provider_name' do
        it 'returns the provider name from structured data' do
          expect(subject.provider_name).to eq 'Pet News'
        end
      end
    end

    context 'when is wrapped in CDATA tags' do
      let(:html) { <<~HTML }
        <!doctype html>
        <html>
          <head>
            <script type="application/ld+json">
              //<![CDATA[
              #{ld_json}
              //]]>
            </script>
          </head>
        </html>
      HTML

      include_examples 'structured data'
    end

    context 'with the first tag is invalid JSON' do
      let(:html) { <<~HTML }
        <!doctype html>
        <html>
        <body>
          <script type="application/ld+json">
            invalid LD+JSON
          </script>
          <script type="application/ld+json">
            #{ld_json}
          </script>
        </body>
        </html>
      HTML

      include_examples 'structured data'
    end

    context 'with preceding block of unsupported LD+JSON' do
      let(:html) { <<~HTML }
        <!doctype html>
        <html>
        <body>
          <script type="application/ld+json">
            [
              {
                "@context": "https://schema.org",
                "@type": "ItemList",
                "url": "https://example.com/cat.html",
                "name": "Man bites cat",
                "description": "A cat's tale"
              },
              {
                "@context": "https://schema.org",
                "@type": "BreadcrumbList",
                "itemListElement":[
                  {
                    "@type": "ListItem",
                    "position": 1,
                    "item": {
                      "@id": "https://www.example.com",
                      "name": "Cat News"
                    }
                  }
                ]
              }
            ]
          </script>
          <script type="application/ld+json">
            #{ld_json}
          </script>
        </body>
        </html>
      HTML

      include_examples 'structured data'
    end

    context 'with unsupported in same block LD+JSON' do
      let(:html) { <<~HTML }
        <!doctype html>
        <html>
        <body>
          <script type="application/ld+json">
            [
              {
                "@context": "https://schema.org",
                "@type": "ItemList",
                "url": "https://example.com/cat.html",
                "name": "Man bites cat",
                "description": "A cat's tale"
              },
              #{ld_json}
            ]
          </script>
        </body>
        </html>
      HTML

      include_examples 'structured data'
    end
  end

  context 'when Open Graph protocol data is present' do
    let(:html) { <<~HTML }
      <!doctype html>
      <html>
      <head>
        <meta property="og:url" content="https://example.com/dog.html">
        <meta property="og:title" content="Man bites dog">
        <meta property="og:description" content="A dog's tale">
        <meta property="article:published_time" content="2022-01-31T19:53:00+00:00">
        <meta property="og:author" content="Charlie Brown">
        <meta property="og:locale" content="en">
        <meta property="og:image" content="https://example.com/snoopy.jpg">
        <meta property="og:image:alt" content="A good boy">
        <meta property="og:site_name" content="Pet News">
      </head>
      </html>
    HTML

    describe '#canonical_url' do
      it 'returns the URL from Open Graph protocol data' do
        expect(subject.canonical_url).to eq 'https://example.com/dog.html'
      end
    end

    describe '#title' do
      it 'returns the title from Open Graph protocol data' do
        expect(subject.title).to eq 'Man bites dog'
      end
    end

    describe '#description' do
      it 'returns the description from Open Graph protocol data' do
        expect(subject.description).to eq "A dog's tale"
      end
    end

    describe '#published_at' do
      it 'returns the publicaton time from Open Graph protocol data' do
        expect(subject.published_at).to eq '2022-01-31T19:53:00+00:00'
      end
    end

    describe '#author_name' do
      it 'returns the author name from Open Graph protocol data' do
        expect(subject.author_name).to eq 'Charlie Brown'
      end
    end

    describe '#language' do
      it 'returns the language from Open Graph protocol data' do
        expect(subject.language).to eq 'en'
      end
    end

    describe '#image' do
      it 'returns the image from Open Graph protocol data' do
        expect(subject.image).to eq 'https://example.com/snoopy.jpg'
      end
    end

    describe '#image:alt' do
      it 'returns the image description from Open Graph protocol data' do
        expect(subject.image_alt).to eq 'A good boy'
      end
    end

    describe '#provider_name' do
      it 'returns the provider name from Open Graph protocol data' do
        expect(subject.provider_name).to eq 'Pet News'
      end
    end
  end
end