fetch_resource_service_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe FetchResourceService do
  4. describe '#call' do
  5. subject { described_class.new.call(url) }
  6. let(:url) { 'http://example.com' }
  7. context 'with blank url' do
  8. let(:url) { '' }
  9. it { is_expected.to be_nil }
  10. end
  11. context 'when request fails' do
  12. before do
  13. stub_request(:get, url).to_return(status: 500, body: '', headers: {})
  14. end
  15. it { is_expected.to be_nil }
  16. end
  17. context 'when OpenSSL::SSL::SSLError is raised' do
  18. before do
  19. request = instance_double(Request)
  20. allow(Request).to receive(:new).and_return(request)
  21. allow(request).to receive(:add_headers)
  22. allow(request).to receive(:on_behalf_of)
  23. allow(request).to receive(:perform).and_raise(OpenSSL::SSL::SSLError)
  24. end
  25. it { is_expected.to be_nil }
  26. end
  27. context 'when HTTP::ConnectionError is raised' do
  28. before do
  29. request = instance_double(Request)
  30. allow(Request).to receive(:new).and_return(request)
  31. allow(request).to receive(:add_headers)
  32. allow(request).to receive(:on_behalf_of)
  33. allow(request).to receive(:perform).and_raise(HTTP::ConnectionError)
  34. end
  35. it { is_expected.to be_nil }
  36. end
  37. context 'when request succeeds' do
  38. let(:body) { '' }
  39. let(:content_type) { 'application/json' }
  40. let(:headers) do
  41. { 'Content-Type' => content_type }
  42. end
  43. let(:json) do
  44. {
  45. id: 'http://example.com/foo',
  46. '@context': ActivityPub::TagManager::CONTEXT,
  47. type: 'Note',
  48. }.to_json
  49. end
  50. before do
  51. stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  52. stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
  53. end
  54. it 'signs request' do
  55. subject
  56. expect(a_request(:get, url).with(headers: { 'Signature' => /keyId="#{Regexp.escape(ActivityPub::TagManager.instance.key_uri_for(Account.representative))}"/ })).to have_been_made
  57. end
  58. context 'when content type is application/atom+xml' do
  59. let(:content_type) { 'application/atom+xml' }
  60. it { is_expected.to be_nil }
  61. end
  62. context 'when content type is activity+json' do
  63. let(:content_type) { 'application/activity+json; charset=utf-8' }
  64. let(:body) { json }
  65. it { is_expected.to eq ['http://example.com/foo', { prefetched_body: body }] }
  66. end
  67. context 'when content type is ld+json with profile' do
  68. let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
  69. let(:body) { json }
  70. it { is_expected.to eq ['http://example.com/foo', { prefetched_body: body }] }
  71. end
  72. context 'when link header is present' do
  73. let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"' } }
  74. it { is_expected.to eq ['http://example.com/foo', { prefetched_body: json }] }
  75. end
  76. context 'when content type is text/html' do
  77. let(:content_type) { 'text/html' }
  78. let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
  79. it { is_expected.to eq ['http://example.com/foo', { prefetched_body: json }] }
  80. end
  81. end
  82. end
  83. end