restful_resource 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +20 -0
- data/.codeclimate.yml +3 -0
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +203 -0
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/Rakefile +5 -5
- data/lib/restful_resource/associations.rb +10 -8
- data/lib/restful_resource/base.rb +36 -33
- data/lib/restful_resource/http_client.rb +50 -48
- data/lib/restful_resource/instrumentation.rb +17 -20
- data/lib/restful_resource/null_logger.rb +1 -2
- data/lib/restful_resource/open_object.rb +2 -2
- data/lib/restful_resource/paginated_array.rb +3 -1
- data/lib/restful_resource/rails_validations.rb +12 -12
- data/lib/restful_resource/redirections.rb +5 -6
- data/lib/restful_resource/request.rb +0 -1
- data/lib/restful_resource/resource_id_missing_error.rb +1 -1
- data/lib/restful_resource/response.rb +4 -2
- data/lib/restful_resource/version.rb +1 -1
- data/restful_resource.gemspec +26 -25
- data/spec/fixtures.rb +7 -7
- data/spec/restful_resource/associations_spec.rb +23 -21
- data/spec/restful_resource/base_authorization_spec.rb +6 -7
- data/spec/restful_resource/base_spec.rb +134 -117
- data/spec/restful_resource/http_client_configuration_spec.rb +20 -19
- data/spec/restful_resource/http_client_spec.rb +38 -38
- data/spec/restful_resource/open_object_spec.rb +8 -8
- data/spec/restful_resource/rails_validations_spec.rb +68 -68
- data/spec/restful_resource/redirections_spec.rb +26 -26
- data/spec/spec_helper.rb +3 -4
- metadata +44 -13
- data/circle.yml +0 -3
@@ -16,40 +16,40 @@ RSpec.describe RestfulResource::HttpClient do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe 'Basic HTTP' do
|
19
|
-
it '
|
19
|
+
it 'executes get' do
|
20
20
|
connection = faraday_connection do |stubs|
|
21
|
-
stubs.get('http://httpbin.org/get') { |
|
21
|
+
stubs.get('http://httpbin.org/get') { |_env| [200, {}, nil] }
|
22
22
|
end
|
23
23
|
|
24
24
|
response = http_client(connection).get('http://httpbin.org/get')
|
25
25
|
expect(response.status).to eq 200
|
26
26
|
end
|
27
27
|
|
28
|
-
it '
|
28
|
+
it 'executes put' do
|
29
29
|
connection = faraday_connection do |stubs|
|
30
30
|
# Note: request body is serialized as url-encoded so the stub body must be in the same format to match
|
31
|
-
stubs.put('http://httpbin.org/put', 'name=Alfred') { |
|
31
|
+
stubs.put('http://httpbin.org/put', 'name=Alfred') { |_env| [200, {}, nil] }
|
32
32
|
end
|
33
33
|
|
34
34
|
response = http_client(connection).put('http://httpbin.org/put', data: { name: 'Alfred' })
|
35
35
|
expect(response.status).to eq 200
|
36
36
|
end
|
37
37
|
|
38
|
-
it '
|
38
|
+
it 'executes post' do
|
39
39
|
connection = faraday_connection do |stubs|
|
40
40
|
# Note: request body is serialized as url-encoded so the stub body must be in the same format to match
|
41
|
-
stubs.post('http://httpbin.org/post', 'name=Alfred') { |
|
41
|
+
stubs.post('http://httpbin.org/post', 'name=Alfred') { |_env| [200, {}, %("name": "Alfred")] }
|
42
42
|
end
|
43
43
|
|
44
44
|
response = http_client(connection).post('http://httpbin.org/post', data: { name: 'Alfred' })
|
45
45
|
|
46
|
-
expect(response.body).to include
|
46
|
+
expect(response.body).to include 'name": "Alfred'
|
47
47
|
expect(response.status).to eq 200
|
48
48
|
end
|
49
49
|
|
50
|
-
it '
|
50
|
+
it 'executes delete' do
|
51
51
|
connection = faraday_connection do |stubs|
|
52
|
-
stubs.delete('http://httpbin.org/delete') { |
|
52
|
+
stubs.delete('http://httpbin.org/delete') { |_env| [200, {}, nil] }
|
53
53
|
end
|
54
54
|
|
55
55
|
response = http_client(connection).delete('http://httpbin.org/delete')
|
@@ -59,7 +59,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
59
59
|
|
60
60
|
it 'put should raise error 409' do
|
61
61
|
connection = faraday_connection do |stubs|
|
62
|
-
stubs.put('http://httpbin.org/status/409') { |
|
62
|
+
stubs.put('http://httpbin.org/status/409') { |_env| [409, {}, nil] }
|
63
63
|
end
|
64
64
|
|
65
65
|
expect { http_client(connection).put('http://httpbin.org/status/409') }.to raise_error(RestfulResource::HttpClient::Conflict)
|
@@ -67,7 +67,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
67
67
|
|
68
68
|
it 'put should raise error 422' do
|
69
69
|
connection = faraday_connection do |stubs|
|
70
|
-
stubs.put('http://httpbin.org/status/422') { |
|
70
|
+
stubs.put('http://httpbin.org/status/422') { |_env| [422, {}, nil] }
|
71
71
|
end
|
72
72
|
|
73
73
|
expect { http_client(connection).put('http://httpbin.org/status/422') }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
|
@@ -75,7 +75,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
75
75
|
|
76
76
|
it 'post should raise error 422' do
|
77
77
|
connection = faraday_connection do |stubs|
|
78
|
-
stubs.post('http://httpbin.org/status/422') { |
|
78
|
+
stubs.post('http://httpbin.org/status/422') { |_env| [422, {}, nil] }
|
79
79
|
end
|
80
80
|
|
81
81
|
expect { http_client(connection).post('http://httpbin.org/status/422') }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
|
@@ -83,7 +83,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
83
83
|
|
84
84
|
it 'post should raise error 429' do
|
85
85
|
connection = faraday_connection do |stubs|
|
86
|
-
stubs.post('http://httpbin.org/status/429') { |
|
86
|
+
stubs.post('http://httpbin.org/status/429') { |_env| [429, {}, nil] }
|
87
87
|
end
|
88
88
|
|
89
89
|
expect { http_client(connection).post('http://httpbin.org/status/429') }.to raise_error(RestfulResource::HttpClient::TooManyRequests)
|
@@ -91,7 +91,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
91
91
|
|
92
92
|
it 'put should raise error 502' do
|
93
93
|
connection = faraday_connection do |stubs|
|
94
|
-
stubs.put('http://httpbin.org/status/502') { |
|
94
|
+
stubs.put('http://httpbin.org/status/502') { |_env| [502, {}, nil] }
|
95
95
|
end
|
96
96
|
|
97
97
|
expect { http_client(connection).put('http://httpbin.org/status/502') }.to raise_error(RestfulResource::HttpClient::BadGateway)
|
@@ -99,7 +99,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
99
99
|
|
100
100
|
it 'post should raise error 502' do
|
101
101
|
connection = faraday_connection do |stubs|
|
102
|
-
stubs.post('http://httpbin.org/status/502') { |
|
102
|
+
stubs.post('http://httpbin.org/status/502') { |_env| [502, {}, nil] }
|
103
103
|
end
|
104
104
|
|
105
105
|
expect { http_client(connection).post('http://httpbin.org/status/502') }.to raise_error(RestfulResource::HttpClient::BadGateway)
|
@@ -107,7 +107,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
107
107
|
|
108
108
|
it 'put should raise error 503' do
|
109
109
|
connection = faraday_connection do |stubs|
|
110
|
-
stubs.put('http://httpbin.org/status/503') { |
|
110
|
+
stubs.put('http://httpbin.org/status/503') { |_env| [503, {}, nil] }
|
111
111
|
end
|
112
112
|
|
113
113
|
expect { http_client(connection).put('http://httpbin.org/status/503') }.to raise_error(RestfulResource::HttpClient::ServiceUnavailable)
|
@@ -115,18 +115,18 @@ RSpec.describe RestfulResource::HttpClient do
|
|
115
115
|
|
116
116
|
it 'post should raise error 503' do
|
117
117
|
connection = faraday_connection do |stubs|
|
118
|
-
stubs.post('http://httpbin.org/status/503') { |
|
118
|
+
stubs.post('http://httpbin.org/status/503') { |_env| [503, {}, nil] }
|
119
119
|
end
|
120
120
|
|
121
121
|
expect { http_client(connection).post('http://httpbin.org/status/503') }.to raise_error(RestfulResource::HttpClient::ServiceUnavailable)
|
122
122
|
end
|
123
123
|
|
124
|
-
it '
|
124
|
+
it 'raises error on 404' do
|
125
125
|
connection = faraday_connection do |stubs|
|
126
|
-
stubs.get('http://httpbin.org/status/404') { |
|
127
|
-
stubs.post('http://httpbin.org/status/404') { |
|
128
|
-
stubs.put('http://httpbin.org/status/404') { |
|
129
|
-
stubs.delete('http://httpbin.org/status/404') { |
|
126
|
+
stubs.get('http://httpbin.org/status/404') { |_env| [404, {}, nil] }
|
127
|
+
stubs.post('http://httpbin.org/status/404') { |_env| [404, {}, nil] }
|
128
|
+
stubs.put('http://httpbin.org/status/404') { |_env| [404, {}, nil] }
|
129
|
+
stubs.delete('http://httpbin.org/status/404') { |_env| [404, {}, nil] }
|
130
130
|
end
|
131
131
|
|
132
132
|
expect { http_client(connection).get('http://httpbin.org/status/404') }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
@@ -135,17 +135,17 @@ RSpec.describe RestfulResource::HttpClient do
|
|
135
135
|
expect { http_client(connection).post('http://httpbin.org/status/404', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
136
136
|
end
|
137
137
|
|
138
|
-
it '
|
138
|
+
it 'raises Faraday::ConnectionFailed errors' do
|
139
139
|
connection = faraday_connection do |stubs|
|
140
|
-
stubs.get('https://localhost:3005') {|
|
140
|
+
stubs.get('https://localhost:3005') { |_env| raise Faraday::ConnectionFailed, nil }
|
141
141
|
end
|
142
142
|
|
143
143
|
expect { http_client(connection).get('https://localhost:3005') }.to raise_error(Faraday::ConnectionFailed)
|
144
144
|
end
|
145
145
|
|
146
|
-
it '
|
146
|
+
it 'raises Timeout error' do
|
147
147
|
connection = faraday_connection do |stubs|
|
148
|
-
stubs.get('https://localhost:3005') {|
|
148
|
+
stubs.get('https://localhost:3005') { |_env| raise Faraday::TimeoutError, nil }
|
149
149
|
end
|
150
150
|
|
151
151
|
expect { http_client(connection).get('https://localhost:3005') }.to raise_error(RestfulResource::HttpClient::Timeout)
|
@@ -153,7 +153,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
153
153
|
|
154
154
|
it 'raises ClientError when a client errors with no response' do
|
155
155
|
connection = faraday_connection do |stubs|
|
156
|
-
stubs.get('https://localhost:3005') {|
|
156
|
+
stubs.get('https://localhost:3005') { |_env| raise Faraday::ClientError, nil }
|
157
157
|
end
|
158
158
|
|
159
159
|
expect { http_client(connection).get('https://localhost:3005') }.to raise_error(RestfulResource::HttpClient::ClientError)
|
@@ -161,7 +161,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
161
161
|
|
162
162
|
it 'raises OtherHttpError for other status response codes' do
|
163
163
|
connection = faraday_connection do |stubs|
|
164
|
-
stubs.get('http://httpbin.org/status/418') { |
|
164
|
+
stubs.get('http://httpbin.org/status/418') { |_env| [418, {}, nil] }
|
165
165
|
end
|
166
166
|
|
167
167
|
expect { http_client(connection).get('http://httpbin.org/status/418') }.to raise_error(RestfulResource::HttpClient::OtherHttpError)
|
@@ -174,12 +174,12 @@ RSpec.describe RestfulResource::HttpClient do
|
|
174
174
|
described_class.new(connection: connection, username: 'user', password: 'passwd')
|
175
175
|
end
|
176
176
|
|
177
|
-
it '
|
177
|
+
it 'executes authenticated get' do
|
178
178
|
connection = faraday_connection do |stubs|
|
179
|
-
stubs.get('http://httpbin.org/basic-auth/user/passwd') { |
|
179
|
+
stubs.get('http://httpbin.org/basic-auth/user/passwd') { |_env| [200, {}, nil] }
|
180
180
|
end
|
181
181
|
|
182
|
-
response = http_client(connection).get('http://httpbin.org/basic-auth/user/passwd', headers: {
|
182
|
+
response = http_client(connection).get('http://httpbin.org/basic-auth/user/passwd', headers: { 'Authorization' => 'Basic dXNlcjpwYXNzd2Q=' })
|
183
183
|
|
184
184
|
expect(response.status).to eq 200
|
185
185
|
end
|
@@ -190,12 +190,12 @@ RSpec.describe RestfulResource::HttpClient do
|
|
190
190
|
described_class.new(connection: connection, auth_token: 'abc123')
|
191
191
|
end
|
192
192
|
|
193
|
-
it '
|
193
|
+
it 'executes authenticated get' do
|
194
194
|
connection = faraday_connection do |stubs|
|
195
|
-
stubs.get('http://httpbin.org/bearer',
|
195
|
+
stubs.get('http://httpbin.org/bearer', 'Authorization' => 'Bearer abc123') { |_env| [200, {}, nil] }
|
196
196
|
end
|
197
197
|
|
198
|
-
response = http_client(connection).get('http://httpbin.org/bearer', headers: { 'Authorization' => 'Bearer abc123'})
|
198
|
+
response = http_client(connection).get('http://httpbin.org/bearer', headers: { 'Authorization' => 'Bearer abc123' })
|
199
199
|
|
200
200
|
expect(response.status).to eq 200
|
201
201
|
end
|
@@ -205,7 +205,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
205
205
|
describe 'Headers' do
|
206
206
|
it 'uses custom headers' do
|
207
207
|
connection = faraday_connection do |stubs|
|
208
|
-
stubs.get('http://httpbin.org/get',
|
208
|
+
stubs.get('http://httpbin.org/get', 'Cache-Control' => 'no-cache') { |_env| [200, {}, nil] }
|
209
209
|
end
|
210
210
|
|
211
211
|
response = http_client(connection).get('http://httpbin.org/get', headers: { cache_control: 'no-cache' })
|
@@ -221,7 +221,7 @@ RSpec.describe RestfulResource::HttpClient do
|
|
221
221
|
it 'sets a default user-agent header' do
|
222
222
|
connection = faraday_connection do |stubs|
|
223
223
|
user_agent = "carwow/internal RestfulResource/#{RestfulResource::VERSION} Faraday/#{Faraday::VERSION}"
|
224
|
-
stubs.get('http://httpbin.org/get',
|
224
|
+
stubs.get('http://httpbin.org/get', 'User-Agent' => user_agent) { |_env| [200, {}, nil] }
|
225
225
|
end
|
226
226
|
|
227
227
|
response = http_client(connection).get('http://httpbin.org/get')
|
@@ -232,10 +232,10 @@ RSpec.describe RestfulResource::HttpClient do
|
|
232
232
|
it 'sets a default user-agent header including app name' do
|
233
233
|
connection = faraday_connection do |stubs|
|
234
234
|
user_agent = "carwow/internal RestfulResource/#{RestfulResource::VERSION} (my-app) Faraday/#{Faraday::VERSION}"
|
235
|
-
stubs.get('http://httpbin.org/get',
|
235
|
+
stubs.get('http://httpbin.org/get', 'User-Agent' => user_agent) { |_env| [200, {}, nil] }
|
236
236
|
end
|
237
237
|
|
238
|
-
response = http_client(connection, app_name:
|
238
|
+
response = http_client(connection, app_name: 'my-app').get('http://httpbin.org/get')
|
239
239
|
|
240
240
|
expect(response.status).to eq 200
|
241
241
|
end
|
@@ -1,23 +1,23 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe RestfulResource::OpenObject do
|
4
|
-
it
|
5
|
-
object =
|
4
|
+
it 'acts as an openstruct' do
|
5
|
+
object = described_class.new(name: 'David', surname: 'Santoro')
|
6
6
|
|
7
7
|
expect(object.name).to eq 'David'
|
8
8
|
expect(object.surname).to eq 'Santoro'
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
12
|
-
object =
|
11
|
+
it "raises an error when accessing a field that doesn't exist" do
|
12
|
+
object = described_class.new(name: 'David', surname: 'Santoro')
|
13
13
|
|
14
14
|
expect { object.age }.to raise_error(NoMethodError)
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
18
|
-
a =
|
19
|
-
b =
|
20
|
-
c =
|
17
|
+
it 'implements equality operators correctly' do
|
18
|
+
a = described_class.new(name: 'Joe', age: 13)
|
19
|
+
b = described_class.new(name: 'Joe', age: 13)
|
20
|
+
c = described_class.new(name: 'Mike', age: 13)
|
21
21
|
|
22
22
|
list = [a, b, c]
|
23
23
|
|
@@ -1,176 +1,176 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe RestfulResource::RailsValidations do
|
4
|
-
before
|
5
|
-
@mock_http = double(
|
6
|
-
RestfulResource::Base.configure(base_url:
|
4
|
+
before do
|
5
|
+
@mock_http = double('mock_http')
|
6
|
+
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
|
7
7
|
allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
|
8
8
|
end
|
9
9
|
|
10
|
-
context
|
11
|
-
before
|
12
|
-
data = {name: 'Barak'}
|
13
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Barak'}.to_json)
|
14
|
-
expect_put(
|
10
|
+
context '#put without errors' do
|
11
|
+
before do
|
12
|
+
data = { name: 'Barak' }
|
13
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Barak' }.to_json)
|
14
|
+
expect_put('http://api.carwow.co.uk/dealers/1', expected_response, data: data)
|
15
15
|
|
16
16
|
@object = Dealer.put(1, data: data)
|
17
17
|
end
|
18
18
|
|
19
|
-
it '
|
19
|
+
it 'returns object' do
|
20
20
|
expect(@object.name).to eq 'Barak'
|
21
21
|
end
|
22
22
|
|
23
|
-
it '
|
24
|
-
expect(@object
|
23
|
+
it 'returns valid object' do
|
24
|
+
expect(@object).to be_valid
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
|
-
context
|
28
|
+
context '#put with errors' do
|
29
29
|
before do
|
30
|
-
data = {name: 'Leonardo'}
|
30
|
+
data = { name: 'Leonardo' }
|
31
31
|
@error = 'Cannot use Ninja Turtles names'
|
32
|
-
expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
|
33
|
-
expect_put_with_unprocessable_entity(
|
32
|
+
expected_response = RestfulResource::Response.new(body: { errors: [@error] }.to_json)
|
33
|
+
expect_put_with_unprocessable_entity('http://api.carwow.co.uk/dealers/1', expected_response, data: data)
|
34
34
|
|
35
35
|
@object = Dealer.put(1, data: data)
|
36
36
|
end
|
37
37
|
|
38
|
-
it
|
38
|
+
it 'has an error' do
|
39
39
|
expect(@object.errors.count).to eq 1
|
40
40
|
end
|
41
41
|
|
42
|
-
it '
|
42
|
+
it 'has correct error' do
|
43
43
|
expect(@object.errors.first).to eq @error
|
44
44
|
end
|
45
45
|
|
46
|
-
it '
|
46
|
+
it 'returns properly built object' do
|
47
47
|
expect(@object.name).to eq 'Leonardo'
|
48
48
|
end
|
49
49
|
|
50
|
-
it '
|
51
|
-
expect(@object
|
50
|
+
it 'returns not valid object' do
|
51
|
+
expect(@object).not_to be_valid
|
52
52
|
end
|
53
53
|
|
54
|
-
it '
|
55
|
-
data = {name: 'Michelangelo'}
|
54
|
+
it 'handles errors returned as root object' do
|
55
|
+
data = { name: 'Michelangelo' }
|
56
56
|
expected_response = RestfulResource::Response.new(body: @error.to_json)
|
57
|
-
expect_put_with_unprocessable_entity(
|
57
|
+
expect_put_with_unprocessable_entity('http://api.carwow.co.uk/dealers/1', expected_response, data: data)
|
58
58
|
|
59
59
|
@object = Dealer.put(1, data: data)
|
60
|
-
expect(@object
|
60
|
+
expect(@object).not_to be_valid
|
61
61
|
expect(@object.errors).to eq @error
|
62
62
|
end
|
63
63
|
|
64
|
-
it '
|
65
|
-
data = {name: 'Michelangelo'}
|
64
|
+
it 'returns the resource id as part of the response' do
|
65
|
+
data = { name: 'Michelangelo' }
|
66
66
|
expected_response = RestfulResource::Response.new(body: @error.to_json)
|
67
|
-
expect_put_with_unprocessable_entity(
|
67
|
+
expect_put_with_unprocessable_entity('http://api.carwow.co.uk/dealers/1', expected_response, data: data)
|
68
68
|
|
69
69
|
@object = Dealer.put(1, data: data)
|
70
|
-
expect(@object
|
70
|
+
expect(@object).not_to be_valid
|
71
71
|
expect(@object.id).to be(1)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
|
-
context
|
76
|
-
before
|
77
|
-
data = {name: 'Barak'}
|
78
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Barak'}.to_json)
|
79
|
-
expect_post(
|
75
|
+
context '#post without errors' do
|
76
|
+
before do
|
77
|
+
data = { name: 'Barak' }
|
78
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Barak' }.to_json)
|
79
|
+
expect_post('http://api.carwow.co.uk/dealers', expected_response, data: data)
|
80
80
|
|
81
81
|
@object = Dealer.post(data: data)
|
82
82
|
end
|
83
83
|
|
84
|
-
it '
|
84
|
+
it 'returns object' do
|
85
85
|
expect(@object.name).to eq 'Barak'
|
86
86
|
end
|
87
87
|
|
88
|
-
it '
|
89
|
-
expect(@object
|
88
|
+
it 'returns valid object' do
|
89
|
+
expect(@object).to be_valid
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
|
-
context
|
94
|
-
before
|
95
|
-
data = {name: 'Leonardo'}
|
93
|
+
context '#post with errors' do
|
94
|
+
before do
|
95
|
+
data = { name: 'Leonardo' }
|
96
96
|
@error = 'Cannot use Ninja Turtles names'
|
97
|
-
expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
|
98
|
-
expect_post_with_unprocessable_entity(
|
97
|
+
expected_response = RestfulResource::Response.new(body: { errors: [@error] }.to_json)
|
98
|
+
expect_post_with_unprocessable_entity('http://api.carwow.co.uk/dealers', expected_response, data: data)
|
99
99
|
|
100
100
|
@object = Dealer.post(data: data)
|
101
101
|
end
|
102
102
|
|
103
|
-
it
|
103
|
+
it 'has an error' do
|
104
104
|
expect(@object.errors.count).to eq 1
|
105
105
|
end
|
106
106
|
|
107
|
-
it '
|
107
|
+
it 'has correct error' do
|
108
108
|
expect(@object.errors.first).to eq @error
|
109
109
|
end
|
110
110
|
|
111
|
-
it '
|
111
|
+
it 'returns properly built object' do
|
112
112
|
expect(@object.name).to eq 'Leonardo'
|
113
113
|
end
|
114
114
|
|
115
|
-
it '
|
116
|
-
expect(@object
|
115
|
+
it 'returns not valid object' do
|
116
|
+
expect(@object).not_to be_valid
|
117
117
|
end
|
118
118
|
|
119
|
-
it '
|
120
|
-
data = {name: 'Michelangelo'}
|
119
|
+
it 'handles errors returned as root object' do
|
120
|
+
data = { name: 'Michelangelo' }
|
121
121
|
expected_response = RestfulResource::Response.new(body: @error.to_json)
|
122
|
-
expect_post_with_unprocessable_entity(
|
122
|
+
expect_post_with_unprocessable_entity('http://api.carwow.co.uk/dealers', expected_response, data: data)
|
123
123
|
|
124
124
|
@object = Dealer.post(data: data)
|
125
|
-
expect(@object
|
125
|
+
expect(@object).not_to be_valid
|
126
126
|
expect(@object.errors).to eq @error
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
|
-
context
|
131
|
-
before
|
132
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Barak'}.to_json)
|
133
|
-
expect_get(
|
130
|
+
context '#get without errors' do
|
131
|
+
before do
|
132
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Barak' }.to_json)
|
133
|
+
expect_get('http://api.carwow.co.uk/dealers', expected_response)
|
134
134
|
|
135
135
|
@object = Dealer.get
|
136
136
|
end
|
137
137
|
|
138
|
-
it '
|
138
|
+
it 'returns object' do
|
139
139
|
expect(@object.name).to eq 'Barak'
|
140
140
|
end
|
141
141
|
|
142
|
-
it '
|
143
|
-
expect(@object
|
142
|
+
it 'returns valid object' do
|
143
|
+
expect(@object).to be_valid
|
144
144
|
end
|
145
145
|
end
|
146
146
|
|
147
|
-
context
|
148
|
-
before
|
147
|
+
context '#get with errors' do
|
148
|
+
before do
|
149
149
|
@error = 'Missing parameter'
|
150
|
-
expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
|
151
|
-
expect_get_with_unprocessable_entity(
|
150
|
+
expected_response = RestfulResource::Response.new(body: { errors: [@error] }.to_json)
|
151
|
+
expect_get_with_unprocessable_entity('http://api.carwow.co.uk/dealers', expected_response)
|
152
152
|
|
153
153
|
@object = Dealer.get
|
154
154
|
end
|
155
155
|
|
156
|
-
it
|
156
|
+
it 'has an error' do
|
157
157
|
expect(@object.errors.count).to eq 1
|
158
158
|
end
|
159
159
|
|
160
|
-
it '
|
160
|
+
it 'has correct error' do
|
161
161
|
expect(@object.errors.first).to eq @error
|
162
162
|
end
|
163
163
|
|
164
|
-
it '
|
165
|
-
expect(@object
|
164
|
+
it 'returns not valid object' do
|
165
|
+
expect(@object).not_to be_valid
|
166
166
|
end
|
167
167
|
|
168
|
-
it '
|
168
|
+
it 'handles errors returned as root object' do
|
169
169
|
expected_response = RestfulResource::Response.new(body: @error.to_json)
|
170
|
-
expect_get_with_unprocessable_entity(
|
170
|
+
expect_get_with_unprocessable_entity('http://api.carwow.co.uk/dealers', expected_response)
|
171
171
|
|
172
172
|
@object = Dealer.get
|
173
|
-
expect(@object
|
173
|
+
expect(@object).not_to be_valid
|
174
174
|
expect(@object.errors).to eq @error
|
175
175
|
end
|
176
176
|
end
|
@@ -2,20 +2,20 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
RSpec.describe RestfulResource::Redirections do
|
4
4
|
before do
|
5
|
-
@mock_http = double(
|
5
|
+
@mock_http = double('mock_http')
|
6
6
|
allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
|
7
7
|
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
|
8
8
|
end
|
9
9
|
|
10
|
-
describe
|
11
|
-
let(:data) { {data: 123} }
|
12
|
-
|
10
|
+
describe '#post' do
|
13
11
|
subject { ModelWithRedirections.post(data: data) }
|
14
12
|
|
13
|
+
let(:data) { { data: 123 } }
|
14
|
+
|
15
15
|
context 'with a 200 response' do
|
16
|
-
it '
|
17
|
-
expected_response = RestfulResource::Response.new(body: {test_data: 42}.to_json)
|
18
|
-
expect_post(
|
16
|
+
it 'behaves as usual' do
|
17
|
+
expected_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json)
|
18
|
+
expect_post('http://api.carwow.co.uk/model_with_redirections', expected_response, data: data)
|
19
19
|
|
20
20
|
expect(subject.test_data).to eq 42
|
21
21
|
end
|
@@ -25,48 +25,48 @@ RSpec.describe RestfulResource::Redirections do
|
|
25
25
|
let(:redirect_target) { 'http://api.carwow.co.uk/model_with_redirections/123' }
|
26
26
|
|
27
27
|
before do
|
28
|
-
allow(
|
29
|
-
expected_redirect_response = RestfulResource::Response.new(body: 'You are being redirected', status: 303, headers: { location: redirect_target})
|
30
|
-
expect_post(
|
28
|
+
allow(described_class).to receive(:wait)
|
29
|
+
expected_redirect_response = RestfulResource::Response.new(body: 'You are being redirected', status: 303, headers: { location: redirect_target })
|
30
|
+
expect_post('http://api.carwow.co.uk/model_with_redirections', expected_redirect_response, data: data)
|
31
31
|
end
|
32
32
|
|
33
|
-
it '
|
34
|
-
expected_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
|
33
|
+
it 'gets the resource from the new location' do
|
34
|
+
expected_get_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json, status: 200)
|
35
35
|
expect_get(redirect_target, expected_get_response)
|
36
36
|
|
37
37
|
expect(subject.test_data).to eq 42
|
38
38
|
end
|
39
39
|
|
40
|
-
it '
|
41
|
-
expected_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
|
40
|
+
it 'waits 1.0 seconds after first redirect' do
|
41
|
+
expected_get_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json, status: 200)
|
42
42
|
|
43
|
-
expect(
|
43
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
44
44
|
expect_get(redirect_target, expected_get_response).ordered
|
45
45
|
|
46
46
|
expect(subject.test_data).to eq 42
|
47
47
|
end
|
48
48
|
|
49
|
-
it '
|
49
|
+
it 'waits 1.0 seconds between retries' do
|
50
50
|
resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
|
51
|
-
resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
|
51
|
+
resource_ready_get_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json, status: 200)
|
52
52
|
|
53
|
-
expect(
|
53
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
54
54
|
expect_get(redirect_target, resource_not_ready_get_response).ordered
|
55
|
-
expect(
|
55
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
56
56
|
expect_get(redirect_target, resource_ready_get_response).ordered
|
57
57
|
|
58
58
|
expect(subject.test_data).to eq 42
|
59
59
|
end
|
60
60
|
|
61
|
-
it '
|
61
|
+
it 'retries 10 times by default' do
|
62
62
|
resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
|
63
|
-
resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
|
63
|
+
resource_ready_get_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json, status: 200)
|
64
64
|
|
65
65
|
9.times do
|
66
|
-
expect(
|
66
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
67
67
|
expect_get(redirect_target, resource_not_ready_get_response).ordered
|
68
68
|
end
|
69
|
-
expect(
|
69
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
70
70
|
expect_get(redirect_target, resource_ready_get_response).ordered
|
71
71
|
|
72
72
|
expect(subject.test_data).to eq 42
|
@@ -74,14 +74,14 @@ RSpec.describe RestfulResource::Redirections do
|
|
74
74
|
|
75
75
|
it 'raise after max_retries value is reached' do
|
76
76
|
resource_not_ready_get_response = RestfulResource::Response.new(body: 'pending', status: 202)
|
77
|
-
resource_ready_get_response = RestfulResource::Response.new(body: {test_data: 42}.to_json, status: 200)
|
77
|
+
resource_ready_get_response = RestfulResource::Response.new(body: { test_data: 42 }.to_json, status: 200)
|
78
78
|
|
79
79
|
11.times do
|
80
|
-
expect(
|
80
|
+
expect(described_class).to receive(:wait).with(1.0).ordered
|
81
81
|
expect_get(redirect_target, resource_not_ready_get_response).ordered
|
82
82
|
end
|
83
83
|
|
84
|
-
expect{subject}.to raise_error(RestfulResource::MaximumAttemptsReached)
|
84
|
+
expect { subject }.to raise_error(RestfulResource::MaximumAttemptsReached)
|
85
85
|
end
|
86
86
|
end
|
87
87
|
end
|