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.

96 lines
3.2 KiB

  1. require 'rails_helper'
  2. RSpec.describe FetchAtomService, type: :service do
  3. describe '#call' do
  4. let(:url) { 'http://example.com' }
  5. subject { FetchAtomService.new.call(url) }
  6. context 'url is blank' do
  7. let(:url) { '' }
  8. it { is_expected.to be_nil }
  9. end
  10. context 'request failed' do
  11. before do
  12. WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {})
  13. end
  14. it { is_expected.to be_nil }
  15. end
  16. context 'raise OpenSSL::SSL::SSLError' do
  17. before do
  18. allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError)
  19. end
  20. it 'output log and return nil' do
  21. expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('SSL error: OpenSSL::SSL::SSLError')
  22. is_expected.to be_nil
  23. end
  24. end
  25. context 'raise HTTP::ConnectionError' do
  26. before do
  27. allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError)
  28. end
  29. it 'output log and return nil' do
  30. expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('HTTP ConnectionError: HTTP::ConnectionError')
  31. is_expected.to be_nil
  32. end
  33. end
  34. context 'response success' do
  35. let(:body) { '' }
  36. let(:headers) { { 'Content-Type' => content_type } }
  37. let(:json) {
  38. { id: 1,
  39. '@context': ActivityPub::TagManager::CONTEXT,
  40. type: 'Note',
  41. }.to_json
  42. }
  43. before do
  44. WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  45. end
  46. context 'content type is application/atom+xml' do
  47. let(:content_type) { 'application/atom+xml' }
  48. it { is_expected.to eq [url, { :prefetched_body => "" }, :ostatus] }
  49. end
  50. context 'content_type is activity+json' do
  51. let(:content_type) { 'application/activity+json; charset=utf-8' }
  52. let(:body) { json }
  53. it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
  54. end
  55. context 'content_type is ld+json with profile' do
  56. let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
  57. let(:body) { json }
  58. it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
  59. end
  60. before do
  61. WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  62. WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
  63. end
  64. context 'has link header' do
  65. let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
  66. it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
  67. end
  68. context 'content type is text/html' do
  69. let(:content_type) { 'text/html' }
  70. let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
  71. it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
  72. end
  73. end
  74. end
  75. end