hyperclient 2.0.0 → 2.2.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/.github/workflows/coverage.yml +14 -10
- data/.github/workflows/danger-comment.yml +18 -0
- data/.github/workflows/danger.yml +8 -17
- data/.github/workflows/lint.yml +10 -3
- data/.github/workflows/release.yml +26 -0
- data/.github/workflows/test.yml +21 -5
- data/.gitignore +3 -0
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +8 -5
- data/AGENTS.md +26 -0
- data/CHANGELOG.md +25 -4
- data/CONTRIBUTING.md +10 -0
- data/Dangerfile +2 -0
- data/Gemfile +14 -3
- data/README.md +110 -41
- data/RELEASING.md +32 -43
- data/Rakefile +18 -0
- data/examples/oxford/README.md +42 -0
- data/examples/oxford/app.rb +48 -0
- data/examples/oxford/client.rb +14 -0
- data/examples/oxford/places.json +21 -0
- data/examples/oxford/server.rb +13 -0
- data/features/support/env.rb +2 -1
- data/gemfiles/release.gemfile +6 -0
- data/lib/hyperclient/entry_point.rb +14 -0
- data/lib/hyperclient/link.rb +133 -8
- data/lib/hyperclient/resource.rb +27 -0
- data/lib/hyperclient/version.rb +1 -1
- data/test/hyperclient/attributes_test.rb +9 -0
- data/test/hyperclient/collection_test.rb +22 -0
- data/test/hyperclient/curie_test.rb +7 -0
- data/test/hyperclient/entry_point_test.rb +36 -0
- data/test/hyperclient/link_collection_test.rb +10 -0
- data/test/hyperclient/link_test.rb +434 -1
- data/test/hyperclient/resource_test.rb +65 -1
- data/test/hyperclient_test.rb +1 -0
- data/test/oxford_example_test.rb +63 -0
- data/test/test_helper.rb +1 -2
- metadata +13 -8
- data/.DS_Store +0 -0
- data/examples/splines_api.rb +0 -39
|
@@ -11,11 +11,13 @@ module Hyperclient
|
|
|
11
11
|
describe prop do
|
|
12
12
|
it 'returns the property value' do
|
|
13
13
|
link = Link.new('key', { prop => 'value' }, entry_point)
|
|
14
|
+
|
|
14
15
|
_(link.send("_#{prop}")).must_equal 'value'
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
it 'returns nil if the property is not present' do
|
|
18
19
|
link = Link.new('key', {}, entry_point)
|
|
20
|
+
|
|
19
21
|
_(link.send("_#{prop}")).must_be_nil
|
|
20
22
|
end
|
|
21
23
|
end
|
|
@@ -53,11 +55,13 @@ module Hyperclient
|
|
|
53
55
|
describe 'required argument' do
|
|
54
56
|
it 'builds a Link with the templated URI representation' do
|
|
55
57
|
link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point)
|
|
58
|
+
|
|
56
59
|
_(link._expand(id: '1')._url).must_equal '/orders/1'
|
|
57
60
|
end
|
|
58
61
|
|
|
59
62
|
it 'expands an uri template without variables' do
|
|
60
63
|
link = Link.new('key', { 'href' => '/orders/{id}', 'templated' => true }, entry_point)
|
|
64
|
+
|
|
61
65
|
_(link._expand._url).must_equal '/orders/'
|
|
62
66
|
_(link._url).must_equal '/orders/'
|
|
63
67
|
end
|
|
@@ -66,32 +70,99 @@ module Hyperclient
|
|
|
66
70
|
describe 'query string argument' do
|
|
67
71
|
it 'builds a Link with the templated URI representation' do
|
|
68
72
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
73
|
+
|
|
69
74
|
_(link._expand(id: '1')._url).must_equal '/orders?id=1'
|
|
70
75
|
end
|
|
71
76
|
|
|
72
77
|
it 'expands an uri template without variables' do
|
|
73
78
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
79
|
+
|
|
74
80
|
_(link._expand._url).must_equal '/orders'
|
|
75
81
|
_(link._url).must_equal '/orders'
|
|
76
82
|
end
|
|
77
83
|
|
|
78
84
|
it 'does not expand unknown variables' do
|
|
79
85
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
86
|
+
|
|
80
87
|
_(link._expand(unknown: '1')._url).must_equal '/orders'
|
|
81
88
|
end
|
|
82
89
|
|
|
83
90
|
it 'only expands known variables' do
|
|
84
91
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
92
|
+
|
|
85
93
|
_(link._expand(unknown: '1', id: '2')._url).must_equal '/orders?id=2'
|
|
86
94
|
end
|
|
87
95
|
|
|
88
96
|
it 'only expands templated links' do
|
|
89
97
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => false }, entry_point)
|
|
98
|
+
|
|
90
99
|
_(link._expand(id: '1')._url).must_equal '/orders{?id}'
|
|
91
100
|
end
|
|
92
101
|
end
|
|
93
102
|
end
|
|
94
103
|
|
|
104
|
+
describe '_query' do
|
|
105
|
+
it 'adds query parameters to a link that is not templated' do
|
|
106
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
107
|
+
|
|
108
|
+
_(link._query(sort: 'name')._url).must_equal '/orders?sort=name'
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it 'adds query parameters a templated link does not declare' do
|
|
112
|
+
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
113
|
+
|
|
114
|
+
_(link._expand(id: '1')._query(sort: 'name')._url).must_equal '/orders?id=1&sort=name'
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
it 'keeps query parameters when expanded afterwards' do
|
|
118
|
+
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
119
|
+
|
|
120
|
+
_(link._query(sort: 'name')._expand(id: '1')._url).must_equal '/orders?id=1&sort=name'
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it 'keeps the existing query string and fragment' do
|
|
124
|
+
link = Link.new('key', { 'href' => '/orders?status=open&status=paid#top' }, entry_point)
|
|
125
|
+
|
|
126
|
+
_(link._query(sort: 'name')._url).must_equal '/orders?status=open&status=paid&sort=name#top'
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it 'combines chained query parameters' do
|
|
130
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
131
|
+
|
|
132
|
+
_(link._query(sort: 'name')._query(page: 2, sort: 'date')._url).must_equal '/orders?sort=date&page=2'
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'encodes values and repeats array parameters' do
|
|
136
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
137
|
+
|
|
138
|
+
_(link._query(q: 'a&b c', ids: [1, 2])._url).must_equal '/orders?q=a%26b+c&ids=1&ids=2'
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it 'drops nil and empty query parameters' do
|
|
142
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
143
|
+
|
|
144
|
+
_(link._query(sort: nil)._url).must_equal '/orders'
|
|
145
|
+
_(link._query({})._url).must_equal '/orders'
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'does not change the original link' do
|
|
149
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
150
|
+
link._query(sort: 'name')
|
|
151
|
+
|
|
152
|
+
_(link._url).must_equal '/orders'
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it 'sends the query parameters with the request' do
|
|
156
|
+
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
157
|
+
|
|
158
|
+
stub_request(entry_point.connection) do |stub|
|
|
159
|
+
stub.get('http://api.example.org/orders?sort=name') { [200, {}, { 'sorted' => true }] }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
_(link._query(sort: 'name')._get.sorted).must_equal true
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
95
166
|
describe '_url' do
|
|
96
167
|
it 'expands an uri template without variables' do
|
|
97
168
|
link = Link.new('key', { 'href' => '/orders{?id}', 'templated' => true }, entry_point)
|
|
@@ -119,6 +190,7 @@ module Hyperclient
|
|
|
119
190
|
|
|
120
191
|
it 'returns the link when no uri template' do
|
|
121
192
|
link = Link.new('key', { 'href' => '/orders' }, entry_point)
|
|
193
|
+
|
|
122
194
|
_(link._url).must_equal '/orders'
|
|
123
195
|
end
|
|
124
196
|
|
|
@@ -141,9 +213,53 @@ module Hyperclient
|
|
|
141
213
|
|
|
142
214
|
link._resource
|
|
143
215
|
end
|
|
216
|
+
|
|
217
|
+
it 'fetches a fresh resource after a mutating request instead of returning the cached response (#107)' do
|
|
218
|
+
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
|
219
|
+
|
|
220
|
+
stub_request(entry_point.connection) do |stub|
|
|
221
|
+
stub.post('http://api.example.org/productions/1') { [201, {}, { 'status' => 'created' }] }
|
|
222
|
+
stub.get('http://api.example.org/productions/1') { [200, {}, { 'status' => 'fetched' }] }
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
link._post('foo' => 'bar')
|
|
226
|
+
|
|
227
|
+
_(link._resource.status).must_equal 'fetched'
|
|
228
|
+
end
|
|
144
229
|
end
|
|
145
230
|
|
|
146
231
|
describe 'get' do
|
|
232
|
+
%w[application/hal+json application/json].each do |content_type|
|
|
233
|
+
it "parses a #{content_type} response into a resource" do
|
|
234
|
+
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
|
235
|
+
|
|
236
|
+
stub_request(entry_point.connection) do |stub|
|
|
237
|
+
stub.get('http://api.example.org/productions/1') do
|
|
238
|
+
[200, { 'Content-Type' => content_type },
|
|
239
|
+
'{"title":"Production","_links":{"self":{"href":"/productions/1"}}}']
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
resource = link._get
|
|
244
|
+
|
|
245
|
+
_(resource.title).must_equal 'Production'
|
|
246
|
+
_(resource._links.self._url).must_equal '/productions/1'
|
|
247
|
+
_(resource._response.body).must_be_kind_of Hash
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
it 'raises a parsing error for an invalid JSON response' do
|
|
252
|
+
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
|
253
|
+
|
|
254
|
+
stub_request(entry_point.connection) do |stub|
|
|
255
|
+
stub.get('http://api.example.org/productions/1') do
|
|
256
|
+
[200, { 'Content-Type' => 'application/hal+json' }, '{"title":']
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
_(-> { link._get }).must_raise Faraday::ParsingError
|
|
261
|
+
end
|
|
262
|
+
|
|
147
263
|
it 'sends a GET request with the link url' do
|
|
148
264
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
|
149
265
|
|
|
@@ -261,12 +377,215 @@ module Hyperclient
|
|
|
261
377
|
end
|
|
262
378
|
end
|
|
263
379
|
|
|
380
|
+
describe 'Allow header' do
|
|
381
|
+
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }
|
|
382
|
+
let(:url) { 'http://api.example.org/productions/1' }
|
|
383
|
+
|
|
384
|
+
it 'does not know the allowed methods before a request' do
|
|
385
|
+
_(link._allow).must_be_nil
|
|
386
|
+
_(link._allow?(:delete)).must_equal true
|
|
387
|
+
end
|
|
388
|
+
|
|
389
|
+
it 'sends any method when the response has no Allow header' do
|
|
390
|
+
stub_request(entry_point.connection) do |stub|
|
|
391
|
+
stub.get(url) { [200, {}, nil] }
|
|
392
|
+
stub.delete(url) { [204, {}, nil] }
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
link._get
|
|
396
|
+
|
|
397
|
+
_(link._allow).must_be_nil
|
|
398
|
+
_(link._delete).must_be_kind_of Resource
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
it 'parses methods case-insensitively, ignoring whitespace and empty entries' do
|
|
402
|
+
stub_request(entry_point.connection) do |stub|
|
|
403
|
+
stub.get(url) { [200, { 'allow' => ' get,head ,, Post,get ' }, nil] }
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
link._get
|
|
407
|
+
|
|
408
|
+
_(link._allow).must_equal %w[GET HEAD POST]
|
|
409
|
+
_(link._allow?(:post)).must_equal true
|
|
410
|
+
_(link._allow?('Head')).must_equal true
|
|
411
|
+
_(link._allow?(:delete)).must_equal false
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
it 'sends a method that the Allow header does not list by default' do
|
|
415
|
+
stub_request(entry_point.connection) do |stub|
|
|
416
|
+
stub.get(url) { [200, { 'Allow' => 'GET, HEAD' }, nil] }
|
|
417
|
+
stub.delete(url) { [204, {}, nil] }
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
link._get
|
|
421
|
+
|
|
422
|
+
_(entry_point.enforce_allowed_methods?).must_equal false
|
|
423
|
+
_(link._allow?(:delete)).must_equal false
|
|
424
|
+
_(link._delete).must_be_kind_of Resource
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
it 'repeats a request that returned 405 by default' do
|
|
428
|
+
requests = 0
|
|
429
|
+
stub_request(entry_point.connection) do |stub|
|
|
430
|
+
stub.delete(url) do
|
|
431
|
+
requests += 1
|
|
432
|
+
[405, { 'Allow' => 'GET' }, nil]
|
|
433
|
+
end
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
_(-> { link._delete }).must_raise Faraday::ClientError
|
|
437
|
+
_(link._allow).must_equal %w[GET]
|
|
438
|
+
_(-> { link._delete }).must_raise Faraday::ClientError
|
|
439
|
+
_(requests).must_equal 2
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
it 'returns a copy of the allowed methods' do
|
|
443
|
+
stub_request(entry_point.connection) do |stub|
|
|
444
|
+
stub.get(url) { [200, { 'Allow' => 'GET' }, nil] }
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
link._get
|
|
448
|
+
link._allow << 'DELETE'
|
|
449
|
+
|
|
450
|
+
_(link._allow?(:delete)).must_equal false
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
describe 'with enforce_allowed_methods' do
|
|
454
|
+
before do
|
|
455
|
+
entry_point.enforce_allowed_methods = true
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
it 'sends an allowed method' do
|
|
459
|
+
stub_request(entry_point.connection) do |stub|
|
|
460
|
+
stub.get(url) { [200, { 'Allow' => 'GET, POST' }, nil] }
|
|
461
|
+
stub.post(url) { [201, {}, nil] }
|
|
462
|
+
end
|
|
463
|
+
|
|
464
|
+
link._get
|
|
465
|
+
|
|
466
|
+
_(link._post('foo' => 'bar')).must_be_kind_of Resource
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
it 'raises MethodNotAllowedError without sending a disallowed method' do
|
|
470
|
+
stub_request(entry_point.connection) do |stub|
|
|
471
|
+
stub.get(url) { [200, { 'Allow' => 'GET, HEAD' }, nil] }
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
link._get
|
|
475
|
+
error = _(-> { link._delete }).must_raise MethodNotAllowedError
|
|
476
|
+
|
|
477
|
+
_(error.http_method).must_equal 'DELETE'
|
|
478
|
+
_(error.url).must_equal '/productions/1'
|
|
479
|
+
_(error.allowed).must_equal %w[GET HEAD]
|
|
480
|
+
_(error.message).must_equal 'DELETE is not allowed on /productions/1, the server allows only GET, HEAD.'
|
|
481
|
+
end
|
|
482
|
+
|
|
483
|
+
it 'raises for every method when the Allow header is empty, except OPTIONS' do
|
|
484
|
+
stub_request(entry_point.connection) do |stub|
|
|
485
|
+
stub.head(url) { [200, { 'Allow' => '' }, nil] }
|
|
486
|
+
stub.options(url) { [200, { 'Allow' => 'GET' }, nil] }
|
|
487
|
+
stub.get(url) { [200, {}, nil] }
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
link._head
|
|
491
|
+
|
|
492
|
+
_(link._allow).must_equal []
|
|
493
|
+
error = _(-> { link._get }).must_raise MethodNotAllowedError
|
|
494
|
+
_(error.message).must_equal 'GET is not allowed on /productions/1, the server allows no methods.'
|
|
495
|
+
_(-> { link._head }).must_raise MethodNotAllowedError
|
|
496
|
+
|
|
497
|
+
link._options
|
|
498
|
+
|
|
499
|
+
_(link._allow).must_equal %w[GET]
|
|
500
|
+
_(link._get).must_be_kind_of Resource
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
it 'does not imply HEAD from GET' do
|
|
504
|
+
stub_request(entry_point.connection) do |stub|
|
|
505
|
+
stub.get(url) { [200, { 'Allow' => 'GET' }, nil] }
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
link._get
|
|
509
|
+
|
|
510
|
+
_(-> { link._head }).must_raise MethodNotAllowedError
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
it 'records the Allow header of a 405 response and refuses to repeat the request' do
|
|
514
|
+
requests = 0
|
|
515
|
+
stub_request(entry_point.connection) do |stub|
|
|
516
|
+
stub.delete(url) do
|
|
517
|
+
requests += 1
|
|
518
|
+
[405, { 'Allow' => 'GET, PUT' }, nil]
|
|
519
|
+
end
|
|
520
|
+
stub.put(url) { [200, {}, nil] }
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
_(-> { link._delete }).must_raise Faraday::ClientError
|
|
524
|
+
_(link._allow).must_equal %w[GET PUT]
|
|
525
|
+
_(-> { link._delete }).must_raise MethodNotAllowedError
|
|
526
|
+
_(requests).must_equal 1
|
|
527
|
+
_(link._put('foo' => 'bar')).must_be_kind_of Resource
|
|
528
|
+
end
|
|
529
|
+
|
|
530
|
+
it 'records the Allow header of a 405 response when errors are not raised' do
|
|
531
|
+
entry_point.connection(default: false) do |conn|
|
|
532
|
+
conn.adapter :test do |stub|
|
|
533
|
+
stub.post(url) { [405, { 'Allow' => 'GET' }, nil] }
|
|
534
|
+
end
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
_(link._post._status).must_equal 405
|
|
538
|
+
_(-> { link._post }).must_raise MethodNotAllowedError
|
|
539
|
+
end
|
|
540
|
+
|
|
541
|
+
it 'uses the most recent response' do
|
|
542
|
+
stub_request(entry_point.connection) do |stub|
|
|
543
|
+
stub.get(url) { [200, { 'Allow' => 'GET, PATCH' }, nil] }
|
|
544
|
+
stub.patch(url) { [200, {}, nil] }
|
|
545
|
+
stub.delete(url) { [204, {}, nil] }
|
|
546
|
+
end
|
|
547
|
+
|
|
548
|
+
link._get
|
|
549
|
+
|
|
550
|
+
_(-> { link._delete }).must_raise MethodNotAllowedError
|
|
551
|
+
link._patch('foo' => 'bar')
|
|
552
|
+
|
|
553
|
+
_(link._allow).must_be_nil
|
|
554
|
+
_(link._delete).must_be_kind_of Resource
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
it 'keeps the allowed methods per link' do
|
|
558
|
+
stub_request(entry_point.connection) do |stub|
|
|
559
|
+
stub.get(url) { [200, { 'Allow' => 'GET' }, nil] }
|
|
560
|
+
stub.delete(url) { [204, {}, nil] }
|
|
561
|
+
end
|
|
562
|
+
|
|
563
|
+
link._get
|
|
564
|
+
|
|
565
|
+
_(Link.new('key', { 'href' => '/productions/1' }, entry_point)._delete).must_be_kind_of Resource
|
|
566
|
+
end
|
|
567
|
+
|
|
568
|
+
it 'starts an expanded link without restrictions' do
|
|
569
|
+
stub_request(entry_point.connection) do |stub|
|
|
570
|
+
stub.get(url) { [200, { 'Allow' => 'GET' }, nil] }
|
|
571
|
+
stub.delete(url) { [204, {}, nil] }
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
link._get
|
|
575
|
+
|
|
576
|
+
_(-> { link._delete }).must_raise MethodNotAllowedError
|
|
577
|
+
_(link._expand._allow).must_be_nil
|
|
578
|
+
_(link._expand._delete).must_be_kind_of Resource
|
|
579
|
+
end
|
|
580
|
+
end
|
|
581
|
+
end
|
|
582
|
+
|
|
264
583
|
describe 'inspect' do
|
|
265
584
|
it 'outputs a custom-friendly output' do
|
|
266
585
|
link = Link.new('key', { 'href' => '/productions/1' }, 'foo')
|
|
267
586
|
|
|
268
587
|
_(link.inspect).must_include 'Link'
|
|
269
|
-
_(link.inspect).must_include '
|
|
588
|
+
_(link.inspect).must_include({ 'href' => '/productions/1' }.inspect)
|
|
270
589
|
end
|
|
271
590
|
end
|
|
272
591
|
|
|
@@ -313,6 +632,78 @@ module Hyperclient
|
|
|
313
632
|
|
|
314
633
|
_(resource.next._links.next._url).must_equal 'http://api.example.org/page3'
|
|
315
634
|
end
|
|
635
|
+
|
|
636
|
+
it 'iterates over paginated, embedded items across pages' do
|
|
637
|
+
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
|
|
638
|
+
|
|
639
|
+
stub_request(entry_point.connection) do |stub|
|
|
640
|
+
stub.get('http://api.example.org/orders') do
|
|
641
|
+
[200, {}, { '_links' => { 'next' => { 'href' => 'http://api.example.org/orders?page=2' } },
|
|
642
|
+
'_embedded' => { 'orders' => [{ 'id' => 1 }] } }]
|
|
643
|
+
end
|
|
644
|
+
stub.get('http://api.example.org/orders?page=2') do
|
|
645
|
+
[200, {}, { '_embedded' => { 'orders' => [{ 'id' => 2 }] } }]
|
|
646
|
+
end
|
|
647
|
+
end
|
|
648
|
+
|
|
649
|
+
ids = resource._links.orders.map(&:id)
|
|
650
|
+
|
|
651
|
+
_(ids).must_equal [1, 2]
|
|
652
|
+
end
|
|
653
|
+
|
|
654
|
+
it 'raises a clear error when the resource links back to itself under the same name' do
|
|
655
|
+
resource = Resource.new({ '_links' => { 'sub_nodes' => { 'href' => '/sub_nodes' } } }, entry_point)
|
|
656
|
+
|
|
657
|
+
stub_request(entry_point.connection) do |stub|
|
|
658
|
+
stub.get('http://api.example.org/sub_nodes') do
|
|
659
|
+
[200, {}, { '_links' => { 'sub_nodes' => { 'href' => 'http://api.example.org/sub_nodes' } },
|
|
660
|
+
'_embedded' => { 'nodes' => [{ 'name' => 'a' }] } }]
|
|
661
|
+
end
|
|
662
|
+
end
|
|
663
|
+
|
|
664
|
+
error = _(-> { resource._links.sub_nodes.map(&:name) }).must_raise Hyperclient::SelfReferencingLinkError
|
|
665
|
+
_(error.message).must_equal 'The sub_nodes link at http://api.example.org/sub_nodes links back to itself, ' \
|
|
666
|
+
'use _embedded to iterate over embedded items.'
|
|
667
|
+
_(resource._links.sub_nodes._embedded.nodes.map(&:name)).must_equal ['a']
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
it 'does not follow a link back to itself when checking respond_to? or delegating' do
|
|
671
|
+
resource = Resource.new({ '_links' => { 'sub_nodes' => { 'href' => '/sub_nodes' } } }, entry_point)
|
|
672
|
+
|
|
673
|
+
stub_request(entry_point.connection) do |stub|
|
|
674
|
+
stub.get('http://api.example.org/sub_nodes') do
|
|
675
|
+
[200, {}, { 'name' => nil,
|
|
676
|
+
'_links' => { 'sub_nodes' => { 'href' => 'http://api.example.org/sub_nodes' } } }]
|
|
677
|
+
end
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
_(resource._links.sub_nodes.respond_to?(:unknown)).must_equal false
|
|
681
|
+
_(resource._links.sub_nodes.name).must_be_nil
|
|
682
|
+
end
|
|
683
|
+
|
|
684
|
+
it 'iterates a link that points to a different url with the same name' do
|
|
685
|
+
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
|
|
686
|
+
|
|
687
|
+
stub_request(entry_point.connection) do |stub|
|
|
688
|
+
stub.get('http://api.example.org/orders') { [200, {}, { '_links' => { 'orders' => { 'href' => '/all_orders' } } }] }
|
|
689
|
+
stub.get('http://api.example.org/all_orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
|
|
690
|
+
end
|
|
691
|
+
|
|
692
|
+
_(resource._links.orders.map(&:id)).must_equal [1]
|
|
693
|
+
end
|
|
694
|
+
|
|
695
|
+
it 'returns an Enumerator when called without a block' do
|
|
696
|
+
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
|
|
697
|
+
|
|
698
|
+
stub_request(entry_point.connection) do |stub|
|
|
699
|
+
stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
|
|
700
|
+
end
|
|
701
|
+
|
|
702
|
+
enum = resource._links.orders.each
|
|
703
|
+
|
|
704
|
+
_(enum).must_be_kind_of Enumerator
|
|
705
|
+
_(enum.to_a.first.id).must_equal 1
|
|
706
|
+
end
|
|
316
707
|
end
|
|
317
708
|
|
|
318
709
|
describe 'resource' do
|
|
@@ -341,13 +732,55 @@ module Hyperclient
|
|
|
341
732
|
it 'responds to missing methods' do
|
|
342
733
|
resource.expects(:respond_to?).with('orders').returns(false)
|
|
343
734
|
resource.expects(:respond_to?).with('embedded').returns(true)
|
|
735
|
+
|
|
344
736
|
_(link.respond_to?(:embedded)).must_equal true
|
|
345
737
|
end
|
|
346
738
|
|
|
739
|
+
it 'responds to missing methods that delegate to another resource' do
|
|
740
|
+
delegate = mock('Delegate')
|
|
741
|
+
resource.expects(:respond_to?).with('orders').returns(true)
|
|
742
|
+
resource.expects(:send).with('orders').returns(delegate)
|
|
743
|
+
delegate.expects(:respond_to?).with('foo').returns(true)
|
|
744
|
+
|
|
745
|
+
_(link.respond_to?(:foo)).must_equal true
|
|
746
|
+
end
|
|
747
|
+
|
|
347
748
|
it 'does not delegate to_ary to resource' do
|
|
348
749
|
resource.expects(:to_ary).never
|
|
750
|
+
|
|
349
751
|
_([[link, link]].flatten).must_equal [link, link]
|
|
350
752
|
end
|
|
753
|
+
|
|
754
|
+
it 'delegates through delegate_method when the resource responds but returns nil' do
|
|
755
|
+
delegate = mock('Delegate')
|
|
756
|
+
resource.expects(:respond_to?).with('foo').returns(true)
|
|
757
|
+
resource.expects(:send).with(:foo).returns(nil)
|
|
758
|
+
resource.expects(:respond_to?).with('orders').returns(true)
|
|
759
|
+
resource.expects(:send).with('orders').returns(delegate)
|
|
760
|
+
delegate.expects(:respond_to?).with('foo').returns(true)
|
|
761
|
+
delegate.expects(:send).with(:foo).returns('delegated result')
|
|
762
|
+
|
|
763
|
+
_(link.foo).must_equal 'delegated result'
|
|
764
|
+
end
|
|
765
|
+
|
|
766
|
+
it 'returns nil from delegate_method when the resource has no matching key' do
|
|
767
|
+
keyless_link = Link.new(nil, { 'href' => 'http://myapi.org/orders' }, entry_point)
|
|
768
|
+
resource.expects(:respond_to?).with('foo').returns(true)
|
|
769
|
+
resource.expects(:send).with(:foo).returns(nil)
|
|
770
|
+
|
|
771
|
+
_(keyless_link.foo).must_be_nil
|
|
772
|
+
end
|
|
773
|
+
|
|
774
|
+
it 'returns nil from delegate_method when the delegate does not respond to the method' do
|
|
775
|
+
delegate = mock('Delegate')
|
|
776
|
+
resource.expects(:respond_to?).with('foo').returns(true)
|
|
777
|
+
resource.expects(:send).with(:foo).returns(nil)
|
|
778
|
+
resource.expects(:respond_to?).with('orders').returns(true)
|
|
779
|
+
resource.expects(:send).with('orders').returns(delegate)
|
|
780
|
+
delegate.expects(:respond_to?).with('foo').returns(false)
|
|
781
|
+
|
|
782
|
+
_(link.foo).must_be_nil
|
|
783
|
+
end
|
|
351
784
|
end
|
|
352
785
|
end
|
|
353
786
|
end
|
|
@@ -13,7 +13,7 @@ module Hyperclient
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
it 'initializes its attributes' do
|
|
16
|
-
Attributes.expects(:new).with(foo: :bar)
|
|
16
|
+
Attributes.expects(:new).with({ foo: :bar })
|
|
17
17
|
|
|
18
18
|
Resource.new({ foo: :bar }, entry_point)
|
|
19
19
|
end
|
|
@@ -62,6 +62,7 @@ module Hyperclient
|
|
|
62
62
|
it '_expand' do
|
|
63
63
|
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders/{id}', 'templated' => true } } },
|
|
64
64
|
entry_point)
|
|
65
|
+
|
|
65
66
|
_(resource._links.orders._expand(id: 1)._url).must_equal '/orders/1'
|
|
66
67
|
_(resource.orders._expand(id: 1)._url).must_equal '/orders/1'
|
|
67
68
|
_(resource.orders(id: 1)._url).must_equal '/orders/1'
|
|
@@ -94,16 +95,19 @@ module Hyperclient
|
|
|
94
95
|
describe 'method_missing' do
|
|
95
96
|
it 'delegates to attributes' do
|
|
96
97
|
resource._attributes.expects(:foo).returns('bar')
|
|
98
|
+
|
|
97
99
|
_(resource.foo).must_equal 'bar'
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
it 'delegates to links' do
|
|
101
103
|
resource._links.expects(:foo).returns('bar')
|
|
104
|
+
|
|
102
105
|
_(resource.foo).must_equal 'bar'
|
|
103
106
|
end
|
|
104
107
|
|
|
105
108
|
it 'delegates to embedded' do
|
|
106
109
|
resource._embedded.expects(:foo).returns('bar')
|
|
110
|
+
|
|
107
111
|
_(resource.foo).must_equal 'bar'
|
|
108
112
|
end
|
|
109
113
|
|
|
@@ -111,11 +115,13 @@ module Hyperclient
|
|
|
111
115
|
resource._attributes.expects('respond_to?').with('foo').returns(false)
|
|
112
116
|
resource._links.expects('respond_to?').with('foo').returns(false)
|
|
113
117
|
resource._embedded.expects('respond_to?').with('foo').returns(false)
|
|
118
|
+
|
|
114
119
|
_(-> { resource.foo }).must_raise NoMethodError
|
|
115
120
|
end
|
|
116
121
|
|
|
117
122
|
it 'delegates []' do
|
|
118
123
|
resource._attributes.expects(:foo).returns('bar')
|
|
124
|
+
|
|
119
125
|
_(resource['foo']).must_equal 'bar'
|
|
120
126
|
end
|
|
121
127
|
|
|
@@ -133,6 +139,7 @@ module Hyperclient
|
|
|
133
139
|
describe 'with a default value' do
|
|
134
140
|
it 'returns the value for keys that exist' do
|
|
135
141
|
resource._attributes.expects(:foo).returns('bar')
|
|
142
|
+
|
|
136
143
|
_(resource.fetch('foo', 'default value')).must_equal 'bar'
|
|
137
144
|
end
|
|
138
145
|
|
|
@@ -144,6 +151,7 @@ module Hyperclient
|
|
|
144
151
|
describe 'with a block' do
|
|
145
152
|
it 'returns the value for keys that exist' do
|
|
146
153
|
resource._attributes.expects(:foo).returns('bar')
|
|
154
|
+
|
|
147
155
|
_(resource.fetch('foo') { 'default value' }).must_equal 'bar'
|
|
148
156
|
end
|
|
149
157
|
|
|
@@ -220,5 +228,61 @@ module Hyperclient
|
|
|
220
228
|
end
|
|
221
229
|
end
|
|
222
230
|
end
|
|
231
|
+
|
|
232
|
+
describe '._allow' do
|
|
233
|
+
it 'returns nil without a response object' do
|
|
234
|
+
resource = Resource.new({}, entry_point)
|
|
235
|
+
|
|
236
|
+
_(resource._allow).must_be_nil
|
|
237
|
+
_(resource._allow?(:delete)).must_equal true
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
it 'returns nil without an Allow header' do
|
|
241
|
+
resource = Resource.new({}, entry_point, stub(headers: {}))
|
|
242
|
+
|
|
243
|
+
_(resource._allow).must_be_nil
|
|
244
|
+
_(resource._allow?(:delete)).must_equal true
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
it 'returns the allowed methods' do
|
|
248
|
+
resource = Resource.new({}, entry_point, stub(headers: Faraday::Utils::Headers.new('allow' => 'get, Head')))
|
|
249
|
+
|
|
250
|
+
_(resource._allow).must_equal %w[GET HEAD]
|
|
251
|
+
_(resource._allow?(:head)).must_equal true
|
|
252
|
+
_(resource._allow?('DELETE')).must_equal false
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
it 'returns an empty list for an empty Allow header' do
|
|
256
|
+
resource = Resource.new({}, entry_point, stub(headers: { 'Allow' => ' ' }))
|
|
257
|
+
|
|
258
|
+
_(resource._allow).must_equal []
|
|
259
|
+
_(resource._allow?(:get)).must_equal false
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
describe 'inspect' do
|
|
264
|
+
it 'outputs a custom-friendly output' do
|
|
265
|
+
resource = Resource.new({ '_links' => { 'self' => { 'href' => '/orders/523' } }, 'title' => 'Order' },
|
|
266
|
+
entry_point)
|
|
267
|
+
|
|
268
|
+
_(resource.inspect).must_include 'Resource'
|
|
269
|
+
_(resource.inspect).must_include 'self_link:'
|
|
270
|
+
_(resource.inspect).must_include 'attributes:'
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
describe '#to_h and #to_hash' do
|
|
275
|
+
let(:resource) do
|
|
276
|
+
Resource.new({ '_links' => {}, 'title' => 'Order', 'total' => 42 }, entry_point)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it 'returns the attributes as a Hash from #to_hash' do
|
|
280
|
+
_(resource.to_hash).must_equal({ 'title' => 'Order', 'total' => 42 })
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it 'returns the same result from #to_h as #to_hash' do
|
|
284
|
+
_(resource.to_h).must_equal(resource.to_hash)
|
|
285
|
+
end
|
|
286
|
+
end
|
|
223
287
|
end
|
|
224
288
|
end
|
data/test/hyperclient_test.rb
CHANGED
|
@@ -31,6 +31,7 @@ describe Hyperclient do
|
|
|
31
31
|
|
|
32
32
|
it 'creates a Faraday connection with the default block plus any additional handlers' do
|
|
33
33
|
handlers = client.connection.builder.handlers
|
|
34
|
+
|
|
34
35
|
_(handlers).must_include Faraday::Request::Instrumentation
|
|
35
36
|
_(handlers).must_include Faraday::Response::RaiseError
|
|
36
37
|
_(handlers).must_include Faraday::FollowRedirects::Middleware
|