fetch_resource_service_spec.rb 3.4 KB

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