-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathvimeo_test.js
966 lines (757 loc) · 34 KB
/
vimeo_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/* eslint-env mocha */
'use strict'
const Vimeo = require('../../lib/vimeo').Vimeo
const requestDefaults = require('../../lib/vimeo').request_defaults
const authEndpoints = require('../../lib/vimeo').authEndpoints
const http = require('http') // Needed for mocking
const https = require('https') // Needed for mocking
const fs = require('fs') // Needed for mocking
const events = require('events')
const expect = require('chai').expect
const sinon = require('sinon')
afterEach(() => {
sinon.restore()
})
describe('Vimeo.buildAuthorizationEndpoint', () => {
const REDIRECT_URL = 'https://myapp.com/login'
const vimeo = new Vimeo('id', 'secret', 'token')
it('uses `public` scope by default', () => {
const url = vimeo.buildAuthorizationEndpoint(REDIRECT_URL)
expect(url).to.contain('scope=public')
})
it('uses a space-separated list for scopes', () => {
const url = vimeo.buildAuthorizationEndpoint(REDIRECT_URL, ['scope1', 'scope2'])
expect(url).to.contain('scope=scope1%20scope2')
})
it('uses a space-separated list for scopes', () => {
const url = vimeo.buildAuthorizationEndpoint(REDIRECT_URL, 'scope1 scope2')
expect(url).to.contain('scope=scope1%20scope2')
})
it('uses state if present', () => {
const url = vimeo.buildAuthorizationEndpoint(REDIRECT_URL, 'scope', 'state')
expect(url).to.contain('state=state')
})
it('uses request_defaults to build the URL', () => {
const url = vimeo.buildAuthorizationEndpoint(REDIRECT_URL, 'scope', 'state')
expect(url).to.contain(requestDefaults.protocol)
expect(url).to.contain(requestDefaults.hostname)
})
})
describe('Vimeo.generateClientCredentials', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
describe('request is called with the expected parameters', () => {
let mockRequest
beforeEach(() => {
mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
})
it('with `public` scope by default', () => {
vimeo.generateClientCredentials(null, () => {})
sinon.assert.calledOnce(mockRequest)
sinon.assert.calledWith(mockRequest, sinon.match({ query: sinon.match.has('scope', 'public') }))
})
it('with a space-separated list for scopes', () => {
vimeo.generateClientCredentials(['scope1', 'scope2'], () => {})
sinon.assert.calledOnce(mockRequest)
sinon.assert.calledWith(mockRequest, sinon.match({ query: sinon.match.has('scope', 'scope1 scope2') }))
})
it('with a space-separated list for scopes', () => {
vimeo.generateClientCredentials('scope1 scope2', () => {})
sinon.assert.calledOnce(mockRequest)
sinon.assert.calledWith(mockRequest, sinon.match({ query: sinon.match.has('scope', 'scope1 scope2') }))
})
it('with all defaults', () => {
vimeo.generateClientCredentials(null, () => {})
const expectedPayload = {
method: 'POST',
hostname: requestDefaults.hostname,
path: authEndpoints.clientCredentials,
query: {
grant_type: 'client_credentials'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
sinon.assert.calledOnce(mockRequest)
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
})
describe('request is called with the expected parameters for the promise implementation', () => {
let requestStub
beforeEach(() => {
requestStub = sinon.stub(vimeo, 'request').resolves('Success.')
})
it('with `public` scope by default', async () => {
await vimeo.generateClientCredentials()
sinon.assert.calledOnce(requestStub)
sinon.assert.calledWith(requestStub, sinon.match({ query: sinon.match.has('scope', 'public') }))
})
it('with a space-separated list for scopes', async () => {
await vimeo.generateClientCredentials(['scope1', 'scope2'])
sinon.assert.calledOnce(requestStub)
sinon.assert.calledWith(requestStub, sinon.match({ query: sinon.match.has('scope', 'scope1 scope2') }))
})
it('with a space-separated list for scopes', async () => {
await vimeo.generateClientCredentials('scope1 scope2')
sinon.assert.calledOnce(requestStub)
sinon.assert.calledWith(requestStub, sinon.match({ query: sinon.match.has('scope', 'scope1 scope2') }))
})
it('with all defaults', async () => {
await vimeo.generateClientCredentials()
const expectedPayload = {
method: 'POST',
hostname: requestDefaults.hostname,
path: authEndpoints.clientCredentials,
query: {
grant_type: 'client_credentials'
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
sinon.assert.calledOnce(requestStub)
sinon.assert.calledWith(requestStub, sinon.match(expectedPayload))
})
})
describe('callback is called with the expected parameters', () => {
it('request returns an error', () => {
const error = 'Request Error'
const body = { body: 'body' }
const status = { status: 'status' }
const headers = { headers: 'headers' }
const mockRequest = sinon.fake.yields(error, body, status, headers)
sinon.replace(vimeo, 'request', mockRequest)
const mockCallback = sinon.fake()
vimeo.generateClientCredentials('scope', mockCallback)
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, error, null, status, headers)
})
it('request is successful', () => {
const body = { body: 'body' }
const status = { status: 'status' }
const headers = { headers: 'headers' }
const mockRequest = sinon.fake.yields(null, body, status, headers)
sinon.replace(vimeo, 'request', mockRequest)
const mockCallback = sinon.fake()
vimeo.generateClientCredentials('scope', mockCallback)
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, null, body, status, headers)
})
})
describe('a Promise is returned with the expected response or error when the callback is not passed in', () => {
it('request returns an error', async () => {
const error = new Error('Request Error')
sinon.stub(vimeo, 'request').rejects(error)
await vimeo.generateClientCredentials('scope').catch(err => sinon.assert.match(err, error))
})
it('request is successful', async () => {
const body = 'body'
sinon.stub(vimeo, 'request').resolves(body)
await vimeo.generateClientCredentials('scope').then(res => sinon.assert.match(res, body))
})
})
})
describe('Vimeo.accessToken', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
const CODE = 'code'
const REDIRECT_URI = 'redirectURI'
it('request is called with the expected parameters', () => {
const mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
vimeo.accessToken(CODE, REDIRECT_URI, () => {})
const expectedPayload = {
method: 'POST',
hostname: requestDefaults.hostname,
path: authEndpoints.accessToken,
query: {
grant_type: 'authorization_code',
code: CODE,
redirect_uri: REDIRECT_URI
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
sinon.assert.calledOnce(mockRequest)
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
describe('callback is called with the expected parameters', () => {
it('request returns an error', () => {
const error = 'Request Error'
const body = { body: 'body' }
const status = { status: 'status' }
const headers = { headers: 'headers' }
const mockRequest = sinon.fake.yields(error, body, status, headers)
sinon.replace(vimeo, 'request', mockRequest)
const mockCallback = sinon.fake()
vimeo.accessToken(CODE, REDIRECT_URI, mockCallback)
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, error, null, status, headers)
})
it('request is successful', () => {
const body = { body: 'body' }
const status = { status: 'status' }
const headers = { headers: 'headers' }
const mockRequest = sinon.fake.yields(null, body, status, headers)
sinon.replace(vimeo, 'request', mockRequest)
const mockCallback = sinon.fake()
vimeo.accessToken(CODE, REDIRECT_URI, mockCallback)
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, null, body, status, headers)
})
})
describe('request and response are expected for the Promise implementation', () => {
it('vimeo.request is called with the expected parameters', async () => {
const requestStub = sinon.stub(vimeo, 'request').resolves('Success.')
await vimeo.accessToken(CODE, REDIRECT_URI)
const expectedPayload = {
method: 'POST',
hostname: requestDefaults.hostname,
path: authEndpoints.accessToken,
query: {
grant_type: 'authorization_code',
code: CODE,
redirect_uri: REDIRECT_URI
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
sinon.assert.calledOnce(requestStub)
sinon.assert.calledWith(requestStub, sinon.match(expectedPayload))
})
it('returns expected error response when the request fails', async () => {
const err = new Error('Request Error')
sinon.stub(vimeo, 'request').rejects(err)
await vimeo.accessToken(CODE, REDIRECT_URI).catch((error) => {
sinon.assert.match(error, err)
})
})
it('returns the expected response when the request is successful', async () => {
const body = 'body'
sinon.stub(vimeo, 'request').resolves(body)
await vimeo.accessToken(CODE, REDIRECT_URI).then((res) => {
sinon.assert.match(res, body)
})
})
})
})
describe('Vimeo.setAccessToken', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
it('changes the access token', () => {
vimeo.setAccessToken('token2')
expect(vimeo._accessToken).to.equal('token2')
})
})
describe('Vimeo._applyQuerystringParams', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
const PATH = '/path'
const PATH_QS = '/path?a=b'
const QS = { c: 'd' }
it('returns the path if no query is passed', () => {
const newPath = vimeo._applyQuerystringParams({ path: PATH }, {})
expect(newPath).to.equal(PATH)
})
it('returns the path if no query is passed', () => {
const newPath = vimeo._applyQuerystringParams({ path: PATH_QS }, {})
expect(newPath).to.equal(PATH_QS)
})
it('adds the query string after the ?', () => {
const newPath = vimeo._applyQuerystringParams({ path: PATH }, { query: QS })
expect(newPath).to.equal(PATH + '?c=d')
})
it('appens the query string after the &', () => {
const newPath = vimeo._applyQuerystringParams({ path: PATH_QS }, { query: QS })
expect(newPath).to.equal(PATH_QS + '&c=d')
})
})
describe('Vimeo.request', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
let mockHttpRequest, mockHttpsRequest, mockReq, handleRequestStub
beforeEach(() => {
mockReq = new events.EventEmitter()
mockReq.on = sinon.fake(mockReq.on)
mockReq.end = sinon.fake()
mockReq.write = sinon.fake()
mockHttpRequest = sinon.fake.returns(mockReq)
sinon.replace(http, 'request', mockHttpRequest)
mockHttpsRequest = sinon.fake.returns(mockReq)
sinon.replace(https, 'request', mockHttpsRequest)
})
describe('callback function is used when passed in', () => {
it('calls callback with an error if options has no path', () => {
const mockCallback = sinon.fake()
vimeo.request({}, mockCallback)
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, sinon.match.instanceOf(Error))
})
it('parses options if passed as a string', () => {
vimeo.request('https://example.com:1234/path', () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ method: 'GET', path: '/path', host: 'example.com', port: '1234' }))
})
it('adds a leading slash if missing', () => {
vimeo.request({ path: 'path' }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ path: '/path' }))
})
it('uses https client when requested', () => {
vimeo.request({ protocol: 'https:', path: '/path' }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.notCalled(mockHttpRequest)
})
it('uses http client by default', () => {
vimeo.request({ protocol: 'proto:', path: '/path' }, () => { })
sinon.assert.calledOnce(mockHttpRequest)
sinon.assert.notCalled(mockHttpsRequest)
})
it('sends body as JSON if content type is application/json', () => {
vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' }, headers: { 'Content-Type': 'application/json' } }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: '{"a":"b"}' }))
})
it('sends form data as string if content type is application/x-www-form-urlencoded', () => {
vimeo.request({ method: 'POST', path: '/path', query: { a: 'b', c: 'd' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: 'a=b&c=d' }))
})
it('sends body as it is if content type is not application/x-www-form-urlencoded nor application/json', () => {
vimeo.request({ method: 'POST', path: '/path', body: 'text', headers: { 'Content-Type': 'text/plain' } }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: 'text' }))
})
it('sets the correct body Content-Length', () => {
vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' }, headers: { 'Content-Type': 'application/json' } }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ headers: sinon.match.has('Content-Length', 9) }))
})
it('sets the correct body Content-Length', () => {
vimeo.request({ method: 'POST', path: '/path' }, () => { })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ headers: sinon.match.has('Content-Length', 0) }))
})
it('calls req.write with the body', () => {
vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' } }, () => { })
sinon.assert.calledOnce(mockReq.write)
sinon.assert.calledWith(mockReq.write, '{"a":"b"}')
})
it('doesn\'t call req.write if there is no body', () => {
vimeo.request({ method: 'POST', path: '/path' }, () => { })
sinon.assert.notCalled(mockReq.write)
})
it('sets on error listener', () => {
const mockCallback = sinon.fake()
vimeo.request({ path: '/path' }, mockCallback)
sinon.assert.calledOnce(mockReq.on)
sinon.assert.calledWith(mockReq.on, 'error', sinon.match.func)
mockReq.emit('error', 'Error Emitted')
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, 'Error Emitted')
})
it('calls req.end()', () => {
vimeo.request({ path: '/path' }, () => { })
sinon.assert.calledOnce(mockReq.end)
})
})
describe('a Promise is returned when the callback function is not passed in', () => {
beforeEach(() => {
handleRequestStub = sinon.stub(vimeo, '_handleRequest').callsFake((resolve) => { resolve('Success.') })
})
it('returns an error if options has no path', async () => {
const error = sinon.match.instanceOf(Error).and(sinon.match.has('message', 'You must provide an API path.'))
await vimeo.request({}).catch((e) => sinon.assert.match(e, error))
})
it('parses options if passed as a string', async () => {
await vimeo.request('https://example.com:1234/path')
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ method: 'GET', path: '/path', host: 'example.com', port: '1234' }))
sinon.assert.calledOnce(handleRequestStub)
})
it('adds a leading slash if missing', async () => {
await vimeo.request({ path: 'path' })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ path: '/path' }))
})
it('uses https client when requested', async () => {
await vimeo.request({ protocol: 'https:', path: '/path' })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.notCalled(mockHttpRequest)
})
it('uses http client by default', async () => {
await vimeo.request({ protocol: 'proto:', path: '/path' })
sinon.assert.calledOnce(mockHttpRequest)
sinon.assert.notCalled(mockHttpsRequest)
})
it('sends body as JSON if content type is application/json', async () => {
await vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' }, headers: { 'Content-Type': 'application/json' } })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: '{"a":"b"}' }))
})
it('sends form data as string if content type is application/x-www-form-urlencoded', async () => {
await vimeo.request({ method: 'POST', path: '/path', query: { a: 'b', c: 'd' }, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: 'a=b&c=d' }))
})
it('sends body as it is if content type is not application/x-www-form-urlencoded nor application/json', async () => {
await vimeo.request({ method: 'POST', path: '/path', body: 'text', headers: { 'Content-Type': 'text/plain' } })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ body: 'text' }))
})
it('sets the correct body Content-Length', async () => {
await vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' }, headers: { 'Content-Type': 'application/json' } })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ headers: sinon.match.has('Content-Length', 9) }))
})
it('sets the correct body Content-Length', async () => {
await vimeo.request({ method: 'POST', path: '/path' })
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ headers: sinon.match.has('Content-Length', 0) }))
})
it('calls req.write with the body', async () => {
await vimeo.request({ method: 'POST', path: '/path', query: { a: 'b' } })
sinon.assert.calledOnce(mockReq.write)
sinon.assert.calledWith(mockReq.write, '{"a":"b"}')
})
it('doesn\'t call req.write if there is no body', async () => {
await vimeo.request({ method: 'POST', path: '/path' })
sinon.assert.notCalled(mockReq.write)
})
it('calls req.end()', async () => {
await vimeo.request({ path: '/path' })
sinon.assert.calledOnce(mockReq.end)
})
it('returns the correct response when the Promise is fullfilled', async () => {
const req = await vimeo.request({ method: 'POST', path: '/path' })
expect(req).to.equal('Success.')
})
it('returns the error object when the Promise is rejected', async () => {
handleRequestStub.resetBehavior()
const err = new Error('Request Error')
handleRequestStub.callsFake((resolve, reject) => {
reject(err)
})
await vimeo.request({ method: 'POST', path: '/path' }).catch((e) => {
expect(e).to.equal(err)
})
})
it('sets on error listener', async () => {
handleRequestStub.resetBehavior()
const req = vimeo.request({ path: '/path' }).catch((error) => {
sinon.assert.match(error, sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Error Emitted')))
})
mockReq.emit('error', new Error('Error Emitted'))
sinon.assert.calledOnce(mockReq.on)
sinon.assert.calledWith(mockReq.on, 'error', sinon.match.func)
return req
})
it('throws an error when the Promise rejects', async () => {
handleRequestStub.resetBehavior()
handleRequestStub.callsFake((resolve, reject) => { reject(new Error('Failure.')) })
const error = sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Failure.'))
await vimeo.request('https://example.com:1234/path').catch((e) => {
sinon.assert.match(e, error)
})
sinon.assert.calledOnce(mockHttpsRequest)
sinon.assert.calledWith(mockHttpsRequest, sinon.match({ method: 'GET', path: '/path', host: 'example.com', port: '1234' }))
sinon.assert.calledOnce(handleRequestStub)
})
})
})
describe('Vimeo._handleRequest', () => {
const vimeo = new Vimeo('id', 'secret', 'token')
let mockRes
beforeEach(() => {
mockRes = new events.EventEmitter()
mockRes.on = sinon.fake(mockRes.on)
mockRes.setEncoding = sinon.fake()
mockRes.headers = { headers: 'value' }
})
it('sets the encoding to utf8', () => {
const handler = vimeo._handleRequest(() => { })
handler(mockRes)
sinon.assert.calledOnce(mockRes.setEncoding)
sinon.assert.calledWith(mockRes.setEncoding, 'utf8')
})
it('calls callback with an error if status code >= 400', () => {
const mockCallback = sinon.fake()
const handler = vimeo._handleRequest(mockCallback)
mockRes.statusCode = 404
handler(mockRes)
mockRes.emit('end')
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, sinon.match.instanceOf(Error), '', mockRes.statusCode, mockRes.headers)
})
it('calls callback no error if status code < 400', () => {
const mockCallback = sinon.fake()
const handler = vimeo._handleRequest(mockCallback)
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('end')
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, null, {}, mockRes.statusCode, mockRes.headers)
})
it('calls callback with an error if the body is not valid JSON', () => {
const mockCallback = sinon.fake()
const handler = vimeo._handleRequest(mockCallback)
mockRes.read = sinon.fake.returns('{"bad": "json"')
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('readable')
mockRes.emit('end')
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Unexpected end of JSON input')))
})
it('calls callback the body parsed as JSON', () => {
const mockCallback = sinon.fake()
const handler = vimeo._handleRequest(mockCallback)
mockRes.read = sinon.fake.returns('{"good": "json"}')
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('readable')
mockRes.emit('end')
sinon.assert.calledOnce(mockCallback)
sinon.assert.calledWith(mockCallback, null, { good: 'json' }, mockRes.statusCode, mockRes.headers)
})
describe('when there is a second fn passed in, calls the second fn on error', () => {
let mockResolve, mockReject, handler
beforeEach(() => {
mockResolve = sinon.fake()
mockReject = sinon.fake()
handler = vimeo._handleRequest(mockResolve, mockReject)
})
it('sets the encoding to utf8', () => {
const handler = vimeo._handleRequest(() => { }, () => { })
handler(mockRes)
sinon.assert.calledOnce(mockRes.setEncoding)
sinon.assert.calledWith(mockRes.setEncoding, 'utf8')
})
it('calls the second fn with an error if status code >= 400', () => {
mockRes.statusCode = 404
handler(mockRes)
mockRes.emit('end')
sinon.assert.calledOnce(mockReject)
sinon.assert.calledWith(mockReject, sinon.match.instanceOf(Error))
})
it('calls the first fn with no error if status code < 400', () => {
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('end')
sinon.assert.calledOnce(mockResolve)
sinon.assert.calledWith(mockResolve, { body: {}, headers: mockRes.headers, statusCode: mockRes.statusCode })
})
it('calls the second fn with an error if the body is not valid JSON', () => {
mockRes.read = sinon.fake.returns('{"bad": "json"')
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('readable')
mockRes.emit('end')
sinon.assert.calledOnce(mockReject)
sinon.assert.calledWith(mockReject,
sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Unexpected end of JSON input')),
'{"bad": "json"',
mockRes.statusCode,
mockRes.headers)
})
it('calls the first fn if the body parsed as JSON', () => {
mockRes.read = sinon.fake.returns('{"good": "json"}')
mockRes.statusCode = 200
handler(mockRes)
mockRes.emit('readable')
mockRes.emit('end')
sinon.assert.calledOnce(mockResolve)
sinon.assert.calledWith(mockResolve, { body: { good: 'json' }, headers: mockRes.headers, statusCode: mockRes.statusCode })
})
})
})
describe('Vimeo.upload', () => {
const FILE_NAME = '/real/file'
const FILE_SIZE = 24601
const vimeo = new Vimeo('id', 'secret', 'token')
let mockCompleteCallback
let mockProgressCallback
let mockErrorCallback
beforeEach(() => {
mockCompleteCallback = sinon.fake()
mockProgressCallback = sinon.fake()
mockErrorCallback = sinon.fake()
})
it('calls the errorCallback if the file is inexistant', () => {
const errFs = sinon.fake.throws('File Error')
sinon.replace(fs, 'statSync', errFs)
const vimeo = new Vimeo('id', 'secret', 'token')
vimeo.upload(FILE_NAME, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, 'Unable to locate file to upload.')
})
it('calls the errorCallback if the file parameter is an object', () => {
const fileObject = {
size: FILE_SIZE
}
vimeo.upload(fileObject, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Please pass in a valid file path.')))
})
describe('file exists', () => {
beforeEach(() => {
const mockFs = sinon.fake.returns({ size: FILE_SIZE })
sinon.replace(fs, 'statSync', mockFs)
})
describe('always uses `tus` to upload', () => {
let mockRequest
beforeEach(() => {
mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
})
it('if upload.approach is not specified', () => {
vimeo.upload(FILE_NAME, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
query: { upload: { approach: 'tus' } }
}
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
it('if upload.approach is not tus', () => {
vimeo.upload(FILE_NAME, { upload: { approach: 'not-tus' } }, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
query: { upload: { approach: 'tus' } }
}
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
})
it('request is called with the expected parameters', () => {
const mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
vimeo.upload(FILE_NAME, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
method: 'POST',
path: '/me/videos?fields=uri,name,upload',
query: { upload: { approach: 'tus', size: FILE_SIZE } }
}
sinon.assert.calledWith(mockRequest, expectedPayload)
})
it('calls the errorCallback if request returned an error', () => {
const mockRequest = sinon.fake.yields('Request Error')
sinon.replace(vimeo, 'request', mockRequest)
vimeo.upload(FILE_NAME, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, sinon.match('Request Error'))
})
it('calls _performTusUpload with the expected parameters', () => {
const mockRequest = sinon.fake.yields(null, {})
sinon.replace(vimeo, 'request', mockRequest)
const mockTusUpload = sinon.fake()
sinon.replace(vimeo, '_performTusUpload', mockTusUpload)
vimeo.upload(FILE_NAME, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockTusUpload)
sinon.assert.calledWith(mockTusUpload, FILE_NAME, FILE_SIZE, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
})
it('shifts callbacks if param is not passed to the function', () => {
const mockRequest = sinon.fake.yields(null, {})
sinon.replace(vimeo, 'request', mockRequest)
const mockTusUpload = sinon.fake()
sinon.replace(vimeo, '_performTusUpload', mockTusUpload)
vimeo.upload(FILE_NAME, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockTusUpload)
sinon.assert.calledWith(mockTusUpload, FILE_NAME, FILE_SIZE, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
})
})
})
describe('Vimeo.replace', () => {
const FILE_NAME = '/real/file/name'
const FILE_SIZE = 24601
const VIDEO_URI = '/videos/123456789'
const vimeo = new Vimeo('id', 'secret', 'token')
let mockCompleteCallback
let mockProgressCallback
let mockErrorCallback
beforeEach(() => {
mockCompleteCallback = sinon.fake()
mockProgressCallback = sinon.fake()
mockErrorCallback = sinon.fake()
})
it('calls the errorCallback if the file is inexistant', () => {
const errFs = sinon.fake.throws('File Error')
sinon.replace(fs, 'statSync', errFs)
const vimeo = new Vimeo('id', 'secret', 'token')
vimeo.replace(FILE_NAME, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, 'Unable to locate file to upload.')
})
it('calls the errorCallback if the file parameter is an object', () => {
const fileObject = {
size: FILE_SIZE
}
vimeo.replace(fileObject, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, sinon.match.instanceOf(Error).and(sinon.match.has('message', 'Please pass in a valid file path.')))
})
describe('file exists', () => {
beforeEach(() => {
const mockFs = sinon.fake.returns({ size: FILE_SIZE })
sinon.replace(fs, 'statSync', mockFs)
})
describe('always uses `tus` to upload', () => {
let mockRequest
beforeEach(() => {
mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
})
it('if upload.approach is not specified', () => {
vimeo.replace(FILE_NAME, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
query: { upload: { approach: 'tus' } }
}
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
it('if upload.approach is not tus', () => {
vimeo.replace(FILE_NAME, VIDEO_URI, { upload: { approach: 'not-tus' } }, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
query: { upload: { approach: 'tus' } }
}
sinon.assert.calledWith(mockRequest, sinon.match(expectedPayload))
})
})
it('request is called with the expected parameters', () => {
const mockRequest = sinon.fake()
sinon.replace(vimeo, 'request', mockRequest)
vimeo.replace(FILE_NAME, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockRequest)
const expectedPayload = {
method: 'POST',
path: VIDEO_URI + '/versions?fields=upload',
query: { file_name: 'name', upload: { approach: 'tus', size: FILE_SIZE } }
}
sinon.assert.calledWith(mockRequest, expectedPayload)
})
it('calls the errorCallback if request returned an error', () => {
const mockRequest = sinon.fake.yields('Request Error')
sinon.replace(vimeo, 'request', mockRequest)
vimeo.replace(FILE_NAME, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockErrorCallback)
sinon.assert.calledWith(mockErrorCallback, sinon.match('Request Error'))
})
it('calls _performTusUpload with the expected parameters', () => {
const mockRequest = sinon.fake.yields(null, {})
sinon.replace(vimeo, 'request', mockRequest)
const mockTusUpload = sinon.fake()
sinon.replace(vimeo, '_performTusUpload', mockTusUpload)
vimeo.replace(FILE_NAME, VIDEO_URI, {}, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockTusUpload)
sinon.assert.calledWith(mockTusUpload, FILE_NAME, FILE_SIZE, { uri: VIDEO_URI }, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
})
it('shifts callbacks if param is not passed to the function', () => {
const mockRequest = sinon.fake.yields(null, {})
sinon.replace(vimeo, 'request', mockRequest)
const mockTusUpload = sinon.fake()
sinon.replace(vimeo, '_performTusUpload', mockTusUpload)
vimeo.replace(FILE_NAME, VIDEO_URI, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
sinon.assert.calledOnce(mockTusUpload)
sinon.assert.calledWith(mockTusUpload, FILE_NAME, FILE_SIZE, { uri: VIDEO_URI }, mockCompleteCallback, mockProgressCallback, mockErrorCallback)
})
})
})