fetch_link_card_service_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. require 'rails_helper'
  2. RSpec.describe FetchLinkCardService, type: :service do
  3. subject { described_class.new }
  4. before do
  5. stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt'))
  6. stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt'))
  7. stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt'))
  8. stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
  9. stub_request(:get, 'http://example.com/日本語').to_return(request_fixture('sjis.txt'))
  10. stub_request(:get, 'https://github.com/qbi/WannaCry').to_return(status: 404)
  11. stub_request(:get, 'http://example.com/test?data=file.gpx%5E1').to_return(status: 200)
  12. stub_request(:get, 'http://example.com/test-').to_return(request_fixture('idn.txt'))
  13. stub_request(:get, 'http://example.com/windows-1251').to_return(request_fixture('windows-1251.txt'))
  14. stub_request(:get, 'http://example.com/low_confidence_latin1').to_return(request_fixture('low_confidence_latin1.txt'))
  15. subject.call(status)
  16. end
  17. context 'in a local status' do
  18. context do
  19. let(:status) { Fabricate(:status, text: 'Check out http://example.中国') }
  20. it 'works with IDN URLs' do
  21. expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once
  22. end
  23. end
  24. context do
  25. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') }
  26. it 'works with SJIS' do
  27. expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once
  28. expect(status.preview_cards.first.title).to eq("SJISのページ")
  29. end
  30. end
  31. context do
  32. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') }
  33. it 'works with SJIS even with wrong charset header' do
  34. expect(a_request(:get, 'http://example.com/sjis_with_wrong_charset')).to have_been_made.at_least_once
  35. expect(status.preview_cards.first.title).to eq("SJISのページ")
  36. end
  37. end
  38. context do
  39. let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') }
  40. it 'works with koi8-r' do
  41. expect(a_request(:get, 'http://example.com/koi8-r')).to have_been_made.at_least_once
  42. expect(status.preview_cards.first.title).to eq("Московя начинаетъ только въ XVI ст. привлекать внимане иностранцевъ.")
  43. end
  44. end
  45. context do
  46. let(:status) { Fabricate(:status, text: 'Check out http://example.com/windows-1251') }
  47. it 'works with windows-1251' do
  48. expect(a_request(:get, 'http://example.com/windows-1251')).to have_been_made.at_least_once
  49. expect(status.preview_cards.first.title).to eq('сэмпл текст')
  50. end
  51. end
  52. context 'with a URL of a page in ISO-8859-1 encoding, that charlock_holmes cannot detect' do
  53. let(:status) { Fabricate(:status, text: 'Check out http://example.com/low_confidence_latin1') }
  54. it 'decodes the HTML' do
  55. expect(status.preview_card.title).to eq("Tofu á l'orange")
  56. end
  57. end
  58. context 'with a Japanese path URL' do
  59. let(:status) { Fabricate(:status, text: 'テストhttp://example.com/日本語') }
  60. it 'works with Japanese path string' do
  61. expect(a_request(:get, 'http://example.com/日本語')).to have_been_made.at_least_once
  62. expect(status.preview_cards.first.title).to eq("SJISのページ")
  63. end
  64. end
  65. context do
  66. let(:status) { Fabricate(:status, text: 'test http://example.com/test-') }
  67. it 'works with a URL ending with a hyphen' do
  68. expect(a_request(:get, 'http://example.com/test-')).to have_been_made.at_least_once
  69. end
  70. end
  71. context do
  72. let(:status) { Fabricate(:status, text: 'testhttp://example.com/sjis') }
  73. it 'does not fetch URLs with not isolated from their surroundings' do
  74. expect(a_request(:get, 'http://example.com/sjis')).to_not have_been_made
  75. end
  76. end
  77. context do
  78. let(:status) { Fabricate(:status, text: 'test http://example.com/test?data=file.gpx^1') }
  79. it 'does fetch URLs with a caret in search params' do
  80. expect(a_request(:get, 'http://example.com/test?data=file.gpx')).to_not have_been_made
  81. expect(a_request(:get, 'http://example.com/test?data=file.gpx%5E1')).to have_been_made.once
  82. end
  83. end
  84. context 'with an URL too long for PostgreSQL unique indexes' do
  85. let(:url) { "http://example.com/#{'a' * 2674}" }
  86. let(:status) { Fabricate(:status, text: url) }
  87. it 'does not fetch the URL' do
  88. expect(a_request(:get, url)).to_not have_been_made
  89. end
  90. it 'does not create a preview card' do
  91. expect(status.preview_card).to be_nil
  92. end
  93. end
  94. end
  95. context 'in a remote status' do
  96. let(:status) { Fabricate(:status, account: Fabricate(:account, domain: 'example.com'), text: 'Habt ihr ein paar gute Links zu <a>foo</a> #<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? Ich will mal unter <br> <a href="https://github.com/qbi/WannaCry" target="_blank" rel="noopener noreferrer" title="https://github.com/qbi/WannaCry">https://github.com/qbi/WannaCry</a> was sammeln. !<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;') }
  97. it 'parses out URLs' do
  98. expect(a_request(:get, 'https://github.com/qbi/WannaCry')).to have_been_made.at_least_once
  99. end
  100. it 'ignores URLs to hashtags' do
  101. expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made
  102. end
  103. end
  104. end