|
5 | 5 |
|
6 | 6 | describe '#call' do |
7 | 7 | let(:url) { 'http://example.com' } |
| 8 | + |
8 | 9 | subject { described_class.new.call(url) } |
9 | 10 |
|
10 | | - context 'url is blank' do |
| 11 | + context 'with blank url' do |
11 | 12 | let(:url) { '' } |
12 | 13 | it { is_expected.to be_nil } |
13 | 14 | end |
14 | 15 |
|
15 | | - context 'request failed' do |
| 16 | + context 'when request fails' do |
16 | 17 | before do |
17 | | - WebMock.stub_request(:get, url).to_return(status: 500, body: '', headers: {}) |
| 18 | + stub_request(:get, url).to_return(status: 500, body: '', headers: {}) |
18 | 19 | end |
19 | 20 |
|
20 | 21 | it { is_expected.to be_nil } |
21 | 22 | end |
22 | 23 |
|
23 | | - context 'raise OpenSSL::SSL::SSLError' do |
| 24 | + context 'when OpenSSL::SSL::SSLError is raised' do |
24 | 25 | before do |
25 | | - allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(OpenSSL::SSL::SSLError) |
| 26 | + allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(OpenSSL::SSL::SSLError) |
26 | 27 | end |
27 | 28 |
|
28 | | - it 'return nil' do |
29 | | - is_expected.to be_nil |
30 | | - end |
| 29 | + it { is_expected.to be_nil } |
31 | 30 | end |
32 | 31 |
|
33 | | - context 'raise HTTP::ConnectionError' do |
| 32 | + context 'when HTTP::ConnectionError is raised' do |
34 | 33 | before do |
35 | | - allow(Request).to receive_message_chain(:new, :add_headers, :perform).and_raise(HTTP::ConnectionError) |
| 34 | + allow(Request).to receive_message_chain(:new, :add_headers, :on_behalf_of, :perform).and_raise(HTTP::ConnectionError) |
36 | 35 | end |
37 | 36 |
|
38 | | - it 'return nil' do |
39 | | - is_expected.to be_nil |
40 | | - end |
| 37 | + it { is_expected.to be_nil } |
41 | 38 | end |
42 | 39 |
|
43 | | - context 'response success' do |
| 40 | + context 'when request succeeds' do |
44 | 41 | let(:body) { '' } |
45 | | - let(:headers) { { 'Content-Type' => content_type } } |
46 | | - let(:json) { |
47 | | - { id: 1, |
| 42 | + |
| 43 | + let(:content_type) { 'application/json' } |
| 44 | + |
| 45 | + let(:headers) do |
| 46 | + { 'Content-Type' => content_type } |
| 47 | + end |
| 48 | + |
| 49 | + let(:json) do |
| 50 | + { |
| 51 | + id: 1, |
48 | 52 | '@context': ActivityPub::TagManager::CONTEXT, |
49 | 53 | type: 'Note', |
50 | 54 | }.to_json |
51 | | - } |
| 55 | + end |
52 | 56 |
|
53 | 57 | before do |
54 | | - WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers) |
| 58 | + stub_request(:get, url).to_return(status: 200, body: body, headers: headers) |
| 59 | + end |
| 60 | + |
| 61 | + it 'signs request' do |
| 62 | + subject |
| 63 | + expect(a_request(:get, url).with(headers: { 'Signature' => /keyId="#{Regexp.escape(ActivityPub::TagManager.instance.uri_for(representative) + '#main-key')}"/ })).to have_been_made |
55 | 64 | end |
56 | 65 |
|
57 | | - context 'content type is application/atom+xml' do |
| 66 | + context 'when content type is application/atom+xml' do |
58 | 67 | let(:content_type) { 'application/atom+xml' } |
59 | 68 |
|
60 | 69 | it { is_expected.to eq nil } |
61 | 70 | end |
62 | 71 |
|
63 | | - context 'content_type is activity+json' do |
| 72 | + context 'when content type is activity+json' do |
64 | 73 | let(:content_type) { 'application/activity+json; charset=utf-8' } |
65 | 74 | let(:body) { json } |
66 | 75 |
|
67 | 76 | it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] } |
68 | 77 | end |
69 | 78 |
|
70 | | - context 'content_type is ld+json with profile' do |
| 79 | + context 'when content type is ld+json with profile' do |
71 | 80 | let(:content_type) { 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' } |
72 | 81 | let(:body) { json } |
73 | 82 |
|
74 | 83 | it { is_expected.to eq [1, { prefetched_body: body, id: true }, :activitypub] } |
75 | 84 | end |
76 | 85 |
|
77 | 86 | before do |
78 | | - WebMock.stub_request(:get, url).to_return(status: 200, body: body, headers: headers) |
79 | | - WebMock.stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' }) |
| 87 | + stub_request(:get, url).to_return(status: 200, body: body, headers: headers) |
| 88 | + stub_request(:get, 'http://example.com/foo').to_return(status: 200, body: json, headers: { 'Content-Type' => 'application/activity+json' }) |
80 | 89 | end |
81 | 90 |
|
82 | | - context 'has link header' do |
| 91 | + context 'when link header is present' do |
83 | 92 | let(:headers) { { 'Link' => '<http://example.com/foo>; rel="alternate"; type="application/activity+json"', } } |
84 | 93 |
|
85 | 94 | it { is_expected.to eq [1, { prefetched_body: json, id: true }, :activitypub] } |
86 | 95 | end |
87 | 96 |
|
88 | | - context 'content type is text/html' do |
| 97 | + context 'when content type is text/html' do |
89 | 98 | let(:content_type) { 'text/html' } |
90 | 99 | let(:body) { '<html><head><link rel="alternate" href="http://example.com/foo" type="application/activity+json"/></head></html>' } |
91 | 100 |
|
|
0 commit comments