You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
4.5 KiB

  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe FetchLinkCardService, type: :service do
  4. subject { described_class.new }
  5. before do
  6. stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt'))
  7. stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt'))
  8. stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt'))
  9. stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
  10. stub_request(:get, 'http://example.com/日本語').to_return(request_fixture('sjis.txt'))
  11. stub_request(:get, 'https://github.com/qbi/WannaCry').to_return(status: 404)
  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. subject.call(status)
  15. end
  16. context 'in a local status' do
  17. context do
  18. let(:status) { Fabricate(:status, text: 'Check out http://example.中国') }
  19. it 'works with IDN URLs' do
  20. expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once
  21. end
  22. end
  23. context do
  24. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') }
  25. it 'works with SJIS' do
  26. expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once
  27. expect(status.preview_cards.first.title).to eq('SJISのページ')
  28. end
  29. end
  30. context do
  31. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') }
  32. it 'works with SJIS even with wrong charset header' do
  33. expect(a_request(:get, 'http://example.com/sjis_with_wrong_charset')).to have_been_made.at_least_once
  34. expect(status.preview_cards.first.title).to eq('SJISのページ')
  35. end
  36. end
  37. context do
  38. let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') }
  39. it 'works with koi8-r' do
  40. expect(a_request(:get, 'http://example.com/koi8-r')).to have_been_made.at_least_once
  41. expect(status.preview_cards.first.title).to eq('Московя начинаетъ только въ XVI ст. привлекать внимане иностранцевъ.')
  42. end
  43. end
  44. context do
  45. let(:status) { Fabricate(:status, text: 'Check out http://example.com/windows-1251') }
  46. it 'works with windows-1251' do
  47. expect(a_request(:get, 'http://example.com/windows-1251')).to have_been_made.at_least_once
  48. expect(status.preview_cards.first.title).to eq('сэмпл текст')
  49. end
  50. end
  51. context do
  52. let(:status) { Fabricate(:status, text: 'テストhttp://example.com/日本語') }
  53. it 'works with Japanese path string' do
  54. expect(a_request(:get, 'http://example.com/日本語')).to have_been_made.at_least_once
  55. expect(status.preview_cards.first.title).to eq('SJISのページ')
  56. end
  57. end
  58. context do
  59. let(:status) { Fabricate(:status, text: 'test http://example.com/test-') }
  60. it 'works with a URL ending with a hyphen' do
  61. expect(a_request(:get, 'http://example.com/test-')).to have_been_made.at_least_once
  62. end
  63. end
  64. context do
  65. let(:status) { Fabricate(:status, text: 'testhttp://example.com/sjis') }
  66. it 'does not fetch URLs with not isolated from their surroundings' do
  67. expect(a_request(:get, 'http://example.com/sjis')).to_not have_been_made
  68. end
  69. end
  70. end
  71. context 'in a remote status' do
  72. 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;') }
  73. it 'parses out URLs' do
  74. expect(a_request(:get, 'https://github.com/qbi/WannaCry')).to have_been_made.at_least_once
  75. end
  76. it 'ignores URLs to hashtags' do
  77. expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made
  78. end
  79. end
  80. end