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.

89 lines
4.4 KiB

  1. require 'rails_helper'
  2. RSpec.describe FetchLinkCardService, type: :service do
  3. subject { FetchLinkCardService.new }
  4. before do
  5. stub_request(:head, 'http://example.xn--fiqs8s/').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  6. stub_request(:get, 'http://example.xn--fiqs8s/').to_return(request_fixture('idn.txt'))
  7. stub_request(:head, 'http://example.com/sjis').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  8. stub_request(:get, 'http://example.com/sjis').to_return(request_fixture('sjis.txt'))
  9. stub_request(:head, 'http://example.com/sjis_with_wrong_charset').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  10. stub_request(:get, 'http://example.com/sjis_with_wrong_charset').to_return(request_fixture('sjis_with_wrong_charset.txt'))
  11. stub_request(:head, 'http://example.com/koi8-r').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  12. stub_request(:get, 'http://example.com/koi8-r').to_return(request_fixture('koi8-r.txt'))
  13. stub_request(:head, 'http://example.com/日本語').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  14. stub_request(:get, 'http://example.com/日本語').to_return(request_fixture('sjis.txt'))
  15. stub_request(:head, 'https://github.com/qbi/WannaCry').to_return(status: 404)
  16. stub_request(:head, 'http://example.com/test-').to_return(status: 200, headers: { 'Content-Type' => 'text/html' })
  17. stub_request(:get, 'http://example.com/test-').to_return(request_fixture('idn.txt'))
  18. subject.call(status)
  19. end
  20. context 'in a local status' do
  21. context do
  22. let(:status) { Fabricate(:status, text: 'Check out http://example.中国') }
  23. it 'works with IDN URLs' do
  24. expect(a_request(:get, 'http://example.xn--fiqs8s/')).to have_been_made.at_least_once
  25. end
  26. end
  27. context do
  28. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis') }
  29. it 'works with SJIS' do
  30. expect(a_request(:get, 'http://example.com/sjis')).to have_been_made.at_least_once
  31. expect(status.preview_cards.first.title).to eq("SJISのページ")
  32. end
  33. end
  34. context do
  35. let(:status) { Fabricate(:status, text: 'Check out http://example.com/sjis_with_wrong_charset') }
  36. it 'works with SJIS even with wrong charset header' do
  37. expect(a_request(:get, 'http://example.com/sjis_with_wrong_charset')).to have_been_made.at_least_once
  38. expect(status.preview_cards.first.title).to eq("SJISのページ")
  39. end
  40. end
  41. context do
  42. let(:status) { Fabricate(:status, text: 'Check out http://example.com/koi8-r') }
  43. it 'works with koi8-r' do
  44. expect(a_request(:get, 'http://example.com/koi8-r')).to have_been_made.at_least_once
  45. expect(status.preview_cards.first.title).to eq("Московя начинаетъ только въ XVI ст. привлекать внимане иностранцевъ.")
  46. end
  47. end
  48. context do
  49. let(:status) { Fabricate(:status, text: 'テストhttp://example.com/日本語') }
  50. it 'works with Japanese path string' do
  51. expect(a_request(:get, 'http://example.com/日本語')).to have_been_made.at_least_once
  52. expect(status.preview_cards.first.title).to eq("SJISのページ")
  53. end
  54. end
  55. context do
  56. let(:status) { Fabricate(:status, text: 'test http://example.com/test-') }
  57. it 'works with a URL ending with a hyphen' do
  58. expect(a_request(:get, 'http://example.com/test-')).to have_been_made.at_least_once
  59. end
  60. end
  61. end
  62. context 'in a remote status' do
  63. let(:status) { Fabricate(:status, account: Fabricate(:account, domain: 'example.com'), text: 'Habt ihr ein paar gute Links zu #<span class="tag"><a href="https://quitter.se/tag/wannacry" target="_blank" rel="tag noopener" 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" 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" title="http://sn.jonkman.ca/group/416/id">security</a>&nbsp;') }
  64. it 'parses out URLs' do
  65. expect(a_request(:head, 'https://github.com/qbi/WannaCry')).to have_been_made.at_least_once
  66. end
  67. it 'ignores URLs to hashtags' do
  68. expect(a_request(:head, 'https://quitter.se/tag/wannacry')).to_not have_been_made
  69. end
  70. end
  71. end