1
0
mirror of https://github.com/mastodon/mastodon synced 2024-12-21 18:18:13 +09:00
mastodon/spec/services/fetch_atom_service_spec.rb
valerauko c36a4a1617 Fix FetchAtomService content type handling (#9132)
* Add profile to json+ld in Accept

It's required by the ActivityPub spec

* Use headers['Content-type'] instead of mime_type

mime_type strips the profile from the content type, but it's still available raw in the headers hash

* Add test for ld+json with profile
2018-10-30 15:07:57 +01:00

97 lines
3.2 KiB
Ruby

require 'rails_helper'
RSpec.describe FetchAtomService, type: :service do
describe '#call' do
let(:url) { 'http://example.com' }
subject { FetchAtomService.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 'output log and return nil' do
expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('SSL error: OpenSSL::SSL::SSLError')
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 'output log and return nil' do
expect_any_instance_of(ActiveSupport::Logger).to receive(:debug).with('HTTP ConnectionError: HTTP::ConnectionError')
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 [url, { :prefetched_body => "" }, :ostatus] }
end
context 'content_type is activity+json' do
let(:content_type) { 'application/activity+json' }
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