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,320 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe '#save' 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 :featured, Boolean
|
15
|
+
attribute :created_at, Time
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:response_body) do
|
19
|
+
{
|
20
|
+
data: {
|
21
|
+
id: 12,
|
22
|
+
title: 'Lorem Ipsum',
|
23
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
24
|
+
featured: false,
|
25
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:expected_default_headers) do
|
31
|
+
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'User-Agent' => "RemoteResource #{RemoteResource::VERSION}" }
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'when resource is persisted' do
|
35
|
+
let(:resource) { Post.new(id: 12, title: 'Lorem Ipsum', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', featured: false, created_at: Time.new(2015, 10, 4, 9, 30, 0)) }
|
36
|
+
|
37
|
+
let(:expected_request_body) do
|
38
|
+
{
|
39
|
+
data: {
|
40
|
+
title: 'Lorem Ipsum',
|
41
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
42
|
+
featured: false,
|
43
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
44
|
+
}
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'default behaviour' do
|
49
|
+
let!(:expected_request) do
|
50
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
51
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
52
|
+
mock_request.to_return(status: 200, body: JSON.generate(response_body))
|
53
|
+
mock_request
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'performs the correct HTTP PATCH request' do
|
57
|
+
resource.save
|
58
|
+
expect(expected_request).to have_been_requested
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'builds the correct resource' do
|
62
|
+
resource.save
|
63
|
+
|
64
|
+
post = resource
|
65
|
+
|
66
|
+
aggregate_failures do
|
67
|
+
expect(post.persisted?).to eql true
|
68
|
+
expect(post.id).to eql 12
|
69
|
+
expect(post.title).to eql 'Lorem Ipsum'
|
70
|
+
expect(post.body).to eql 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
71
|
+
expect(post.featured).to eql false
|
72
|
+
expect(post.created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe 'with connection_options[:headers]' do
|
78
|
+
let!(:expected_request) do
|
79
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
80
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
81
|
+
mock_request.to_return(status: 200, body: JSON.generate(response_body))
|
82
|
+
mock_request
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'performs the correct HTTP PATCH request' do
|
86
|
+
resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } })
|
87
|
+
expect(expected_request).to have_been_requested
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe 'with a 422 response' do
|
92
|
+
let(:response_body) do
|
93
|
+
{
|
94
|
+
errors: {
|
95
|
+
title: ['Please use a title which is more than 5 characters'],
|
96
|
+
body: ['Please fill in a body']
|
97
|
+
}
|
98
|
+
}
|
99
|
+
end
|
100
|
+
|
101
|
+
let(:expected_request_body) do
|
102
|
+
{
|
103
|
+
data: {
|
104
|
+
title: 'Lore',
|
105
|
+
body: '',
|
106
|
+
featured: true,
|
107
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
108
|
+
}
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
let!(:expected_request) do
|
113
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
114
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
115
|
+
mock_request.to_return(status: 422, body: JSON.generate(response_body))
|
116
|
+
mock_request
|
117
|
+
end
|
118
|
+
|
119
|
+
before do
|
120
|
+
resource.title = 'Lore'
|
121
|
+
resource.body = ''
|
122
|
+
resource.featured = true
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'performs the correct HTTP PATCH request' do
|
126
|
+
resource.save
|
127
|
+
expect(expected_request).to have_been_requested
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'builds the correct resource with validation errors' do
|
131
|
+
resource.save
|
132
|
+
|
133
|
+
post = resource
|
134
|
+
|
135
|
+
aggregate_failures do
|
136
|
+
expect(post.persisted?).to eql true
|
137
|
+
expect(post.id).to eql 12
|
138
|
+
expect(post.title).to eql 'Lore'
|
139
|
+
expect(post.body).to eql ''
|
140
|
+
expect(post.featured).to eql true
|
141
|
+
expect(post.created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
142
|
+
expect(post.errors.messages[:title]).to eql ['Please use a title which is more than 5 characters']
|
143
|
+
expect(post.errors.messages[:body]).to eql ['Please fill in a body']
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe 'with a 500 response' do
|
149
|
+
let!(:expected_request) do
|
150
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
151
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
152
|
+
mock_request.to_return(status: 500)
|
153
|
+
mock_request
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'raises the server error' do
|
157
|
+
expect { resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } }) }.to raise_error RemoteResource::HTTPServerError
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'adds metadata to the raised error' do
|
161
|
+
begin
|
162
|
+
resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } })
|
163
|
+
rescue RemoteResource::HTTPServerError => error
|
164
|
+
aggregate_failures do
|
165
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=500 with http_action=patch with request_url=https://www.example.com/posts/12.json'
|
166
|
+
expect(error.request_url).to eql 'https://www.example.com/posts/12.json'
|
167
|
+
expect(error.response_code).to eql 500
|
168
|
+
expect(error.request_query).to be_nil
|
169
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe 'when resource is NOT persisted' do
|
177
|
+
let(:resource) { Post.new(title: 'Lorem Ipsum', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', featured: false) }
|
178
|
+
|
179
|
+
let(:expected_request_body) do
|
180
|
+
{
|
181
|
+
data: {
|
182
|
+
title: 'Lorem Ipsum',
|
183
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
184
|
+
featured: false,
|
185
|
+
created_at: nil,
|
186
|
+
}
|
187
|
+
}
|
188
|
+
end
|
189
|
+
|
190
|
+
describe 'default behaviour' do
|
191
|
+
let!(:expected_request) do
|
192
|
+
mock_request = stub_request(:post, 'https://www.example.com/posts.json')
|
193
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
194
|
+
mock_request.to_return(status: 201, body: JSON.generate(response_body))
|
195
|
+
mock_request
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'performs the correct HTTP POST request' do
|
199
|
+
resource.save
|
200
|
+
expect(expected_request).to have_been_requested
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'builds the correct resource' do
|
204
|
+
resource.save
|
205
|
+
|
206
|
+
post = resource
|
207
|
+
|
208
|
+
aggregate_failures do
|
209
|
+
expect(post.persisted?).to eql true
|
210
|
+
expect(post.id).to eql 12
|
211
|
+
expect(post.title).to eql 'Lorem Ipsum'
|
212
|
+
expect(post.body).to eql 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
213
|
+
expect(post.featured).to eql false
|
214
|
+
expect(post.created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
describe 'with connection_options[:headers]' do
|
220
|
+
let!(:expected_request) do
|
221
|
+
mock_request = stub_request(:post, 'https://www.example.com/posts.json')
|
222
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
223
|
+
mock_request.to_return(status: 201, body: JSON.generate(response_body))
|
224
|
+
mock_request
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'performs the correct HTTP POST request' do
|
228
|
+
resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } })
|
229
|
+
expect(expected_request).to have_been_requested
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
describe 'with a 422 response' do
|
234
|
+
let(:response_body) do
|
235
|
+
{
|
236
|
+
errors: {
|
237
|
+
title: ['Please use a title which is more than 5 characters'],
|
238
|
+
body: ['Please fill in a body'],
|
239
|
+
virtual_attribute: ['You already posted today', 'Please refrain from using curse words']
|
240
|
+
}
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
let(:expected_request_body) do
|
245
|
+
{
|
246
|
+
data: {
|
247
|
+
title: 'Lore',
|
248
|
+
body: '',
|
249
|
+
featured: true,
|
250
|
+
created_at: nil
|
251
|
+
}
|
252
|
+
}
|
253
|
+
end
|
254
|
+
|
255
|
+
let!(:expected_request) do
|
256
|
+
mock_request = stub_request(:post, 'https://www.example.com/posts.json')
|
257
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
258
|
+
mock_request.to_return(status: 422, body: JSON.generate(response_body))
|
259
|
+
mock_request
|
260
|
+
end
|
261
|
+
|
262
|
+
before do
|
263
|
+
resource.title = 'Lore'
|
264
|
+
resource.body = ''
|
265
|
+
resource.featured = true
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'performs the correct HTTP POST request' do
|
269
|
+
resource.save
|
270
|
+
expect(expected_request).to have_been_requested
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'builds the correct resource with validation errors' do
|
274
|
+
resource.save
|
275
|
+
|
276
|
+
post = resource
|
277
|
+
|
278
|
+
aggregate_failures do
|
279
|
+
expect(post.persisted?).to eql false
|
280
|
+
expect(post.id).to be_nil
|
281
|
+
expect(post.title).to eql 'Lore'
|
282
|
+
expect(post.body).to eql ''
|
283
|
+
expect(post.featured).to eql true
|
284
|
+
expect(post.created_at).to be_blank
|
285
|
+
expect(post.errors.messages[:title]).to eql ['Please use a title which is more than 5 characters']
|
286
|
+
expect(post.errors.messages[:body]).to eql ['Please fill in a body']
|
287
|
+
expect(post.errors.messages[:base]).to eql ['You already posted today', 'Please refrain from using curse words']
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
describe 'with a 500 response' do
|
293
|
+
let!(:expected_request) do
|
294
|
+
mock_request = stub_request(:post, 'https://www.example.com/posts.json')
|
295
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
296
|
+
mock_request.to_return(status: 500)
|
297
|
+
mock_request
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'raises the server error' do
|
301
|
+
expect { resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } }) }.to raise_error RemoteResource::HTTPServerError
|
302
|
+
end
|
303
|
+
|
304
|
+
it 'adds metadata to the raised error' do
|
305
|
+
begin
|
306
|
+
resource.save({ headers: { 'X-Pseudonym' => 'pseudonym' } })
|
307
|
+
rescue RemoteResource::HTTPServerError => error
|
308
|
+
aggregate_failures do
|
309
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=500 with http_action=post with request_url=https://www.example.com/posts.json'
|
310
|
+
expect(error.request_url).to eql 'https://www.example.com/posts.json'
|
311
|
+
expect(error.response_code).to eql 500
|
312
|
+
expect(error.request_query).to be_nil
|
313
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
end
|
@@ -0,0 +1,221 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe '#update_attributes' 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 :featured, Boolean
|
15
|
+
attribute :created_at, Time
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:resource) { Post.new(id: 12, title: 'Lorem Ipsum', body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', featured: true, created_at: Time.new(2015, 10, 4, 9, 30, 0)) }
|
19
|
+
|
20
|
+
let(:response_body) do
|
21
|
+
{
|
22
|
+
data: {
|
23
|
+
id: 12,
|
24
|
+
title: 'Aliquam lobortis',
|
25
|
+
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
|
26
|
+
featured: false,
|
27
|
+
created_at: Time.new(2015, 10, 4, 9, 30, 0),
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:expected_default_headers) do
|
33
|
+
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'User-Agent' => "RemoteResource #{RemoteResource::VERSION}" }
|
34
|
+
end
|
35
|
+
|
36
|
+
describe 'default behaviour' do
|
37
|
+
let(:expected_request_body) do
|
38
|
+
{
|
39
|
+
data: {
|
40
|
+
title: 'Aliquam lobortis',
|
41
|
+
featured: false
|
42
|
+
}
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
let!(:expected_request) do
|
47
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
48
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
49
|
+
mock_request.to_return(status: 201, body: JSON.generate(response_body))
|
50
|
+
mock_request
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'performs the correct HTTP PATCH request' do
|
54
|
+
resource.update_attributes(title: 'Aliquam lobortis', featured: false)
|
55
|
+
expect(expected_request).to have_been_requested
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'builds the correct resource' do
|
59
|
+
resource.update_attributes(title: 'Aliquam lobortis', featured: false)
|
60
|
+
|
61
|
+
post = resource
|
62
|
+
|
63
|
+
aggregate_failures do
|
64
|
+
expect(post.persisted?).to eql true
|
65
|
+
expect(post.id).to eql 12
|
66
|
+
expect(post.title).to eql 'Aliquam lobortis'
|
67
|
+
expect(post.body).to eql 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'
|
68
|
+
expect(post.featured).to eql false
|
69
|
+
expect(post.created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe 'with connection_options[:headers]' do
|
75
|
+
let(:expected_request_body) do
|
76
|
+
{
|
77
|
+
data: {
|
78
|
+
title: 'Aliquam lobortis',
|
79
|
+
featured: false
|
80
|
+
}
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
let!(:expected_request) do
|
85
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
86
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
87
|
+
mock_request.to_return(status: 201, body: JSON.generate(response_body))
|
88
|
+
mock_request
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'performs the correct HTTP PATCH request' do
|
92
|
+
resource.update_attributes({ title: 'Aliquam lobortis', featured: false }, { 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_body) do
|
99
|
+
{
|
100
|
+
data: {
|
101
|
+
title: 'Aliquam lobortis',
|
102
|
+
featured: false
|
103
|
+
}
|
104
|
+
}
|
105
|
+
end
|
106
|
+
|
107
|
+
let!(:expected_request) do
|
108
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
109
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
110
|
+
mock_request.to_return(status: 404)
|
111
|
+
mock_request
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'raises the not found error' do
|
115
|
+
expect { resource.update_attributes({ title: 'Aliquam lobortis', featured: false }, { headers: { 'X-Pseudonym' => 'pseudonym' } }) }.to raise_error RemoteResource::HTTPNotFound
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'adds metadata to the raised error' do
|
119
|
+
begin
|
120
|
+
resource.update_attributes({ title: 'Aliquam lobortis', featured: false }, { headers: { 'X-Pseudonym' => 'pseudonym' } })
|
121
|
+
rescue RemoteResource::HTTPNotFound => error
|
122
|
+
aggregate_failures do
|
123
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=404 with http_action=patch with request_url=https://www.example.com/posts/12.json'
|
124
|
+
expect(error.request_url).to eql 'https://www.example.com/posts/12.json'
|
125
|
+
expect(error.response_code).to eql 404
|
126
|
+
expect(error.request_query).to be_nil
|
127
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe 'with a 422 response' do
|
134
|
+
let(:response_body) do
|
135
|
+
{
|
136
|
+
errors: {
|
137
|
+
title: ['Please use a title which is more than 5 characters'],
|
138
|
+
body: ['Please fill in a body'],
|
139
|
+
virtual_attribute: ['You already posted today', 'Please refrain from using curse words']
|
140
|
+
}
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
let(:expected_request_body) do
|
145
|
+
{
|
146
|
+
data: {
|
147
|
+
title: 'Lore',
|
148
|
+
body: '',
|
149
|
+
featured: false
|
150
|
+
}
|
151
|
+
}
|
152
|
+
end
|
153
|
+
|
154
|
+
let!(:expected_request) do
|
155
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
156
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers)
|
157
|
+
mock_request.to_return(status: 422, body: JSON.generate(response_body))
|
158
|
+
mock_request
|
159
|
+
end
|
160
|
+
|
161
|
+
it 'performs the correct HTTP PATCH request' do
|
162
|
+
resource.update_attributes(title: 'Lore', body: '', featured: false)
|
163
|
+
expect(expected_request).to have_been_requested
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'builds the correct resource with validation errors' do
|
167
|
+
resource.update_attributes(title: 'Lore', body: '', featured: false)
|
168
|
+
|
169
|
+
post = resource
|
170
|
+
|
171
|
+
aggregate_failures do
|
172
|
+
expect(post.persisted?).to eql true
|
173
|
+
expect(post.id).to eql 12
|
174
|
+
expect(post.title).to eql 'Lore'
|
175
|
+
expect(post.body).to eql ''
|
176
|
+
expect(post.featured).to eql false
|
177
|
+
expect(post.created_at).to eql Time.new(2015, 10, 4, 9, 30, 0)
|
178
|
+
expect(post.errors.messages[:title]).to eql ['Please use a title which is more than 5 characters']
|
179
|
+
expect(post.errors.messages[:body]).to eql ['Please fill in a body']
|
180
|
+
expect(post.errors.messages[:base]).to eql ['You already posted today', 'Please refrain from using curse words']
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe 'with a 500 response' do
|
186
|
+
let(:expected_request_body) do
|
187
|
+
{
|
188
|
+
data: {
|
189
|
+
title: 'Aliquam lobortis',
|
190
|
+
featured: false
|
191
|
+
}
|
192
|
+
}
|
193
|
+
end
|
194
|
+
|
195
|
+
let!(:expected_request) do
|
196
|
+
mock_request = stub_request(:patch, 'https://www.example.com/posts/12.json')
|
197
|
+
mock_request.with(query: nil, body: JSON.generate(expected_request_body), headers: expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
198
|
+
mock_request.to_return(status: 500)
|
199
|
+
mock_request
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'raises the server error' do
|
203
|
+
expect { resource.update_attributes({ title: 'Aliquam lobortis', featured: false }, { headers: { 'X-Pseudonym' => 'pseudonym' } }) }.to raise_error RemoteResource::HTTPServerError
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'adds metadata to the raised error' do
|
207
|
+
begin
|
208
|
+
resource.update_attributes({ title: 'Aliquam lobortis', featured: false }, { headers: { 'X-Pseudonym' => 'pseudonym' } })
|
209
|
+
rescue RemoteResource::HTTPServerError => error
|
210
|
+
aggregate_failures do
|
211
|
+
expect(error.message).to eql 'HTTP request failed for Post with response_code=500 with http_action=patch with request_url=https://www.example.com/posts/12.json'
|
212
|
+
expect(error.request_url).to eql 'https://www.example.com/posts/12.json'
|
213
|
+
expect(error.response_code).to eql 500
|
214
|
+
expect(error.request_query).to be_nil
|
215
|
+
expect(error.request_headers).to eql(expected_default_headers.merge({ 'X-Pseudonym' => 'pseudonym' }))
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
end
|