acfs 1.6.0 → 1.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -0
- data/README.md +16 -19
- data/acfs.gemspec +2 -0
- data/lib/acfs/adapter/typhoeus.rb +6 -4
- data/lib/acfs/configuration.rb +13 -3
- data/lib/acfs/errors.rb +1 -1
- data/lib/acfs/global.rb +2 -2
- data/lib/acfs/location.rb +1 -1
- data/lib/acfs/operation.rb +1 -1
- data/lib/acfs/request/callbacks.rb +1 -1
- data/lib/acfs/request.rb +1 -1
- data/lib/acfs/resource/attributes/uuid.rb +1 -1
- data/lib/acfs/resource/locatable.rb +2 -2
- data/lib/acfs/resource/query_methods.rb +2 -2
- data/lib/acfs/runner.rb +15 -15
- data/lib/acfs/stub.rb +34 -29
- data/lib/acfs/version.rb +2 -2
- data/spec/acfs/adapter/typhoeus_spec.rb +1 -1
- data/spec/acfs/collection_spec.rb +66 -41
- data/spec/acfs/configuration_spec.rb +22 -12
- data/spec/acfs/global_spec.rb +9 -7
- data/spec/acfs/middleware/json_spec.rb +8 -8
- data/spec/acfs/middleware/{msgpack_spec.rb → message_pack_spec.rb} +6 -6
- data/spec/acfs/operation_spec.rb +3 -2
- data/spec/acfs/request/callbacks_spec.rb +19 -10
- data/spec/acfs/request_spec.rb +15 -19
- data/spec/acfs/resource/attributes/boolean_spec.rb +32 -32
- data/spec/acfs/resource/attributes/date_time_spec.rb +16 -8
- data/spec/acfs/resource/attributes/dict_spec.rb +15 -9
- data/spec/acfs/resource/attributes/float_spec.rb +20 -10
- data/spec/acfs/resource/attributes/integer_spec.rb +10 -5
- data/spec/acfs/resource/attributes/list_spec.rb +13 -8
- data/spec/acfs/resource/attributes/uuid_spec.rb +12 -6
- data/spec/acfs/resource/attributes_spec.rb +33 -32
- data/spec/acfs/resource/dirty_spec.rb +6 -3
- data/spec/acfs/resource/initialization_spec.rb +4 -5
- data/spec/acfs/resource/loadable_spec.rb +3 -1
- data/spec/acfs/resource/locatable_spec.rb +24 -18
- data/spec/acfs/resource/{persistance_spec.rb → persistence_spec.rb} +114 -82
- data/spec/acfs/resource/query_methods_spec.rb +143 -110
- data/spec/acfs/resource/validation_spec.rb +34 -27
- data/spec/acfs/response/formats_spec.rb +8 -8
- data/spec/acfs/response/status_spec.rb +16 -9
- data/spec/acfs/runner_spec.rb +10 -8
- data/spec/acfs/service/middleware_spec.rb +3 -3
- data/spec/acfs/service_spec.rb +5 -4
- data/spec/acfs/singleton_resource_spec.rb +2 -1
- data/spec/acfs/stub_spec.rb +57 -53
- data/spec/acfs_spec.rb +111 -93
- data/spec/spec_helper.rb +1 -1
- data/spec/support/response.rb +2 -2
- data/spec/support/service.rb +1 -1
- data/spec/support/shared/find_callbacks.rb +14 -10
- metadata +9 -8
@@ -6,149 +6,174 @@ describe Acfs::Collection do
|
|
6
6
|
let(:model) { MyUser }
|
7
7
|
|
8
8
|
describe 'Pagination' do
|
9
|
+
subject do
|
10
|
+
Acfs.run
|
11
|
+
collection
|
12
|
+
end
|
13
|
+
|
9
14
|
let(:params) { {} }
|
10
15
|
let!(:collection) { model.all params }
|
11
16
|
|
12
|
-
subject { Acfs.run; collection }
|
13
|
-
|
14
17
|
context 'without explicit page parameter' do
|
15
18
|
before do
|
16
19
|
stub_request(:get, 'http://users.example.org/users')
|
17
20
|
.to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
|
18
21
|
headers: {
|
19
22
|
'X-Total-Pages' => '2',
|
20
|
-
'X-Total-Count' => '10'
|
21
|
-
})
|
23
|
+
'X-Total-Count' => '10',
|
24
|
+
},)
|
22
25
|
end
|
23
26
|
|
24
|
-
its(:total_pages) {
|
25
|
-
its(:current_page) {
|
26
|
-
its(:total_count) {
|
27
|
+
its(:total_pages) { is_expected.to eq 2 }
|
28
|
+
its(:current_page) { is_expected.to eq 1 }
|
29
|
+
its(:total_count) { is_expected.to eq 10 }
|
27
30
|
end
|
28
31
|
|
29
32
|
context 'with page parameter' do
|
30
33
|
let(:params) { {page: 2} }
|
34
|
+
|
31
35
|
before do
|
32
36
|
stub_request(:get, 'http://users.example.org/users?page=2')
|
33
37
|
.to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
|
34
38
|
headers: {
|
35
39
|
'X-Total-Pages' => '2',
|
36
|
-
'X-Total-Count' => '10'
|
37
|
-
})
|
40
|
+
'X-Total-Count' => '10',
|
41
|
+
},)
|
38
42
|
end
|
39
43
|
|
40
|
-
its(:total_pages) {
|
41
|
-
its(:current_page) {
|
42
|
-
its(:total_count) {
|
44
|
+
its(:total_pages) { is_expected.to eq 2 }
|
45
|
+
its(:current_page) { is_expected.to eq 2 }
|
46
|
+
its(:total_count) { is_expected.to eq 10 }
|
43
47
|
end
|
44
48
|
|
45
49
|
context 'with non-numerical page parameter' do
|
46
50
|
let(:params) { {page: 'e546f5'} }
|
51
|
+
|
47
52
|
before do
|
48
53
|
stub_request(:get, 'http://users.example.org/users?page=e546f5')
|
49
54
|
.to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
|
50
55
|
headers: {
|
51
56
|
'X-Total-Pages' => '2',
|
52
|
-
'X-Total-Count' => '10'
|
53
|
-
})
|
57
|
+
'X-Total-Count' => '10',
|
58
|
+
},)
|
54
59
|
end
|
55
60
|
|
56
|
-
its(:total_pages) {
|
57
|
-
its(:current_page) {
|
58
|
-
its(:total_count) {
|
61
|
+
its(:total_pages) { is_expected.to eq 2 }
|
62
|
+
its(:current_page) { is_expected.to eq 'e546f5' }
|
63
|
+
its(:total_count) { is_expected.to eq 10 }
|
59
64
|
end
|
60
65
|
|
61
66
|
describe '#next_page' do
|
67
|
+
subject(:request_page) do
|
68
|
+
Acfs.run
|
69
|
+
collection.next_page
|
70
|
+
end
|
71
|
+
|
62
72
|
before do
|
63
73
|
stub_request(:get, 'http://users.example.org/users')
|
64
74
|
.to_return response([{id: 1, name: 'Anon', age: 12, born_at: 'Berlin'}],
|
65
75
|
headers: {
|
66
76
|
'X-Total-Pages' => '2',
|
67
|
-
'Link' => '<http://users.example.org/users?page=2>; rel="next"'
|
68
|
-
})
|
77
|
+
'Link' => '<http://users.example.org/users?page=2>; rel="next"',
|
78
|
+
},)
|
69
79
|
end
|
80
|
+
|
70
81
|
let!(:req) do
|
71
82
|
stub_request(:get, 'http://users.example.org/users?page=2').to_return response([])
|
72
83
|
end
|
73
84
|
let!(:collection) { model.all }
|
74
|
-
subject { Acfs.run; collection.next_page }
|
75
85
|
|
76
|
-
it {
|
86
|
+
it { is_expected.to be_a Acfs::Collection }
|
77
87
|
|
78
|
-
it '
|
79
|
-
|
88
|
+
it 'has fetched page 2' do
|
89
|
+
request_page
|
80
90
|
Acfs.run
|
81
91
|
expect(req).to have_been_requested
|
82
92
|
end
|
83
93
|
end
|
84
94
|
|
85
95
|
describe '#prev_page' do
|
96
|
+
subject(:request_page) do
|
97
|
+
Acfs.run
|
98
|
+
collection.prev_page
|
99
|
+
end
|
100
|
+
|
86
101
|
before do
|
87
102
|
stub_request(:get, 'http://users.example.org/users?page=2')
|
88
103
|
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
89
104
|
headers: {
|
90
105
|
'X-Total-Pages' => '2',
|
91
|
-
'Link' => '<http://users.example.org/users>; rel="prev"'
|
92
|
-
})
|
106
|
+
'Link' => '<http://users.example.org/users>; rel="prev"',
|
107
|
+
},)
|
93
108
|
end
|
109
|
+
|
94
110
|
let!(:req) do
|
95
111
|
stub_request(:get, 'http://users.example.org/users').to_return response([])
|
96
112
|
end
|
97
113
|
let!(:collection) { model.all page: 2 }
|
98
|
-
subject { Acfs.run; collection.prev_page }
|
99
114
|
|
100
|
-
it {
|
115
|
+
it { is_expected.to be_a Acfs::Collection }
|
101
116
|
|
102
|
-
it '
|
103
|
-
|
117
|
+
it 'has fetched page 1' do
|
118
|
+
request_page
|
104
119
|
Acfs.run
|
105
120
|
expect(req).to have_been_requested
|
106
121
|
end
|
107
122
|
end
|
108
123
|
|
109
124
|
describe '#first_page' do
|
125
|
+
subject(:request_page) do
|
126
|
+
Acfs.run
|
127
|
+
collection.first_page
|
128
|
+
end
|
129
|
+
|
110
130
|
before do
|
111
131
|
stub_request(:get, 'http://users.example.org/users?page=2')
|
112
132
|
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
113
133
|
headers: {
|
114
134
|
'X-Total-Pages' => '2',
|
115
|
-
'Link' => '<http://users.example.org/users>; rel="first"'
|
116
|
-
})
|
135
|
+
'Link' => '<http://users.example.org/users>; rel="first"',
|
136
|
+
},)
|
117
137
|
end
|
138
|
+
|
118
139
|
let!(:req) do
|
119
140
|
stub_request(:get, 'http://users.example.org/users').to_return response([])
|
120
141
|
end
|
121
142
|
let!(:collection) { model.all page: 2 }
|
122
|
-
subject { Acfs.run; collection.first_page }
|
123
143
|
|
124
|
-
it {
|
144
|
+
it { is_expected.to be_a Acfs::Collection }
|
125
145
|
|
126
|
-
it '
|
127
|
-
|
146
|
+
it 'has fetched page 1' do
|
147
|
+
request_page
|
128
148
|
Acfs.run
|
129
149
|
expect(req).to have_been_requested
|
130
150
|
end
|
131
151
|
end
|
132
152
|
|
133
153
|
describe '#last_page' do
|
154
|
+
subject(:request_page) do
|
155
|
+
Acfs.run
|
156
|
+
collection.last_page
|
157
|
+
end
|
158
|
+
|
134
159
|
before do
|
135
160
|
stub_request(:get, 'http://users.example.org/users?page=2')
|
136
161
|
.to_return response([{id: 2, name: 'Anno', age: 1604, born_at: 'Santa Maria'}],
|
137
162
|
headers: {
|
138
163
|
'X-Total-Pages' => '2',
|
139
|
-
'Link' => '<http://users.example.org/users?page=12>; rel="last"'
|
140
|
-
})
|
164
|
+
'Link' => '<http://users.example.org/users?page=12>; rel="last"',
|
165
|
+
},)
|
141
166
|
end
|
167
|
+
|
142
168
|
let!(:req) do
|
143
169
|
stub_request(:get, 'http://users.example.org/users?page=12').to_return response([])
|
144
170
|
end
|
145
171
|
let!(:collection) { model.all page: 2 }
|
146
|
-
subject { Acfs.run; collection.last_page }
|
147
172
|
|
148
|
-
it {
|
173
|
+
it { is_expected.to be_a Acfs::Collection }
|
149
174
|
|
150
|
-
it '
|
151
|
-
|
175
|
+
it 'has fetched page 1' do
|
176
|
+
request_page
|
152
177
|
Acfs.run
|
153
178
|
expect(req).to have_been_requested
|
154
179
|
end
|
@@ -4,11 +4,16 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Acfs::Configuration do
|
6
6
|
let(:cfg) { Acfs::Configuration.new }
|
7
|
-
|
8
|
-
|
7
|
+
|
8
|
+
around do |example|
|
9
|
+
configuration = Acfs::Configuration.current.dup
|
10
|
+
example.run
|
11
|
+
ensure
|
12
|
+
Acfs::Configuration.set(configuration)
|
13
|
+
end
|
9
14
|
|
10
15
|
describe 'Acfs.configure' do
|
11
|
-
it '
|
16
|
+
it 'invokes configure on current configuration' do
|
12
17
|
expect(Acfs::Configuration.current).to receive(:configure).once.and_call_original
|
13
18
|
|
14
19
|
Acfs.configure do |c|
|
@@ -18,26 +23,31 @@ describe Acfs::Configuration do
|
|
18
23
|
end
|
19
24
|
|
20
25
|
describe '.load' do
|
21
|
-
it '
|
26
|
+
it 'is able to load YAML' do
|
22
27
|
cfg.configure do
|
23
28
|
load 'spec/fixtures/config.yml'
|
24
29
|
end
|
25
30
|
|
26
|
-
expect(cfg.locate(UserService).to_s).to
|
27
|
-
expect(cfg.locate(CommentService).to_s).to
|
31
|
+
expect(cfg.locate(UserService).to_s).to eq 'http://localhost:3001/'
|
32
|
+
expect(cfg.locate(CommentService).to_s).to eq 'http://localhost:3002/'
|
28
33
|
end
|
29
34
|
|
30
35
|
context 'with RACK_ENV' do
|
31
|
-
|
32
|
-
|
36
|
+
around do |example|
|
37
|
+
env = ENV['RACK_ENV']
|
38
|
+
ENV['RACK_ENV'] = 'production'
|
39
|
+
example.run
|
40
|
+
ensure
|
41
|
+
ENV['RACK_ENV'] = env
|
42
|
+
end
|
33
43
|
|
34
|
-
it '
|
44
|
+
it 'loads ENV block' do
|
35
45
|
cfg.configure do
|
36
46
|
load 'spec/fixtures/config.yml'
|
37
47
|
end
|
38
48
|
|
39
|
-
expect(cfg.locate(UserService).to_s).to
|
40
|
-
expect(cfg.locate(CommentService).to_s).to
|
49
|
+
expect(cfg.locate(UserService).to_s).to eq 'http://user.example.org/'
|
50
|
+
expect(cfg.locate(CommentService).to_s).to eq 'http://comment.example.org/'
|
41
51
|
end
|
42
52
|
end
|
43
53
|
end
|
@@ -45,7 +55,7 @@ describe Acfs::Configuration do
|
|
45
55
|
describe '#adapter' do
|
46
56
|
let(:object) { Object.new }
|
47
57
|
|
48
|
-
it '
|
58
|
+
it 'is a accessor' do
|
49
59
|
cfg.adapter = object
|
50
60
|
expect(cfg.adapter).to eq object
|
51
61
|
end
|
data/spec/acfs/global_spec.rb
CHANGED
@@ -28,7 +28,8 @@ describe ::Acfs::Global do
|
|
28
28
|
before do
|
29
29
|
::ActiveSupport::Notifications.subscribe 'acfs.run', collector
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
|
+
it 'triggers event' do
|
32
33
|
Acfs.run
|
33
34
|
expect(collector.events).to have(1).items
|
34
35
|
end
|
@@ -38,7 +39,8 @@ describe ::Acfs::Global do
|
|
38
39
|
before do
|
39
40
|
::ActiveSupport::Notifications.subscribe 'acfs.reset', collector
|
40
41
|
end
|
41
|
-
|
42
|
+
|
43
|
+
it 'triggers event' do
|
42
44
|
Acfs.reset
|
43
45
|
expect(collector.events).to have(1).items
|
44
46
|
end
|
@@ -50,11 +52,11 @@ describe ::Acfs::Global do
|
|
50
52
|
stub_request(:get, %r{http://users.example.org/users/\d+}).to_return(
|
51
53
|
status: 200,
|
52
54
|
body: '{}',
|
53
|
-
headers: {'Content-Type' => 'application/json'}
|
55
|
+
headers: {'Content-Type' => 'application/json'},
|
54
56
|
)
|
55
57
|
end
|
56
58
|
|
57
|
-
it '
|
59
|
+
it 'invokes when both resources' do
|
58
60
|
user1 = MyUser.find 1
|
59
61
|
user2 = MyUser.find 2
|
60
62
|
|
@@ -64,7 +66,7 @@ describe ::Acfs::Global do
|
|
64
66
|
end.to yield_with_args(user1, user2)
|
65
67
|
end
|
66
68
|
|
67
|
-
it '
|
69
|
+
it 'invokes when both resources when loaded' do
|
68
70
|
user1 = MyUser.find 1
|
69
71
|
user2 = MyUser.find 2
|
70
72
|
|
@@ -82,7 +84,7 @@ describe ::Acfs::Global do
|
|
82
84
|
.to_return(
|
83
85
|
status: 200,
|
84
86
|
body: '{}',
|
85
|
-
headers: {'Content-Type' => 'application/json'}
|
87
|
+
headers: {'Content-Type' => 'application/json'},
|
86
88
|
)
|
87
89
|
end
|
88
90
|
|
@@ -125,7 +127,7 @@ describe ::Acfs::Global do
|
|
125
127
|
runner1 = Thread.new { acfs.runner }.value
|
126
128
|
runner2 = Thread.new { acfs.runner }.value
|
127
129
|
|
128
|
-
expect(runner1).
|
130
|
+
expect(runner1).not_to equal runner2
|
129
131
|
end
|
130
132
|
|
131
133
|
it 'uses configurated adapter' do
|
@@ -16,15 +16,15 @@ describe Acfs::Middleware::JSON do
|
|
16
16
|
|
17
17
|
describe 'encode' do
|
18
18
|
context 'with not serialized request' do
|
19
|
-
it '
|
19
|
+
it 'sets Content-Type' do
|
20
20
|
expect(request.headers['Content-Type']).to eq 'application/json'
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'appends Accept header' do
|
24
24
|
expect(request.headers['Accept']).to eq 'application/json;q=1'
|
25
25
|
end
|
26
26
|
|
27
|
-
it '
|
27
|
+
it 'serializes data to JSON' do
|
28
28
|
expect(JSON.parse(request.body)).to eq data.map(&:stringify_keys)
|
29
29
|
end
|
30
30
|
end
|
@@ -38,7 +38,7 @@ describe Acfs::Middleware::JSON do
|
|
38
38
|
end.new
|
39
39
|
end
|
40
40
|
|
41
|
-
it '
|
41
|
+
it 'serializes data with #to_json' do
|
42
42
|
expect(JSON.parse(request.body)).to eq 'a' => 1, 'b' => 2
|
43
43
|
end
|
44
44
|
end
|
@@ -49,10 +49,10 @@ describe Acfs::Middleware::JSON do
|
|
49
49
|
let(:headers) { {'Content-Type' => 'application/json; charset=utf-8'} }
|
50
50
|
let(:body) { data.to_json }
|
51
51
|
|
52
|
-
it '
|
52
|
+
it 'decodes body data' do
|
53
53
|
request.complete! response
|
54
54
|
|
55
|
-
expect(response.data).to
|
55
|
+
expect(response.data).to eq data.map(&:stringify_keys)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -60,7 +60,7 @@ describe Acfs::Middleware::JSON do
|
|
60
60
|
let(:headers) { {'Content-Type' => 'application/json'} }
|
61
61
|
let(:body) { data.to_json[4..-4] }
|
62
62
|
|
63
|
-
it '
|
63
|
+
it 'raises an error' do
|
64
64
|
expect { request.complete! response }.to raise_error(::JSON::ParserError)
|
65
65
|
end
|
66
66
|
end
|
@@ -69,7 +69,7 @@ describe Acfs::Middleware::JSON do
|
|
69
69
|
let(:headers) { {'Content-Type' => 'application/text'} }
|
70
70
|
let(:body) { data.to_json }
|
71
71
|
|
72
|
-
it '
|
72
|
+
it 'does not decode non-JSON encoded responses' do
|
73
73
|
request.complete! response
|
74
74
|
|
75
75
|
expect(response.data).to be_nil
|
@@ -16,23 +16,23 @@ describe Acfs::Middleware::MessagePack do
|
|
16
16
|
|
17
17
|
describe 'encode' do
|
18
18
|
context 'with not serialized request' do
|
19
|
-
it '
|
19
|
+
it 'sets Content-Type' do
|
20
20
|
expect(request.headers['Content-Type']).to eq 'application/x-msgpack'
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
23
|
+
it 'appends Accept header' do
|
24
24
|
expect(request.headers['Accept']).to eq 'application/x-msgpack;q=1'
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'with JSON chained' do
|
28
28
|
let(:decoder) { Acfs::Middleware::JSON.new super(), q: 0.5 }
|
29
29
|
|
30
|
-
it '
|
30
|
+
it 'appends to Accept header' do
|
31
31
|
expect(request.headers['Accept']).to eq 'application/json;q=0.5,application/x-msgpack;q=1'
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
it '
|
35
|
+
it 'serializes data to MessagePack' do
|
36
36
|
expect(MessagePack.unpack(request.body)).to eq data.map(&:stringify_keys)
|
37
37
|
end
|
38
38
|
end
|
@@ -42,7 +42,7 @@ describe Acfs::Middleware::MessagePack do
|
|
42
42
|
let(:headers) { {'Content-Type' => 'application/x-msgpack'} }
|
43
43
|
let(:body) { MessagePack.pack data }
|
44
44
|
|
45
|
-
it '
|
45
|
+
it 'decodes body data' do
|
46
46
|
request.complete! response
|
47
47
|
|
48
48
|
expect(response.data).to be == data.map(&:stringify_keys)
|
@@ -53,7 +53,7 @@ describe Acfs::Middleware::MessagePack do
|
|
53
53
|
let(:headers) { {'Content-Type' => 'application/text'} }
|
54
54
|
let(:body) { data.to_json }
|
55
55
|
|
56
|
-
it '
|
56
|
+
it 'does not decode non-MessagePack encoded responses' do
|
57
57
|
request.complete! response
|
58
58
|
|
59
59
|
expect(response.data).to be_nil
|
data/spec/acfs/operation_spec.rb
CHANGED
@@ -5,8 +5,9 @@ require 'spec_helper'
|
|
5
5
|
describe ::Acfs::Operation do
|
6
6
|
let(:operation) { described_class.new MyUser, :read, params: {id: 0} }
|
7
7
|
|
8
|
-
|
8
|
+
describe '#request' do
|
9
9
|
subject { operation.request }
|
10
|
-
|
10
|
+
|
11
|
+
its(:operation) { is_expected.to eq operation }
|
11
12
|
end
|
12
13
|
end
|
@@ -7,42 +7,51 @@ describe Acfs::Request::Callbacks do
|
|
7
7
|
let(:request) { Acfs::Request.new('fubar') }
|
8
8
|
|
9
9
|
describe '#on_complete' do
|
10
|
-
it '
|
10
|
+
it 'stores a given callback' do
|
11
11
|
request.on_complete(&callback)
|
12
12
|
|
13
13
|
expect(request.callbacks).to have(1).item
|
14
|
-
expect(request.callbacks[0]).to
|
14
|
+
expect(request.callbacks[0]).to eq callback
|
15
15
|
end
|
16
16
|
|
17
|
-
it '
|
17
|
+
it 'stores multiple callback' do
|
18
18
|
request.on_complete {|_res| 'abc' }
|
19
19
|
request.on_complete(&callback)
|
20
20
|
|
21
21
|
expect(request.callbacks).to have(2).item
|
22
|
-
expect(request.callbacks[0]).to
|
22
|
+
expect(request.callbacks[0]).to eq callback
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
describe '#complete!' do
|
27
27
|
let(:response) { Acfs::Response.new(request) }
|
28
28
|
|
29
|
-
it '
|
29
|
+
it 'triggers registered callbacks with given response' do
|
30
30
|
expect(callback).to receive(:call).with(response, kind_of(Proc))
|
31
31
|
|
32
32
|
request.on_complete(&callback)
|
33
33
|
request.complete! response
|
34
34
|
end
|
35
35
|
|
36
|
-
it '
|
36
|
+
it 'triggers multiple callback in reverted insertion order' do
|
37
37
|
check = []
|
38
38
|
|
39
|
-
request.on_complete
|
40
|
-
|
41
|
-
|
39
|
+
request.on_complete do |res, nxt|
|
40
|
+
check << 1
|
41
|
+
nxt.call res
|
42
|
+
end
|
43
|
+
request.on_complete do |res, nxt|
|
44
|
+
check << 2
|
45
|
+
nxt.call res
|
46
|
+
end
|
47
|
+
request.on_complete do |res, nxt|
|
48
|
+
check << 3
|
49
|
+
nxt.call res
|
50
|
+
end
|
42
51
|
|
43
52
|
request.complete! response
|
44
53
|
|
45
|
-
expect(check).to
|
54
|
+
expect(check).to eq [3, 2, 1]
|
46
55
|
end
|
47
56
|
end
|
48
57
|
end
|
data/spec/acfs/request_spec.rb
CHANGED
@@ -12,15 +12,15 @@ describe Acfs::Request do
|
|
12
12
|
let(:request) { Acfs::Request.new(url, **options) }
|
13
13
|
|
14
14
|
describe '#url' do
|
15
|
-
it '
|
16
|
-
expect(request.url).to
|
15
|
+
it 'returns request URL' do
|
16
|
+
expect(request.url).to eq url
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'with parameters' do
|
20
20
|
let(:params) { {id: 10} }
|
21
21
|
|
22
|
-
it '
|
23
|
-
expect(request.url).to
|
22
|
+
it 'returns URL without query' do
|
23
|
+
expect(request.url).to eq url.to_s
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -28,8 +28,8 @@ describe Acfs::Request do
|
|
28
28
|
describe '#headers' do
|
29
29
|
let(:headers) { {'Accept' => 'application/json'} }
|
30
30
|
|
31
|
-
it '
|
32
|
-
expect(request.headers).to
|
31
|
+
it 'returns request headers' do
|
32
|
+
expect(request.headers).to eq headers
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -37,43 +37,39 @@ describe Acfs::Request do
|
|
37
37
|
context 'when nil given' do
|
38
38
|
let(:method) { nil }
|
39
39
|
|
40
|
-
it '
|
41
|
-
expect(request.method).to
|
40
|
+
it 'defaults to :get' do
|
41
|
+
expect(request.method).to eq :get
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
it '
|
46
|
-
expect(request.method).to
|
45
|
+
it 'returns request method' do
|
46
|
+
expect(request.method).to eq method
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
50
|
describe '#params' do
|
51
51
|
let(:params) { {id: 10} }
|
52
52
|
|
53
|
-
it '
|
54
|
-
expect(request.params).to
|
53
|
+
it 'returns request headers' do
|
54
|
+
expect(request.params).to eq params
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
describe '#data' do
|
59
59
|
let(:data) { {id: 10, name: 'Anon'} }
|
60
60
|
|
61
|
-
it '
|
62
|
-
expect(request.data).to
|
61
|
+
it 'returns request data' do
|
62
|
+
expect(request.data).to eq data
|
63
63
|
end
|
64
|
-
end
|
65
64
|
|
66
|
-
describe '#data' do
|
67
65
|
context 'with data' do
|
68
|
-
let(:data) { {id: 10, name: 'Anon'} }
|
69
|
-
|
70
66
|
it { expect(request).to be_data }
|
71
67
|
end
|
72
68
|
|
73
69
|
context 'without data' do
|
74
70
|
let(:data) { nil }
|
75
71
|
|
76
|
-
it { expect(request).
|
72
|
+
it { expect(request).not_to be_data }
|
77
73
|
end
|
78
74
|
end
|
79
75
|
end
|