fetch_link_card_service_spec.rb 4.4 KB

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