@@ -6,7 +6,8 @@ import getRawBody from 'raw-body';
6
6
const CLI_INDEX = './bin/logrocket' ;
7
7
const FIXTURE_PATH = './test/fixtures/' ;
8
8
9
- const executeCommand = async ( cmd , { env = '' } = { } ) => {
9
+ const executeCommand = async ( cmdAsStringOrArray , { env = '' } = { } ) => {
10
+ const cmd = Array . isArray ( cmdAsStringOrArray ) ? cmdAsStringOrArray . join ( ' ' ) : cmdAsStringOrArray ;
10
11
return new Promise ( resolve => {
11
12
exec (
12
13
`${ env } ${ CLI_INDEX } ${ cmd } ` ,
@@ -26,11 +27,24 @@ describe('CLI dispatch tests', function cliTests() {
26
27
let matchedRequests ;
27
28
28
29
const addExpectRequest = ( url , opts ) => {
29
- expectRequests [ url ] = {
30
+ if ( ! expectRequests [ url ] ) {
31
+ expectRequests [ url ] = [ ] ;
32
+ }
33
+
34
+ expectRequests [ url ] . push ( {
30
35
body : { } ,
31
36
status : 200 ,
32
37
...opts ,
33
- } ;
38
+ } ) ;
39
+ } ;
40
+
41
+ const addArtifactRequest = ( ) => {
42
+ const uploadID = ( 100000 + Math . floor ( Math . random ( ) * 999999 ) ) . toString ( 16 ) ;
43
+ addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
44
+ status : 200 ,
45
+ body : { signed_url : `http://localhost:8818/upload/${ uploadID } ` } ,
46
+ } ) ;
47
+ addExpectRequest ( `/upload/${ uploadID } ` , { status : 200 } ) ;
34
48
} ;
35
49
36
50
const addCliStatusMessage = ( { message = '' , status = 204 } = { } ) => {
@@ -59,15 +73,16 @@ describe('CLI dispatch tests', function cliTests() {
59
73
} ;
60
74
server = createServer ( async ( req , res ) => {
61
75
const parts = parse ( req . url ) ;
76
+ const expected = expectRequests [ parts . pathname ] || [ ] ;
62
77
63
- if ( expectRequests [ parts . pathname ] ) {
78
+ if ( expected && expected . length ) {
64
79
const body = await getRawBody ( req ) ;
65
80
const req2 = req ;
66
81
67
82
req2 . body = body . toString ( ) ;
68
83
matchedRequests . push ( simplifyRequest ( req2 ) ) ;
69
84
70
- const request = expectRequests [ parts . pathname ] ;
85
+ const request = expected . shift ( ) ;
71
86
72
87
res . writeHead ( request . status , { 'Content-Type' : 'application/json' } ) ;
73
88
res . write ( JSON . stringify ( request . body ) ) ;
@@ -281,12 +296,10 @@ describe('CLI dispatch tests', function cliTests() {
281
296
282
297
it ( 'should upload the passed directory' , mochaAsync ( async ( ) => {
283
298
addCliStatusMessage ( ) ;
284
- addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
285
- status : 200 ,
286
- body : { signed_url : 'http://localhost:8818/upload/' } ,
287
- } ) ;
288
299
289
- addExpectRequest ( '/upload/' , { status : 200 } ) ;
300
+ addArtifactRequest ( ) ;
301
+ addArtifactRequest ( ) ;
302
+ addArtifactRequest ( ) ;
290
303
291
304
const result = await executeCommand ( `upload -k org:app:secret -r 1.0.2 --apihost="http://localhost:8818" ${ FIXTURE_PATH } ` ) ;
292
305
@@ -336,12 +349,10 @@ describe('CLI dispatch tests', function cliTests() {
336
349
337
350
it ( 'should support a custom url prefix' , mochaAsync ( async ( ) => {
338
351
addCliStatusMessage ( ) ;
339
- addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
340
- status : 200 ,
341
- body : { signed_url : 'http://localhost:8818/upload/' } ,
342
- } ) ;
343
352
344
- addExpectRequest ( '/upload/' , { status : 200 } ) ;
353
+ addArtifactRequest ( ) ;
354
+ addArtifactRequest ( ) ;
355
+ addArtifactRequest ( ) ;
345
356
346
357
const result = await executeCommand ( `upload -k org:app:secret -r 1.0.2 --apihost="http://localhost:8818" ${ FIXTURE_PATH } --url-prefix="~/public"` ) ;
347
358
@@ -376,12 +387,8 @@ describe('CLI dispatch tests', function cliTests() {
376
387
377
388
it ( 'should upload the passed file' , mochaAsync ( async ( ) => {
378
389
addCliStatusMessage ( ) ;
379
- addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
380
- status : 200 ,
381
- body : { signed_url : 'http://localhost:8818/upload/' } ,
382
- } ) ;
383
390
384
- addExpectRequest ( '/upload/' , { status : 200 } ) ;
391
+ addArtifactRequest ( ) ;
385
392
386
393
const result = await executeCommand ( `upload -k org:app:secret -r 1.0.2 --apihost="http://localhost:8818" ${ FIXTURE_PATH } subdir/one.js` ) ;
387
394
@@ -404,12 +411,9 @@ describe('CLI dispatch tests', function cliTests() {
404
411
405
412
it ( 'should upload the passed files' , mochaAsync ( async ( ) => {
406
413
addCliStatusMessage ( ) ;
407
- addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
408
- status : 200 ,
409
- body : { signed_url : 'http://localhost:8818/upload/' } ,
410
- } ) ;
411
414
412
- addExpectRequest ( '/upload/' , { status : 200 } ) ;
415
+ addArtifactRequest ( ) ;
416
+ addArtifactRequest ( ) ;
413
417
414
418
const result = await executeCommand ( `upload -k org:app:secret -r 1.0.2 --apihost="http://localhost:8818" ${ FIXTURE_PATH } subdir/one.js ${ FIXTURE_PATH } two.jsx` ) ;
415
419
@@ -455,4 +459,60 @@ describe('CLI dispatch tests', function cliTests() {
455
459
expect ( result . err . code ) . to . equal ( 1 ) ;
456
460
expect ( result . stderr ) . to . contain ( 'Some error to show' ) ;
457
461
} ) ) ;
462
+
463
+ it ( 'should retry failed uploads' , mochaAsync ( async ( ) => {
464
+ addCliStatusMessage ( ) ;
465
+
466
+ addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
467
+ status : 200 ,
468
+ body : { signed_url : 'http://localhost:8818/upload/' } ,
469
+ } ) ;
470
+ addExpectRequest ( '/upload/' , { status : 429 } ) ;
471
+ addExpectRequest ( '/upload/' , { status : 500 } ) ;
472
+ addExpectRequest ( '/upload/' , { status : 502 } ) ;
473
+ addExpectRequest ( '/upload/' , { status : 503 } ) ;
474
+ addExpectRequest ( '/upload/' , { status : 504 } ) ;
475
+ addExpectRequest ( '/upload/' , { status : 200 } ) ;
476
+
477
+ const result = await executeCommand ( [
478
+ 'upload' ,
479
+ '-k org:app:secret' ,
480
+ '-r 1.0.2' ,
481
+ '--apihost="http://localhost:8818"' ,
482
+ '--max-retries 5' ,
483
+ '--max-retry-delay 100' ,
484
+ `${ FIXTURE_PATH } subdir/one.js` ,
485
+ ] ) ;
486
+
487
+ expect ( result . err ) . to . be . null ( ) ;
488
+ expect ( result . stdout ) . to . contain ( 'Found 1 file' ) ;
489
+ expect ( matchedRequests ) . to . have . length ( 8 ) ;
490
+ expect ( unmatchedRequests ) . to . have . length ( 0 ) ;
491
+ } ) ) ;
492
+
493
+ it ( 'should stop retrying after the configured maximum' , mochaAsync ( async ( ) => {
494
+ addCliStatusMessage ( ) ;
495
+
496
+ addExpectRequest ( '/v1/orgs/org/apps/app/releases/1.0.2/artifacts/' , {
497
+ status : 200 ,
498
+ body : { signed_url : 'http://localhost:8818/upload/' } ,
499
+ } ) ;
500
+ addExpectRequest ( '/upload/' , { status : 429 } ) ;
501
+ addExpectRequest ( '/upload/' , { status : 500 } ) ;
502
+
503
+ const result = await executeCommand ( [
504
+ 'upload' ,
505
+ '-k org:app:secret' ,
506
+ '-r 1.0.2' ,
507
+ '--apihost="http://localhost:8818"' ,
508
+ '--max-retries 1' ,
509
+ '--max-retry-delay 100' ,
510
+ `${ FIXTURE_PATH } subdir/one.js` ,
511
+ ] ) ;
512
+
513
+ expect ( result . err . message ) . to . contain ( 'Failed to upload: one.js' ) ;
514
+ expect ( result . stdout ) . to . contain ( 'Found 1 file' ) ;
515
+ expect ( matchedRequests ) . to . have . length ( 4 ) ;
516
+ expect ( unmatchedRequests ) . to . have . length ( 0 ) ;
517
+ } ) ) ;
458
518
} ) ;
0 commit comments