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
3.1 KiB

  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. allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(OpenSSL::SSL::SSLError)
  19. end
  20. it { is_expected.to be_nil }
  21. end
  22. context 'when HTTP::ConnectionError is raised' do
  23. before do
  24. allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(HTTP::ConnectionError)
  25. end
  26. it { is_expected.to be_nil }
  27. end
  28. context 'when request succeeds' do
  29. let(:body) { '' }
  30. let(:content_type) { 'application/json' }
  31. let(:headers) do
  32. { 'Content-Type' => content_type }
  33. end
  34. let(:json) do
  35. {
  36. id: 1,
  37. '@context': ActivityPub::TagManager::CONTEXT,
  38. type: 'Note',
  39. }.to_json
  40. end
  41. before do
  42. stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  43. end
  44. it 'signs request' do
  45. subject
  46. expect(a_request(:get, url).with(headers: { 'Signature' => /keyId="#{Regexp.escape(ActivityPub::TagManager.instance.uri_for(Account.representative) + '#main-key')}"/ })).to have_been_made
  47. end
  48. context 'when content type is application/atom+xml' do
  49. let(:content_type) { 'application/atom+xml' }
  50. it { is_expected.to eq nil }
  51. end
  52. context 'when content type is activity+json' do
  53. let(:content_type) { 'application/activity+json; charset=utf-8' }
  54. let(:body) { json }
  55. it { is_expected.to eq [1, { prefetched_body: body, id: true }] }
  56. end
  57. context 'when content type is ld+json with profile' do
  58. let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
  59. let(:body) { json }
  60. it { is_expected.to eq [1, { prefetched_body: body, id: true }] }
  61. end
  62. before do
  63. stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  64. stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
  65. end
  66. context 'when link header is present' do
  67. let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
  68. it { is_expected.to eq [1, { prefetched_body: json, id: true }] }
  69. end
  70. context 'when content type is text/html' do
  71. let(:content_type) { 'text/html' }
  72. let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
  73. it { is_expected.to eq [1, { prefetched_body: json, id: true }] }
  74. end
  75. end
  76. end
  77. end