fetch_link_card_service_spec.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe FetchLinkCardService, type: :service do
  4. subject { described_class.new }
  5. let(:html) { '<!doctype html><title>Hello world</title>' }
  6. let(:oembed_cache) { nil }
  7. before do
  8. stub_request(:get, 'http://example.com/html').to_return(headers: { 'Content-Type' => 'text/html' }, body: html)
  9. stub_request(:get, 'http://example.com/not-found').to_return(status: 404, headers: { 'Content-Type' => 'text/html' }, body: html)
  10. stub_request(:get, 'http://example.com/text').to_return(status: 404, headers: { 'Content-Type' => 'text/plain' }, body: 'Hello')
  11. stub_request(:get, 'http://example.com/redirect').to_return(status: 302, headers: { 'Location' => 'http://example.com/html' })
  12. stub_request(:get, 'http://example.com/redirect-to-404').to_return(status: 302, headers: { 'Location' => 'http://example.com/not-found' })
  13. stub_request(:get, 'http://example.com/oembed?url=http://example.com/html').to_return(headers: { 'Content-Type' => 'application/json' }, body: '{ "version": "1.0", "type": "link", "title": "oEmbed title" }')
  14. stub_request(:get, 'http://example.com/oembed?format=json&url=http://example.com/html').to_return(headers: { 'Content-Type' => 'application/json' }, body: '{ "version": "1.0", "type": "link", "title": "oEmbed title" }')
  15. stub_request(:get, 'http://example.xn--fiqs8s')
  16. stub_request(:get, 'http://example.com/日本語')
  17. stub_request(:get, 'http://example.com/test?data=file.gpx%5E1')
  18. stub_request(:get, 'http://example.com/test-')
  19. stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt'))
  20. stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt'))
  21. stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
  22. stub_request(:get, 'http://example.com/windows-1251').to_return(request_fixture('windows-1251.txt'))
  23. stub_request(:get, 'http://example.com/low_confidence_latin1').to_return(request_fixture('low_confidence_latin1.txt'))
  24. Rails.cache.write('oembed_endpoint:example.com', oembed_cache) if oembed_cache
  25. subject.call(status)
  26. end
  27. context 'with a local status' do
  28. context 'with URL of a regular HTML page' do
  29. let(:status) { Fabricate(:status, text: 'http://example.com/html') }
  30. it 'creates preview card' do
  31. expect(status.preview_card).to_not be_nil
  32. expect(status.preview_card.url).to eq 'http://example.com/html'
  33. expect(status.preview_card.title).to eq 'Hello world'
  34. end
  35. end
  36. context 'with URL of a page with no title' do
  37. let(:status) { Fabricate(:status, text: 'http://example.com/html') }
  38. let(:html) { '<!doctype html><title></title>' }
  39. it 'does not create a preview card' do
  40. expect(status.preview_card).to be_nil
  41. end
  42. end
  43. context 'with a URL of a plain-text page' do
  44. let(:status) { Fabricate(:status, text: 'http://example.com/text') }
  45. it 'does not create a preview card' do
  46. expect(status.preview_card).to be_nil
  47. end
  48. end
  49. context 'with multiple URLs' do
  50. let(:status) { Fabricate(:status, text: 'ftp://example.com http://example.com/html http://example.com/text') }
  51. it 'fetches the first valid URL' do
  52. expect(a_request(:get, 'http://example.com/html')).to have_been_made
  53. end
  54. it 'does not fetch the second valid URL' do
  55. expect(a_request(:get, 'http://example.com/text/')).to_not have_been_made
  56. end
  57. end
  58. context 'with a redirect URL' do
  59. let(:status) { Fabricate(:status, text: 'http://example.com/redirect') }
  60. it 'follows redirect' do
  61. expect(a_request(:get, 'http://example.com/redirect')).to have_been_made.once
  62. expect(a_request(:get, 'http://example.com/html')).to have_been_made.once
  63. end
  64. it 'creates preview card' do
  65. expect(status.preview_card).to_not be_nil
  66. expect(status.preview_card.url).to eq 'http://example.com/html'
  67. expect(status.preview_card.title).to eq 'Hello world'
  68. end
  69. end
  70. context 'with a broken redirect URL' do
  71. let(:status) { Fabricate(:status, text: 'http://example.com/redirect-to-404') }
  72. it 'follows redirect' do
  73. expect(a_request(:get, 'http://example.com/redirect-to-404')).to have_been_made.once
  74. expect(a_request(:get, 'http://example.com/not-found')).to have_been_made.once
  75. end
  76. it 'does not create a preview card' do
  77. expect(status.preview_card).to be_nil
  78. end
  79. end
  80. context 'with a 404 URL' do
  81. let(:status) { Fabricate(:status, text: 'http://example.com/not-found') }
  82. it 'does not create a preview card' do
  83. expect(status.preview_card).to be_nil
  84. end
  85. end
  86. context 'with an IDN URL' do
  87. let(:status) { Fabricate(:status, text: 'Check out http://example.中国') }
  88. it 'fetches the URL' do
  89. expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.once
  90. end
  91. end
  92. context 'with a URL of a page in Shift JIS encoding' do
  93. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') }
  94. it 'decodes the HTML' do
  95. expect(status.preview_cards.first.title).to eq('SJISのページ')
  96. end
  97. end
  98. context 'with a URL of a page in Shift JIS encoding labeled as UTF-8' do
  99. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') }
  100. it 'decodes the HTML despite the wrong charset header' do
  101. expect(status.preview_cards.first.title).to eq('SJISのページ')
  102. end
  103. end
  104. context 'with a URL of a page in KOI8-R encoding' do
  105. let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') }
  106. it 'decodes the HTML' do
  107. expect(status.preview_cards.first.title).to eq('Московя начинаетъ только въ XVI ст. привлекать внимане иностранцевъ.')
  108. end
  109. end
  110. context 'with a URL of a page in Windows-1251 encoding' do
  111. let(:status) { Fabricate(:status, text: 'Check out http://example.com/windows-1251') }
  112. it 'decodes the HTML' do
  113. expect(status.preview_cards.first.title).to eq('сэмпл текст')
  114. end
  115. end
  116. context 'with a URL of a page in ISO-8859-1 encoding, that charlock_holmes cannot detect' do
  117. let(:status) { Fabricate(:status, text: 'Check out http://example.com/low_confidence_latin1') }
  118. it 'decodes the HTML' do
  119. expect(status.preview_card.title).to eq("Tofu á l'orange")
  120. end
  121. end
  122. context 'with a Japanese path URL' do
  123. let(:status) { Fabricate(:status, text: 'テストhttp://example.com/日本語') }
  124. it 'fetches the URL' do
  125. expect(a_request(:get, 'http://example.com/日本語')).to have_been_made.once
  126. end
  127. end
  128. context 'with a hyphen-suffixed URL' do
  129. let(:status) { Fabricate(:status, text: 'test http://example.com/test-') }
  130. it 'fetches the URL' do
  131. expect(a_request(:get, 'http://example.com/test-')).to have_been_made.once
  132. end
  133. end
  134. context 'with a caret-suffixed URL' do
  135. let(:status) { Fabricate(:status, text: 'test http://example.com/test?data=file.gpx^1') }
  136. it 'fetches the URL' do
  137. expect(a_request(:get, 'http://example.com/test?data=file.gpx%5E1')).to have_been_made.once
  138. end
  139. it 'does not strip the caret before fetching' do
  140. expect(a_request(:get, 'http://example.com/test?data=file.gpx')).to_not have_been_made
  141. end
  142. end
  143. context 'with a non-isolated URL' do
  144. let(:status) { Fabricate(:status, text: 'testhttp://example.com/sjis') }
  145. it 'does not fetch URLs not isolated from their surroundings' do
  146. expect(a_request(:get, 'http://example.com/sjis')).to_not have_been_made
  147. end
  148. end
  149. context 'with an URL too long for PostgreSQL unique indexes' do
  150. let(:url) { "http://example.com/#{'a' * 2674}" }
  151. let(:status) { Fabricate(:status, text: url) }
  152. it 'does not fetch the URL' do
  153. expect(a_request(:get, url)).to_not have_been_made
  154. end
  155. it 'does not create a preview card' do
  156. expect(status.preview_card).to be_nil
  157. end
  158. end
  159. context 'with a URL of a page with oEmbed support' do
  160. let(:html) { '<!doctype html><title>Hello world</title><link rel="alternate" type="application/json+oembed" href="http://example.com/oembed?url=http://example.com/html">' }
  161. let(:status) { Fabricate(:status, text: 'http://example.com/html') }
  162. it 'fetches the oEmbed URL' do
  163. expect(a_request(:get, 'http://example.com/oembed?url=http://example.com/html')).to have_been_made.once
  164. end
  165. it 'creates preview card' do
  166. expect(status.preview_card).to_not be_nil
  167. expect(status.preview_card.url).to eq 'http://example.com/html'
  168. expect(status.preview_card.title).to eq 'oEmbed title'
  169. end
  170. context 'when oEmbed endpoint cache populated' do
  171. let(:oembed_cache) { { endpoint: 'http://example.com/oembed?format=json&url={url}', format: :json } }
  172. it 'uses the cached oEmbed response' do
  173. expect(a_request(:get, 'http://example.com/oembed?url=http://example.com/html')).to_not have_been_made
  174. expect(a_request(:get, 'http://example.com/oembed?format=json&url=http://example.com/html')).to have_been_made
  175. end
  176. it 'creates preview card' do
  177. expect(status.preview_card).to_not be_nil
  178. expect(status.preview_card.url).to eq 'http://example.com/html'
  179. expect(status.preview_card.title).to eq 'oEmbed title'
  180. end
  181. end
  182. # If the original HTML URL for whatever reason (e.g. DOS protection) redirects to
  183. # an error page, we can still use the cached oEmbed but should not use the
  184. # redirect URL on the card.
  185. context 'when oEmbed endpoint cache populated but page returns 404' do
  186. let(:status) { Fabricate(:status, text: 'http://example.com/redirect-to-404') }
  187. let(:oembed_cache) { { endpoint: 'http://example.com/oembed?url=http://example.com/html', format: :json } }
  188. it 'uses the cached oEmbed response' do
  189. expect(a_request(:get, 'http://example.com/oembed?url=http://example.com/html')).to have_been_made
  190. end
  191. it 'creates preview card' do
  192. expect(status.preview_card).to_not be_nil
  193. expect(status.preview_card.title).to eq 'oEmbed title'
  194. end
  195. it 'uses the original URL' do
  196. expect(status.preview_card&.url).to eq 'http://example.com/redirect-to-404'
  197. end
  198. end
  199. end
  200. end
  201. context 'with a remote status' do
  202. let(:status) do
  203. Fabricate(:status, account: Fabricate(:account, domain: 'example.com'), text: <<-TEXT)
  204. Habt ihr ein paar gute Links zu <a>foo</a>
  205. #<span class="tag"><a href="https://quitter.se/tag/wannacry" target="_blank" rel="tag noopener noreferrer" title="https://quitter.se/tag/wannacry">Wannacry</a></span> herumfliegen?
  206. Ich will mal unter <br> <a href="http://example.com/not-found" target="_blank" rel="noopener noreferrer" title="http://example.com/not-found">http://example.com/not-found</a> was sammeln. !
  207. <a href="http://sn.jonkman.ca/group/416/id" target="_blank" rel="noopener noreferrer" title="http://sn.jonkman.ca/group/416/id">security</a>&nbsp;
  208. TEXT
  209. end
  210. it 'parses out URLs' do
  211. expect(a_request(:get, 'http://example.com/not-found')).to have_been_made.once
  212. end
  213. it 'ignores URLs to hashtags' do
  214. expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made
  215. end
  216. end
  217. end