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

  1. require 'rails_helper'
  2. RSpec.describe FetchAtomService 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 json' do
  51. let(:content_type) { 'application/activity+json' }
  52. let(:body) { json }
  53. it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
  54. end
  55. before do
  56. WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
  57. WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
  58. end
  59. context 'has link header' do
  60. let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
  61. it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
  62. end
  63. context 'content type is text/html' do
  64. let(:content_type) { 'text/html' }
  65. let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
  66. it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
  67. end
  68. end
  69. end
  70. end