2016-09-26 07:42:38 -07:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-05-02 09:58:48 -07:00
|
|
|
RSpec.describe FetchAtomService, type: :service do
|
2018-02-09 18:31:38 -08:00
|
|
|
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
|
2018-02-07 15:17:53 -08:00
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
context 'request failed' do
|
|
|
|
before do
|
|
|
|
WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {})
|
2018-02-07 15:17:53 -08:00
|
|
|
end
|
2018-02-09 18:31:38 -08:00
|
|
|
|
|
|
|
it { is_expected.to be_nil }
|
2018-02-07 15:17:53 -08:00
|
|
|
end
|
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
context 'raise OpenSSL::SSL::SSLError' do
|
|
|
|
before do
|
|
|
|
allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError)
|
|
|
|
end
|
2018-02-07 15:17:53 -08:00
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
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
|
2018-02-07 15:17:53 -08:00
|
|
|
end
|
2018-02-08 15:12:35 -08:00
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
context 'raise HTTP::ConnectionError' do
|
2018-02-08 15:12:35 -08:00
|
|
|
before do
|
2018-02-09 18:31:38 -08:00
|
|
|
allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError)
|
2018-02-08 15:12:35 -08:00
|
|
|
end
|
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
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
|
2018-02-08 15:12:35 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
context 'response success' do
|
|
|
|
let(:body) { '' }
|
|
|
|
let(:headers) { { 'Content-Type' => content_type } }
|
|
|
|
let(:json) {
|
|
|
|
{ id: 1,
|
|
|
|
'@context': ActivityPub::TagManager::CONTEXT,
|
|
|
|
type: 'Note',
|
|
|
|
}.to_json
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:12:35 -08:00
|
|
|
before do
|
2018-02-09 18:31:38 -08:00
|
|
|
WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers)
|
2018-02-08 15:12:35 -08:00
|
|
|
end
|
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
context 'content type is application/atom+xml' do
|
|
|
|
let(:content_type) { 'application/atom+xml' }
|
|
|
|
|
2018-10-04 03:36:53 -07:00
|
|
|
it { is_expected.to eq [url, { :prefetched_body => "" }, :ostatus] }
|
2018-02-09 18:31:38 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'content_type is json' do
|
|
|
|
let(:content_type) { 'application/activity+json' }
|
|
|
|
let(:body) { json }
|
|
|
|
|
|
|
|
it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] }
|
2018-02-08 15:12:35 -08:00
|
|
|
end
|
|
|
|
|
2018-02-09 18:31:38 -08:00
|
|
|
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] }
|
2018-02-08 15:12:35 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-26 07:42:38 -07:00
|
|
|
end
|