Skip to content

Commit 5d46aa5

Browse files
Fixed support of multiple sites
* fixed connection initialization, * added the test.
1 parent c9a5fe1 commit 5d46aa5

File tree

2 files changed

+54
-25
lines changed

2 files changed

+54
-25
lines changed

lib/cirro_io/client/base.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ class Base < JsonApiClient::Resource
44
self.route_format = :dasherized_route
55
self.json_key_format = :dasherized_key
66

7-
@setup_connection = lambda do |connection|
8-
connection.use CirroIO::Client::JwtAuthentication
9-
connection.use Faraday::Response::Logger
10-
# connection.use CirroIO::Client::ResponseDebuggingMiddleware # This middleware can be injected during debugging or while adding new specs
11-
end
12-
13-
connection(&@setup_connection)
14-
157
# HACK: https://github.com/JsonApiClient/json_api_client/issues/215
8+
# Used for initialization as well
169
def self.site=(url)
17-
super.tap { connection true, &@setup_connection }
10+
super.tap do
11+
connection true do |connection|
12+
connection.use JwtAuthentication
13+
connection.use Faraday::Response::Logger
14+
# connection.use ResponseDebuggingMiddleware # for debugging or while adding new specs
15+
end
16+
end
1817
end
1918

2019
def self.custom_post(endpoint, payload)

spec/cirro_io/client/base_spec.rb

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
RSpec.describe CirroIO::Client::Base do
2-
describe 'jwt_authentication' do
3-
before do
4-
configure_api_client
5-
end
2+
let :request_headers do
3+
{
4+
'Accept' => 'application/vnd.api+json',
5+
'Accept-Encoding' => 'gzip,deflate',
6+
'Content-Type' => 'application/vnd.api+json',
7+
'User-Agent' => 'Faraday v1.1.0',
8+
'Authorization' => 'Bearer jwt-token',
9+
}
10+
end
611

7-
it 'sends correct token' do
8-
allow(JWT).to receive(:encode).and_return('jwt-token')
12+
let :response_headers do
13+
{
14+
'Content-Type' => 'application/json',
15+
}
16+
end
917

18+
before do
19+
configure_api_client
20+
allow(JWT).to receive(:encode).and_return('jwt-token')
21+
end
22+
23+
describe 'jwt_authentication' do
24+
it 'sends correct token' do
1025
stub_request(:get, "#{test_site}/v1/app-workers/1")
11-
.with(headers: {
12-
'Accept' => 'application/vnd.api+json',
13-
'Accept-Encoding' => 'gzip,deflate',
14-
'Content-Type' => 'application/vnd.api+json',
15-
'User-Agent' => 'Faraday v1.1.0',
16-
'Authorization' => 'Bearer jwt-token',
17-
})
18-
.to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: { 'Content-Type' => 'application/json' })
26+
.with(headers: request_headers)
27+
.to_return(body: File.read('./spec/fixtures/app_worker.json'), headers: response_headers)
1928

2029
app_worker = CirroIO::Client::AppWorker.find(1).first
2130

2231
expect(app_worker.id).to eq('1')
2332
end
2433

2534
it 'sends token correctly for custom requests as well' do
26-
allow(JWT).to receive(:encode).and_return('jwt-token')
27-
2835
stub_request(:post, "#{test_site}/v1/bulk/custom-endpoint")
2936
.with(headers: {
3037
'Accept' => '*/*',
@@ -38,4 +45,27 @@
3845
described_class.custom_post('bulk/custom-endpoint', { a: :b })
3946
end
4047
end
48+
49+
describe 'configuration' do
50+
let(:other_site) { 'https://api.other.cirro.io' }
51+
let(:other_version) { 'vXXX' }
52+
53+
before do
54+
CirroIO::Client::AppWorker.site = "#{other_site}/#{other_version}"
55+
end
56+
57+
it 'supports multiple backends' do
58+
stub_request(:get, "#{other_site}/#{other_version}/app-workers/1")
59+
.with(headers: request_headers)
60+
.to_return(body: File.read('spec/fixtures/app_worker.json'), headers: response_headers)
61+
62+
expect(CirroIO::Client::AppWorker.find(1).first.id).to eq('1')
63+
64+
stub_request(:get, "#{test_site}/v1/app-users/3")
65+
.with(headers: request_headers)
66+
.to_return(body: File.read('spec/fixtures/app_user.json'), headers: response_headers)
67+
68+
expect(CirroIO::Client::AppUser.find(3).first.id).to eq('3')
69+
end
70+
end
4171
end

0 commit comments

Comments
 (0)