@@ -287,7 +287,7 @@ func TestUploadFiles_Cancelation(t *testing.T) {
287
287
for _ , bundle := range files .Files {
288
288
d .Required = append (d .Required , bundle .Sum )
289
289
}
290
- err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute )
290
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute , false )
291
291
require .ErrorIs (t , err , gocontext .Canceled )
292
292
}
293
293
@@ -317,10 +317,131 @@ func TestUploadFiles_Errors(t *testing.T) {
317
317
for _ , bundle := range files .Files {
318
318
d .Required = append (d .Required , bundle .Sum )
319
319
}
320
- err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute )
320
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute , false )
321
321
require .Equal (t , err .Error (), "[PUT /deploys/{deploy_id}/files/{path}][500] uploadDeployFile default &{Code:0 Message:}" )
322
322
}
323
323
324
+ func TestUploadFiles422Error_SkipsRetry (t * testing.T ) {
325
+ attempts := 0
326
+ ctx := gocontext .Background ()
327
+
328
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , _ * http.Request ) {
329
+ defer func () {
330
+ attempts ++
331
+ }()
332
+
333
+ rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
334
+ rw .WriteHeader (http .StatusUnprocessableEntity )
335
+ rw .Write ([]byte (`{"message": "Unprocessable Entity", "code": 422 }` ))
336
+ }))
337
+ defer server .Close ()
338
+
339
+ // File upload:
340
+ hu , _ := url .Parse (server .URL )
341
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http .DefaultClient )
342
+ client := NewRetryable (tr , strfmt .Default , 1 )
343
+ client .uploadLimit = 1
344
+ ctx = context .WithAuthInfo (ctx , apiClient .BearerToken ("token" ))
345
+
346
+ // Create some files to deploy
347
+ dir , err := ioutil .TempDir ("" , "deploy" )
348
+ require .NoError (t , err )
349
+ defer os .RemoveAll (dir )
350
+ require .NoError (t , ioutil .WriteFile (filepath .Join (dir , "foo.html" ), []byte ("Hello" ), 0644 ))
351
+
352
+ files , err := walk (dir , nil , false , false )
353
+ require .NoError (t , err )
354
+ d := & models.Deploy {}
355
+ for _ , bundle := range files .Files {
356
+ d .Required = append (d .Required , bundle .Sum )
357
+ }
358
+ // Set SkipRetry to true
359
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute , true )
360
+ require .ErrorContains (t , err , "Code:422 Message:Unprocessable Entity" )
361
+ require .Equal (t , attempts , 1 )
362
+ }
363
+
364
+ func TestUploadFunctions422Error_SkipsRetry (t * testing.T ) {
365
+ attempts := 0
366
+ ctx := gocontext .Background ()
367
+
368
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , _ * http.Request ) {
369
+ defer func () {
370
+ attempts ++
371
+ }()
372
+
373
+ rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
374
+ rw .WriteHeader (http .StatusUnprocessableEntity )
375
+ rw .Write ([]byte (`{"message": "Unprocessable Entity", "code": 422 }` ))
376
+ }))
377
+ defer server .Close ()
378
+
379
+ // Function upload:
380
+ hu , _ := url .Parse (server .URL )
381
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http .DefaultClient )
382
+ client := NewRetryable (tr , strfmt .Default , 1 )
383
+ client .uploadLimit = 1
384
+ apiCtx := context .WithAuthInfo (ctx , apiClient .BearerToken ("token" ))
385
+
386
+ dir , err := ioutil .TempDir ("" , "deploy" )
387
+ functionsPath := filepath .Join (dir , ".netlify" , "functions" )
388
+ os .MkdirAll (functionsPath , os .ModePerm )
389
+ require .NoError (t , err )
390
+ defer os .RemoveAll (dir )
391
+ require .NoError (t , ioutil .WriteFile (filepath .Join (functionsPath , "foo.js" ), []byte ("module.exports = () => {}" ), 0644 ))
392
+
393
+ files , _ , _ , err := bundle (ctx , functionsPath , mockObserver {})
394
+ require .NoError (t , err )
395
+ d := & models.Deploy {}
396
+ for _ , bundle := range files .Files {
397
+ d .RequiredFunctions = append (d .RequiredFunctions , bundle .Sum )
398
+ }
399
+ // Set SkipRetry to true
400
+ err = client .uploadFiles (apiCtx , d , files , nil , functionUpload , time .Minute , true )
401
+ require .ErrorContains (t , err , "Code:422 Message:Unprocessable Entity" )
402
+ require .Equal (t , attempts , 1 )
403
+ }
404
+
405
+ func TestUploadFiles400Error_NoSkipRetry (t * testing.T ) {
406
+ attempts := 0
407
+ ctx := gocontext .Background ()
408
+
409
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , _ * http.Request ) {
410
+ defer func () {
411
+ attempts ++
412
+ }()
413
+
414
+ rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
415
+ rw .WriteHeader (http .StatusBadRequest )
416
+ rw .Write ([]byte (`{"message": "Bad Request", "code": 400 }` ))
417
+ return
418
+ }))
419
+ defer server .Close ()
420
+
421
+ hu , _ := url .Parse (server .URL )
422
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http .DefaultClient )
423
+ client := NewRetryable (tr , strfmt .Default , 1 )
424
+ client .uploadLimit = 1
425
+ ctx = context .WithAuthInfo (ctx , apiClient .BearerToken ("token" ))
426
+
427
+ // Create some files to deploy
428
+ dir , err := ioutil .TempDir ("" , "deploy" )
429
+ require .NoError (t , err )
430
+ defer os .RemoveAll (dir )
431
+ require .NoError (t , ioutil .WriteFile (filepath .Join (dir , "foo.html" ), []byte ("Hello" ), 0644 ))
432
+
433
+ files , err := walk (dir , nil , false , false )
434
+ require .NoError (t , err )
435
+ d := & models.Deploy {}
436
+ for _ , bundle := range files .Files {
437
+ d .Required = append (d .Required , bundle .Sum )
438
+ }
439
+ // Set SkipRetry to false
440
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute , false )
441
+ require .ErrorContains (t , err , "Code:400 Message:Bad Request" )
442
+ require .Greater (t , attempts , 1 )
443
+ }
444
+
324
445
func TestUploadFiles_SkipEqualFiles (t * testing.T ) {
325
446
ctx := gocontext .Background ()
326
447
@@ -377,11 +498,11 @@ func TestUploadFiles_SkipEqualFiles(t *testing.T) {
377
498
d .Required = []string {files .Sums ["a.html" ]}
378
499
d .RequiredFunctions = []string {functions .Sums ["a" ]}
379
500
380
- err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute )
501
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute , false )
381
502
require .NoError (t , err )
382
503
assert .Equal (t , 1 , serverRequests )
383
504
384
- err = client .uploadFiles (ctx , d , functions , nil , functionUpload , time .Minute )
505
+ err = client .uploadFiles (ctx , d , functions , nil , functionUpload , time .Minute , false )
385
506
require .NoError (t , err )
386
507
assert .Equal (t , 2 , serverRequests )
387
508
}
@@ -437,7 +558,7 @@ func TestUploadFunctions_RetryCountHeader(t *testing.T) {
437
558
d .RequiredFunctions = append (d .RequiredFunctions , bundle .Sum )
438
559
}
439
560
440
- require .NoError (t , client .uploadFiles (apiCtx , d , files , nil , functionUpload , time .Minute ))
561
+ require .NoError (t , client .uploadFiles (apiCtx , d , files , nil , functionUpload , time .Minute , false ))
441
562
}
442
563
443
564
func TestBundle (t * testing.T ) {
0 commit comments