about summary refs log tree commit diff
path: root/spec/lib/link_details_extractor_spec.rb
blob: 7ea867c614a150ee875b261de7e7aac188df9730 (plain) (blame)
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
require 'rails_helper'

RSpec.describe LinkDetailsExtractor do
  let(:original_url) { '' }
  let(:html) { '' }
  let(:html_charset) { nil }

  subject { described_class.new(original_url, html, html_charset) }

  describe '#canonical_url' do
    let(:original_url) { 'https://foo.com/article?bar=baz123' }

    context 'when canonical URL points to another host' do
      let(:html) { '<!doctype html><link rel="canonical" href="https://bar.com/different-article" />' }

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

    context 'when canonical URL points to the same host' do
      let(:html) { '<!doctype html><link rel="canonical" href="https://foo.com/article" />' }

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

    context 'when canonical URL is set to "null"' do
      let(:html) { '<!doctype html><link rel="canonical" href="null" />' }

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

  context 'when structured data is present' do
    let(:original_url) { 'https://example.com/page.html' }

    context 'and is wrapped in CDATA tags' do
      let(:html) { <<-HTML }
<!doctype html>
<html>
<head>
  <script type="application/ld+json">
  //<![CDATA[
  {"@context":"http://schema.org","@type":"NewsArticle","mainEntityOfPage":"https://example.com/page.html","headline":"Foo","datePublished":"2022-01-31T19:53:00+00:00","url":"https://example.com/page.html","description":"Bar","author":{"@type":"Person","name":"Hoge"},"publisher":{"@type":"Organization","name":"Baz"}}
  //]]>
  </script>
</head>
</html>
      HTML

      describe '#title' do
        it 'returns the title from structured data' do
          expect(subject.title).to eq 'Foo'
        end
      end

      describe '#description' do
        it 'returns the description from structured data' do
          expect(subject.description).to eq 'Bar'
        end
      end

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

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

    context 'but the first tag is invalid JSON' do
      let(:html) { <<-HTML }
<!doctype html>
<html>
<body>
  <script type="application/ld+json">
    {
      "@context":"https://schema.org",
      "@type":"ItemList",
      "url":"https://example.com/page.html",
      "name":"Foo",
      "description":"Bar"
    },
    {
      "@context": "https://schema.org",
      "@type": "BreadcrumbList",
      "itemListElement":[
        {
          "@type":"ListItem",
          "position":1,
          "item":{
            "@id":"https://www.example.com",
            "name":"Baz"
          }
        }
      ]
    }
  </script>
  <script type="application/ld+json">
    {
      "@context":"https://schema.org",
      "@type":"NewsArticle",
      "mainEntityOfPage": {
        "@type":"WebPage",
        "@id": "http://example.com/page.html"
      },
      "headline": "Foo",
      "description": "Bar",
      "datePublished": "2022-01-31T19:46:00+00:00",
      "author": {
        "@type": "Organization",
        "name": "Hoge"
      },
      "publisher": {
        "@type": "NewsMediaOrganization",
        "name":"Baz",
        "url":"https://example.com/"
      }
    }
  </script>
</body>
</html>
      HTML

      describe '#title' do
        it 'returns the title from structured data' do
          expect(subject.title).to eq 'Foo'
        end
      end

      describe '#description' do
        it 'returns the description from structured data' do
          expect(subject.description).to eq 'Bar'
        end
      end

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

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