about summary refs log tree commit diff
path: root/spec/services/fetch_resource_service_spec.rb
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-07-10 18:59:28 +0200
committerGitHub <noreply@github.com>2019-07-10 18:59:28 +0200
commit5d3feed191bcbe2769512119752b426108152fe9 (patch)
treeb36bc69823e5a16250bb9f72d388498737d6725a /spec/services/fetch_resource_service_spec.rb
parentd04c584159fdad36d7713718c4ba0b3b42cd27a9 (diff)
Refactor fetching of remote resources (#11251)
Diffstat (limited to 'spec/services/fetch_resource_service_spec.rb')
-rw-r--r--spec/services/fetch_resource_service_spec.rb96
1 files changed, 96 insertions, 0 deletions
diff --git a/spec/services/fetch_resource_service_spec.rb b/spec/services/fetch_resource_service_spec.rb
new file mode 100644
index 000000000..17c192c44
--- /dev/null
+++ b/spec/services/fetch_resource_service_spec.rb
@@ -0,0 +1,96 @@
+require 'rails_helper'
+
+RSpec.describe FetchResourceService, type: :service do
+  let!(:representative) { Fabricate(:account) }
+
+  describe '#call' do
+    let(:url) { 'http://example.com' }
+    subject { described_class.new.call(url) }
+
+    context 'url is blank' do
+      let(:url) { '' }
+      it { is_expected.to be_nil }
+    end
+
+    context 'request failed' do
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {})
+      end
+
+      it { is_expected.to be_nil }
+    end
+
+    context 'raise OpenSSL::SSL::SSLError' do
+      before do
+        allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError)
+      end
+
+      it 'return nil' do
+        is_expected.to be_nil
+      end
+    end
+
+    context 'raise HTTP::ConnectionError' do
+      before do
+        allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError)
+      end
+
+      it 'return nil' do
+        is_expected.to be_nil
+      end
+    end
+
+    context 'response success' do
+      let(:body) { '' }
+      let(:headers) { { 'Content-Type' => content_type } }
+      let(:json) {
+        { id: 1,
+          '@context': ActivityPub::TagManager::CONTEXT,
+          type: 'Note',
+        }.to_json
+      }
+
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
+      end
+
+      context 'content type is application/atom+xml' do
+        let(:content_type) { 'application/atom+xml' }
+
+        it { is_expected.to eq nil }
+      end
+
+      context 'content_type is activity+json' do
+        let(:content_type) { 'application/activity+json; charset=utf-8' }
+        let(:body) { json }
+
+        it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
+      end
+
+      context 'content_type is ld+json with profile' do
+        let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' }
+        let(:body) { json }
+
+        it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
+      end
+
+      before do
+        WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
+        WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' })
+      end
+
+      context 'has link header' do
+        let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } }
+
+        it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
+      end
+
+      context 'content type is text/html' do
+        let(:content_type) { 'text/html' }
+        let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' }
+
+        it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] }
+      end
+    end
+  end
+end