ddy_remote_resource 0.4.11 → 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/Guardfile +3 -0
- data/lib/remote_resource.rb +77 -34
- data/lib/remote_resource/base.rb +20 -8
- data/lib/remote_resource/connection.rb +26 -5
- data/lib/remote_resource/connection_options.rb +5 -3
- data/lib/remote_resource/querying/finder_methods.rb +3 -3
- data/lib/remote_resource/querying/persistence_methods.rb +17 -12
- data/lib/remote_resource/request.rb +96 -62
- data/lib/remote_resource/response.rb +5 -1
- data/lib/remote_resource/rest.rb +13 -17
- data/lib/remote_resource/url_naming.rb +4 -10
- data/lib/remote_resource/url_naming_determination.rb +1 -3
- data/lib/remote_resource/util.rb +64 -0
- data/lib/remote_resource/version.rb +1 -1
- data/remote_resource.gemspec +2 -2
- data/spec/fixtures/text_file.txt +1 -0
- data/spec/integration/all_spec.rb +166 -0
- data/spec/integration/collection_prefix_spec.rb +99 -0
- data/spec/integration/create_spec.rb +181 -0
- data/spec/integration/destroy_spec.rb +252 -0
- data/spec/integration/find_by_spec.rb +168 -0
- data/spec/integration/find_spec.rb +139 -0
- data/spec/integration/headers_spec.rb +222 -0
- data/spec/integration/naming_spec.rb +138 -0
- data/spec/integration/save_spec.rb +320 -0
- data/spec/integration/update_attributes_spec.rb +221 -0
- data/spec/integration/where_spec.rb +152 -0
- data/spec/lib/extensions/ethon/easy/queryable_spec.rb +4 -4
- data/spec/lib/remote_resource/base_spec.rb +54 -110
- data/spec/lib/remote_resource/builder_spec.rb +1 -1
- data/spec/lib/remote_resource/collection_spec.rb +1 -1
- data/spec/lib/remote_resource/connection_options_spec.rb +20 -17
- data/spec/lib/remote_resource/connection_spec.rb +36 -27
- data/spec/lib/remote_resource/querying/finder_methods_spec.rb +199 -72
- data/spec/lib/remote_resource/querying/persistence_methods_spec.rb +228 -220
- data/spec/lib/remote_resource/request_spec.rb +313 -342
- data/spec/lib/remote_resource/response_spec.rb +9 -3
- data/spec/lib/remote_resource/rest_spec.rb +11 -11
- data/spec/lib/remote_resource/url_naming_determination_spec.rb +1 -1
- data/spec/lib/remote_resource/url_naming_spec.rb +7 -22
- data/spec/lib/remote_resource/util_spec.rb +56 -0
- data/spec/lib/remote_resource/version_spec.rb +2 -3
- data/spec/spec_helper.rb +37 -0
- metadata +33 -22
- data/lib/remote_resource/http_errors.rb +0 -33
- data/lib/remote_resource/response_handeling.rb +0 -48
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe '.where' do
|
4
|
+
|
5
|
+
class Post
|
6
|
+
include RemoteResource::Base
|
7
|
+
|
8
|
+
self.site = 'https://www.example.com'
|
9
|
+
self.collection = true
|
10
|
+
self.root_element = :data
|
11
|
+
|
12
|
+
attribute :title, String
|
13
|
+
attribute :body, String
|
14
|
+
attribute :created_at, Time
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:response_body) do
|
18
|
+
{
|
19
|
+
data: [
|
20
|
+
{
|
21
|
+
id: 12,
|
22
|
+
title: 'Lorem Ipsum',
|
23
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
24
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
25
|
+
},
|
26
|
+
{
|
27
|
+
id: 14,
|
28
|
+
title: 'Mauris Purus',
|
29
|
+
body: 'Mauris purus urna, ultrices et suscipit ut, faucibus eget mauris.',
|
30
|
+
created_at: Time.new(2015, 12, 11, 11, 32, 0),
|
31
|
+
},
|
32
|
+
{
|
33
|
+
id: 16,
|
34
|
+
title: 'Vestibulum Commodo',
|
35
|
+
body: 'Vestibulum commodo fringilla suscipit.',
|
36
|
+
created_at: Time.new(2016, 2, 6, 18, 45, 0),
|
37
|
+
},
|
38
|
+
]
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
let(:expected_default_headers) do
|
43
|
+
{ 'Accept' => 'application/json', 'User-Agent' => "RemoteResource #{RemoteResource::VERSION}" }
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'default behaviour' do
|
47
|
+
let!(:expected_request) do
|
48
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
49
|
+
mock_request.with(query: { pseudonym: 'pseudonym' }, body: nil, headers: expected_default_headers)
|
50
|
+
mock_request.to_return(status: 200, body: response_body.to_json)
|
51
|
+
mock_request
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'performs the correct HTTP GET request' do
|
55
|
+
Post.where({ pseudonym: 'pseudonym' })
|
56
|
+
expect(expected_request).to have_been_requested
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'builds the correct collection of resources' do
|
60
|
+
posts = Post.where({ pseudonym: 'pseudonym' })
|
61
|
+
|
62
|
+
aggregate_failures do
|
63
|
+
expect(posts).to respond_to :each
|
64
|
+
expect(posts).to all(be_a(Post))
|
65
|
+
expect(posts.size).to eql 3
|
66
|
+
|
67
|
+
expect(posts[0].id).to eql 12
|
68
|
+
expect(posts[0].title).to eql 'Lorem Ipsum'
|
69
|
+
expect(posts[0].body).to eql 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
70
|
+
expect(posts[0].created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
71
|
+
expect(posts[1].id).to eql 14
|
72
|
+
expect(posts[1].title).to eql 'Mauris Purus'
|
73
|
+
expect(posts[1].body).to eql 'Mauris purus urna, ultrices et suscipit ut, faucibus eget mauris.'
|
74
|
+
expect(posts[1].created_at).to eql Time.new(2015, 12, 11, 11, 32, 0)
|
75
|
+
expect(posts[2].id).to eql 16
|
76
|
+
expect(posts[2].title).to eql 'Vestibulum Commodo'
|
77
|
+
expect(posts[2].body).to eql 'Vestibulum commodo fringilla suscipit.'
|
78
|
+
expect(posts[2].created_at).to eql Time.new(2016, 2, 6, 18, 45, 0)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'with connection_options[:headers]' do
|
84
|
+
let!(:expected_request) do
|
85
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
86
|
+
mock_request.with(query: { pseudonym: 'pseudonym' }, body: nil, headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
87
|
+
mock_request.to_return(status: 200, body: response_body.to_json)
|
88
|
+
mock_request
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'performs the correct HTTP GET request' do
|
92
|
+
Post.where({ pseudonym: 'pseudonym' }, headers: { 'X-Pseudonym' => 'pseudonym' })
|
93
|
+
expect(expected_request).to have_been_requested
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe 'with a 404 response' do
|
98
|
+
let!(:expected_request) do
|
99
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
100
|
+
mock_request.with(query: { pseudonym: 'pseudonym' }, body: nil, headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
101
|
+
mock_request.to_return(status: 404)
|
102
|
+
mock_request
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'raises the not found error' do
|
106
|
+
expect { Post.where({ pseudonym: 'pseudonym' }, headers: { 'X-Pseudonym' => 'pseudonym' }) }.to raise_error RemoteResource::HTTPNotFound
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'adds metadata to the raised error' do
|
110
|
+
begin
|
111
|
+
Post.where({ pseudonym: 'pseudonym' }, headers: { 'X-Pseudonym' => 'pseudonym' })
|
112
|
+
rescue RemoteResource::HTTPNotFound => error
|
113
|
+
aggregate_failures do
|
114
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=404 with http_action=get with request_url=https://www.example.com/posts.json'
|
115
|
+
expect(error.request_url).to eql 'https://www.example.com/posts.json'
|
116
|
+
expect(error.response_code).to eql 404
|
117
|
+
expect(error.request_query).to eql(RemoteResource::Util.encode_params_to_query({ pseudonym: 'pseudonym' }))
|
118
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'with a 500 response' do
|
125
|
+
let!(:expected_request) do
|
126
|
+
mock_request = stub_request(:get, 'https://www.example.com/posts.json')
|
127
|
+
mock_request.with(query: { pseudonym: 'pseudonym' }, body: nil, headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
128
|
+
mock_request.to_return(status: 500)
|
129
|
+
mock_request
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'raises the server error' do
|
133
|
+
expect { Post.where({ pseudonym: 'pseudonym' }, headers: { 'X-Pseudonym' => 'pseudonym' }) }.to raise_error RemoteResource::HTTPServerError
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'adds metadata to the raised error' do
|
137
|
+
begin
|
138
|
+
Post.where({ pseudonym: 'pseudonym' }, headers: { 'X-Pseudonym' => 'pseudonym' })
|
139
|
+
rescue RemoteResource::HTTPServerError => error
|
140
|
+
aggregate_failures do
|
141
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=500 with http_action=get with request_url=https://www.example.com/posts.json'
|
142
|
+
expect(error.request_url).to eql 'https://www.example.com/posts.json'
|
143
|
+
expect(error.response_code).to eql 500
|
144
|
+
expect(error.request_query).to eql(RemoteResource::Util.encode_params_to_query({ pseudonym: 'pseudonym' }))
|
145
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'ethon'
|
2
2
|
require_relative '../../../../../lib/extensions/ethon/easy/queryable'
|
3
3
|
|
4
|
-
describe Ethon::Easy::Queryable do
|
4
|
+
RSpec.describe Ethon::Easy::Queryable, order: :defined do
|
5
5
|
|
6
6
|
let(:hash) { {} }
|
7
7
|
let!(:easy) { Ethon::Easy.new }
|
@@ -70,7 +70,7 @@ describe Ethon::Easy::Queryable do
|
|
70
70
|
end
|
71
71
|
|
72
72
|
context "when file" do
|
73
|
-
let(:file) { File.open("spec/
|
73
|
+
let(:file) { File.open("spec/fixtures/text_file.txt") }
|
74
74
|
let(:file_info) { params.method(:file_info).call(file) }
|
75
75
|
let(:hash) { {:a => {:b => [file]}} }
|
76
76
|
let(:mime_type) { file_info[1] }
|
@@ -82,7 +82,7 @@ describe Ethon::Easy::Queryable do
|
|
82
82
|
context "when MIME" do
|
83
83
|
context "when mime type" do
|
84
84
|
it "sets mime type to text" do
|
85
|
-
expect(mime_type).to eq("
|
85
|
+
expect(mime_type).to eq("text/plain")
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
@@ -132,4 +132,4 @@ describe Ethon::Easy::Queryable do
|
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|
135
|
-
end
|
135
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RemoteResource::Base do
|
3
|
+
RSpec.describe RemoteResource::Base do
|
4
4
|
|
5
5
|
module RemoteResource
|
6
6
|
class Dummy
|
@@ -22,12 +22,6 @@ describe RemoteResource::Base do
|
|
22
22
|
specify { expect(described_class.const_defined?('RemoteResource::Querying::FinderMethods')).to be_truthy }
|
23
23
|
specify { expect(described_class.const_defined?('RemoteResource::Querying::PersistenceMethods')).to be_truthy }
|
24
24
|
|
25
|
-
describe 'OPTIONS' do
|
26
|
-
let(:options) { [:base_url, :site, :headers, :version, :path_prefix, :path_postfix, :collection_prefix, :content_type, :collection, :collection_name, :root_element] }
|
27
|
-
|
28
|
-
specify { expect(described_class::OPTIONS).to eql options }
|
29
|
-
end
|
30
|
-
|
31
25
|
describe 'attributes' do
|
32
26
|
it '#id' do
|
33
27
|
expect(dummy.attributes).to have_key :id
|
@@ -45,7 +39,7 @@ describe RemoteResource::Base do
|
|
45
39
|
after { described_class.global_headers = nil }
|
46
40
|
|
47
41
|
it 'sets the global headers Thread variable' do
|
48
|
-
expect{ described_class.global_headers = global_headers }.to change{
|
42
|
+
expect{ described_class.global_headers = global_headers }.to change{ described_class.global_headers }.from({}).to(global_headers)
|
49
43
|
end
|
50
44
|
end
|
51
45
|
|
@@ -90,76 +84,33 @@ describe RemoteResource::Base do
|
|
90
84
|
end
|
91
85
|
|
92
86
|
describe '.with_connection_options' do
|
93
|
-
let(:connection_options) { {} }
|
94
|
-
|
95
|
-
let(:block_with_connection_options) do
|
96
|
-
dummy_class.with_connection_options(connection_options) do
|
97
|
-
dummy_class.find_by({ username: 'foobar' }, { content_type: '.json' })
|
98
|
-
dummy_class.create({ username: 'bazbar' }, { content_type: '.xml' })
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
before { allow_any_instance_of(Typhoeus::Request).to receive(:run) { double.as_null_object } }
|
103
|
-
|
104
87
|
it 'yields the block' do
|
105
|
-
expect(dummy_class).to receive(:find_by).with({ username: 'foobar' }, {
|
106
|
-
expect(dummy_class).to receive(:create).with({ username: 'bazbar' }, {
|
107
|
-
block_with_connection_options
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'ensures to set the threaded_connection_options Thread variable to nil' do
|
111
|
-
dummy_class.threaded_connection_options
|
112
|
-
|
113
|
-
expect{ block_with_connection_options }.to change{ Thread.current['remote_resource.dummy.threaded_connection_options'] }.from(an_instance_of(Hash)).to nil
|
114
|
-
end
|
88
|
+
expect(dummy_class).to receive(:find_by).with({ username: 'foobar' }, { path_prefix: '/archive' })
|
89
|
+
expect(dummy_class).to receive(:create).with({ username: 'bazbar' }, { path_prefix: '/featured' })
|
115
90
|
|
116
|
-
|
117
|
-
|
118
|
-
{
|
119
|
-
headers: { "Foo" => "Bar" }
|
120
|
-
}
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'uses the headers of the given connection_options' do
|
124
|
-
expect(Typhoeus::Request).to receive(:get).with('https://foobar.com/dummy.json', params: { username: 'foobar' }, headers: { "Accept" => "application/json", "Foo" => "Bar" }).and_call_original
|
125
|
-
expect(Typhoeus::Request).to receive(:post).with('https://foobar.com/dummy.xml', body: { username: 'bazbar' }, headers: { "Accept" => "application/json", "Foo" => "Bar" }).and_call_original
|
126
|
-
block_with_connection_options
|
91
|
+
dummy_class.with_connection_options({ version: '/api/v1', headers: { 'Foo' => 'Bar' } }) do
|
92
|
+
dummy_class.find_by({ username: 'foobar' }, { path_prefix: '/archive' })
|
93
|
+
dummy_class.create({ username: 'bazbar' }, { path_prefix: '/featured' })
|
127
94
|
end
|
128
95
|
end
|
129
96
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
base_url: 'https://api.foobar.eu/dummy'
|
134
|
-
}
|
135
|
-
end
|
97
|
+
it 'ensures to set the threaded_connection_options back to the default' do
|
98
|
+
allow(dummy_class).to receive(:find_by)
|
99
|
+
allow(dummy_class).to receive(:create)
|
136
100
|
|
137
|
-
|
138
|
-
expect(Typhoeus::Request).to receive(:get).with('https://api.foobar.eu/dummy.json', params: { username: 'foobar' }, headers: { "Accept" => "application/json" }).and_call_original
|
139
|
-
expect(Typhoeus::Request).to receive(:post).with('https://api.foobar.eu/dummy.xml', body: { username: 'bazbar' }, headers: { "Accept" => "application/json" }).and_call_original
|
140
|
-
block_with_connection_options
|
141
|
-
end
|
142
|
-
end
|
101
|
+
expect(dummy_class.threaded_connection_options).to eql({})
|
143
102
|
|
144
|
-
|
145
|
-
|
146
|
-
{
|
147
|
-
collection: true,
|
148
|
-
path_prefix: '/api',
|
149
|
-
root_element: :bazbar
|
150
|
-
}
|
103
|
+
dummy_class.with_connection_options({ version: '/api/v1', headers: { 'Foo' => 'Bar' } }) do
|
104
|
+
dummy_class.find_by({ username: 'foobar' }, { path_prefix: '/archive' })
|
105
|
+
dummy_class.create({ username: 'bazbar' }, { path_prefix: '/featured' })
|
151
106
|
end
|
152
107
|
|
153
|
-
|
154
|
-
expect(Typhoeus::Request).to receive(:get).with('https://foobar.com/api/dummies.json', params: { username: 'foobar' }, headers: { "Accept" => "application/json" }).and_call_original
|
155
|
-
expect(Typhoeus::Request).to receive(:post).with('https://foobar.com/api/dummies.xml', body: { 'bazbar' => { username: 'bazbar' } }, headers: { "Accept" => "application/json" }).and_call_original
|
156
|
-
block_with_connection_options
|
157
|
-
end
|
108
|
+
expect(dummy_class.threaded_connection_options).to eql({})
|
158
109
|
end
|
159
110
|
end
|
160
111
|
|
161
112
|
describe '#connection_options' do
|
162
|
-
it '
|
113
|
+
it 'instantiates as a RemoteResource::ConnectionOptions' do
|
163
114
|
expect(dummy.connection_options).to be_a RemoteResource::ConnectionOptions
|
164
115
|
end
|
165
116
|
|
@@ -168,6 +119,22 @@ describe RemoteResource::Base do
|
|
168
119
|
end
|
169
120
|
end
|
170
121
|
|
122
|
+
describe '#persistence' do
|
123
|
+
context 'when #persisted?' do
|
124
|
+
it 'returns the resource' do
|
125
|
+
dummy.id = 10
|
126
|
+
expect(dummy.persistence).to eql dummy
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'when NOT #persisted?' do
|
131
|
+
it 'returns nil' do
|
132
|
+
dummy.id = nil
|
133
|
+
expect(dummy.persistence).to be_nil
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
171
138
|
describe '#persisted?' do
|
172
139
|
context 'when id is present' do
|
173
140
|
it 'returns true' do
|
@@ -176,26 +143,41 @@ describe RemoteResource::Base do
|
|
176
143
|
end
|
177
144
|
end
|
178
145
|
|
179
|
-
context 'when is is
|
146
|
+
context 'when id is present and destroyed is present' do
|
180
147
|
it 'returns false' do
|
148
|
+
dummy.id = 10
|
149
|
+
dummy.destroyed = true
|
150
|
+
expect(dummy.persisted?).to eql false
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'when id is NOT present' do
|
155
|
+
it 'returns false' do
|
156
|
+
dummy.id = nil
|
157
|
+
expect(dummy.persisted?).to eql false
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when id is NOT present and destroyed is present' do
|
162
|
+
it 'returns false' do
|
163
|
+
dummy.id = nil
|
164
|
+
dummy.destroyed = true
|
181
165
|
expect(dummy.persisted?).to eql false
|
182
166
|
end
|
183
167
|
end
|
184
168
|
end
|
185
169
|
|
186
170
|
describe '#new_record?' do
|
187
|
-
context 'when
|
171
|
+
context 'when #persisted?' do
|
188
172
|
it 'returns false' do
|
189
|
-
|
190
|
-
|
173
|
+
dummy.id = 10
|
191
174
|
expect(dummy.new_record?).to eql false
|
192
175
|
end
|
193
176
|
end
|
194
177
|
|
195
|
-
context 'when
|
178
|
+
context 'when NOT #persisted?' do
|
196
179
|
it 'returns true' do
|
197
|
-
|
198
|
-
|
180
|
+
dummy.id = nil
|
199
181
|
expect(dummy.new_record?).to eql true
|
200
182
|
end
|
201
183
|
end
|
@@ -302,43 +284,5 @@ describe RemoteResource::Base do
|
|
302
284
|
end
|
303
285
|
end
|
304
286
|
|
305
|
-
describe '#assign_errors' do
|
306
|
-
context 'with errors in the error_messages' do
|
307
|
-
let(:error_messages) do
|
308
|
-
{
|
309
|
-
"foo" => ["is required"],
|
310
|
-
"bar" => ["must be greater than 5"]
|
311
|
-
}
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'assigns the error_messages as errors' do
|
315
|
-
dummy.send :assign_errors, error_messages
|
316
|
-
expect(dummy.errors.messages).to eql foo: ["is required"], bar: ["must be greater than 5"]
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
context 'with an empty Hash in the error_messages' do
|
321
|
-
let(:error_messages) do
|
322
|
-
{}
|
323
|
-
end
|
324
|
-
|
325
|
-
it 'does NOT assign the error_messages as errors' do
|
326
|
-
dummy.send :assign_errors, error_messages
|
327
|
-
expect(dummy.errors.messages).to eql({})
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
context 'with a String in the error_messages' do
|
332
|
-
let(:error_messages) do
|
333
|
-
"unauthorized"
|
334
|
-
end
|
335
|
-
|
336
|
-
it 'does NOT assign the error_messages as errors' do
|
337
|
-
dummy.send :assign_errors, error_messages
|
338
|
-
expect(dummy.errors.messages).to eql({})
|
339
|
-
end
|
340
|
-
end
|
341
|
-
end
|
342
|
-
|
343
287
|
end
|
344
288
|
|
@@ -1,22 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe RemoteResource::ConnectionOptions do
|
3
|
+
RSpec.describe RemoteResource::ConnectionOptions do
|
4
4
|
|
5
5
|
module RemoteResource
|
6
6
|
class ConnectionOptionsDummy
|
7
7
|
include RemoteResource::Base
|
8
8
|
|
9
9
|
self.site = 'https://foobar.com'
|
10
|
-
self.
|
11
|
-
self.
|
10
|
+
self.extension = ''
|
11
|
+
self.default_headers = { 'Accept' => 'application/vnd+json' }
|
12
12
|
self.version = '/v1'
|
13
13
|
self.path_prefix = '/prefix'
|
14
14
|
self.path_postfix = '/postfix'
|
15
|
-
self.content_type = '.json'
|
16
15
|
self.collection_prefix = '/parent/:parent_id'
|
17
16
|
self.collection = true
|
18
17
|
self.root_element = :test_dummy
|
19
18
|
|
19
|
+
def self.headers
|
20
|
+
{ 'Authorization' => 'Bearer <token>' }
|
21
|
+
end
|
22
|
+
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
@@ -32,21 +35,21 @@ describe RemoteResource::ConnectionOptions do
|
|
32
35
|
expect(connection_options.base_class).to eql RemoteResource::ConnectionOptionsDummy
|
33
36
|
end
|
34
37
|
|
35
|
-
context 'RemoteResource::
|
38
|
+
context 'RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS' do
|
36
39
|
it 'calls #initialize_connection_options' do
|
37
40
|
expect_any_instance_of(described_class).to receive(:initialize_connection_options)
|
38
41
|
connection_options
|
39
42
|
end
|
40
43
|
|
41
|
-
it 'sets the accessor of the option from the RemoteResource::
|
42
|
-
RemoteResource::
|
44
|
+
it 'sets the accessor of the option from the RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS' do
|
45
|
+
RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS.each do |option|
|
43
46
|
expect(connection_options).to respond_to "#{option}"
|
44
47
|
expect(connection_options).to respond_to "#{option}="
|
45
48
|
end
|
46
49
|
end
|
47
50
|
|
48
|
-
it 'assigns the value of the option from the RemoteResource::
|
49
|
-
RemoteResource::
|
51
|
+
it 'assigns the value of the option from the RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS' do
|
52
|
+
RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS.each do |option|
|
50
53
|
expect(connection_options.public_send(option)).to eql dummy_class.public_send(option)
|
51
54
|
end
|
52
55
|
end
|
@@ -57,7 +60,7 @@ describe RemoteResource::ConnectionOptions do
|
|
57
60
|
let(:custom_connection_options) do
|
58
61
|
{
|
59
62
|
site: 'https://dummy.foobar.com',
|
60
|
-
|
63
|
+
version: '/api/v2',
|
61
64
|
root_element: :test_dummy_api
|
62
65
|
}
|
63
66
|
end
|
@@ -66,7 +69,7 @@ describe RemoteResource::ConnectionOptions do
|
|
66
69
|
connection_options.merge custom_connection_options
|
67
70
|
|
68
71
|
expect(connection_options.site).to eql 'https://dummy.foobar.com'
|
69
|
-
expect(connection_options.
|
72
|
+
expect(connection_options.version).to eql '/api/v2'
|
70
73
|
expect(connection_options.root_element).to eql :test_dummy_api
|
71
74
|
end
|
72
75
|
|
@@ -78,13 +81,13 @@ describe RemoteResource::ConnectionOptions do
|
|
78
81
|
describe '#to_hash' do
|
79
82
|
let(:connection_options_hash) do
|
80
83
|
{
|
81
|
-
base_url: 'https://foobar.com/v1/prefix/parent/:parent_id/connection_options_dummies/postfix',
|
82
84
|
site: 'https://foobar.com',
|
83
|
-
|
85
|
+
default_headers: { 'Accept' => 'application/vnd+json' },
|
86
|
+
headers: { 'Authorization' => 'Bearer <token>' },
|
84
87
|
version: '/v1',
|
85
88
|
path_prefix: '/prefix',
|
86
89
|
path_postfix: '/postfix',
|
87
|
-
|
90
|
+
extension: '',
|
88
91
|
collection_prefix: '/parent/:parent_id',
|
89
92
|
collection: true,
|
90
93
|
collection_name: nil,
|
@@ -102,7 +105,7 @@ describe RemoteResource::ConnectionOptions do
|
|
102
105
|
expect(connection_options.reload).not_to eql connection_options
|
103
106
|
end
|
104
107
|
|
105
|
-
context 'RemoteResource::
|
108
|
+
context 'RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS' do
|
106
109
|
it 'calls #initialize_connection_options' do
|
107
110
|
expect_any_instance_of(described_class).to receive(:initialize_connection_options).twice
|
108
111
|
connection_options.reload
|
@@ -115,7 +118,7 @@ describe RemoteResource::ConnectionOptions do
|
|
115
118
|
expect(connection_options.reload!).to eql connection_options
|
116
119
|
end
|
117
120
|
|
118
|
-
context 'RemoteResource::
|
121
|
+
context 'RemoteResource::ConnectionOptions::AVAILABLE_OPTIONS' do
|
119
122
|
it 'calls #initialize_connection_options' do
|
120
123
|
expect_any_instance_of(described_class).to receive(:initialize_connection_options).twice
|
121
124
|
connection_options.reload!
|
@@ -123,4 +126,4 @@ describe RemoteResource::ConnectionOptions do
|
|
123
126
|
end
|
124
127
|
end
|
125
128
|
|
126
|
-
end
|
129
|
+
end
|