@@ -78,6 +78,24 @@ describe('leetcode_client', function() {
78
78
done ( ) ;
79
79
} ) ;
80
80
} ) ;
81
+
82
+ it ( 'should fail if http error in relogin' , function ( done ) {
83
+ config . AUTO_LOGIN = true ;
84
+ nock ( config . URL_PROBLEMS ) . get ( '/' ) . reply ( 403 ) ;
85
+ core . login = function ( user , cb ) {
86
+ return cb ( 'unknown error!' ) ;
87
+ } ;
88
+
89
+ // the original error will be returned instead
90
+ var expected = {
91
+ msg : 'session expired, please login again' ,
92
+ statusCode : 403
93
+ } ;
94
+ client . getProblems ( function ( e , problems ) {
95
+ assert . deepEqual ( e , expected ) ;
96
+ done ( ) ;
97
+ } ) ;
98
+ } ) ;
81
99
} ) ;
82
100
83
101
describe ( '#getProblems' , function ( ) {
@@ -232,6 +250,17 @@ describe('leetcode_client', function() {
232
250
done ( ) ;
233
251
} ) ;
234
252
} ) ;
253
+
254
+ it ( 'should fail if http error' , function ( done ) {
255
+ nock ( 'https://leetcode.com' )
256
+ . get ( '/problems/find-the-difference' )
257
+ . replyWithError ( 'unknown error!' ) ;
258
+
259
+ client . getProblem ( PROBLEM , function ( e , problem ) {
260
+ assert . equal ( e . message , 'unknown error!' ) ;
261
+ done ( ) ;
262
+ } ) ;
263
+ } ) ;
235
264
} ) ; // #getProblem
236
265
237
266
describe ( '#testProblem' , function ( ) {
@@ -298,6 +327,9 @@ describe('leetcode_client', function() {
298
327
. post ( '/problems/find-the-difference/submit/' )
299
328
. reply ( 200 , '{"submission_id": "id1"}' ) ;
300
329
330
+ nock ( 'https://leetcode.com' )
331
+ . get ( '/submissions/detail/id1/check/' )
332
+ . reply ( 200 , '{"state": "STARTED"}' ) ;
301
333
nock ( 'https://leetcode.com' )
302
334
. get ( '/submissions/detail/id1/check/' )
303
335
. reply ( 200 , '{"state": "SUCCESS"}' ) ;
@@ -319,6 +351,21 @@ describe('leetcode_client', function() {
319
351
done ( ) ;
320
352
} ) ;
321
353
} ) ;
354
+
355
+ it ( 'should fail if server error in check result' , function ( done ) {
356
+ nock ( 'https://leetcode.com' )
357
+ . post ( '/problems/find-the-difference/submit/' )
358
+ . reply ( 200 , '{"submission_id": "id1"}' ) ;
359
+
360
+ nock ( 'https://leetcode.com' )
361
+ . get ( '/submissions/detail/id1/check/' )
362
+ . replyWithError ( 'unknown error!' ) ;
363
+
364
+ client . submitProblem ( PROBLEM , function ( e , results ) {
365
+ assert . equal ( e . message , 'unknown error!' ) ;
366
+ done ( ) ;
367
+ } ) ;
368
+ } ) ;
322
369
} ) ; // #submitProblem
323
370
324
371
describe ( '#starProblem' , function ( ) {
@@ -345,6 +392,17 @@ describe('leetcode_client', function() {
345
392
done ( ) ;
346
393
} ) ;
347
394
} ) ;
395
+
396
+ it ( 'should star fail if http error' , function ( done ) {
397
+ nock ( 'https://leetcode.com' )
398
+ . post ( '/problems/favor/' )
399
+ . replyWithError ( 'unknown error!' ) ;
400
+
401
+ client . starProblem ( PROBLEM , true , function ( e , starred ) {
402
+ assert . equal ( e . message , 'unknown error!' ) ;
403
+ done ( ) ;
404
+ } ) ;
405
+ } ) ;
348
406
} ) ; // #starProblem
349
407
350
408
describe ( '#getSubmissions' , function ( ) {
@@ -383,23 +441,38 @@ describe('leetcode_client', function() {
383
441
done ( ) ;
384
442
} ) ;
385
443
} ) ;
444
+
445
+ it ( 'should fail if http error' , function ( done ) {
446
+ nock ( 'https://leetcode.com' )
447
+ . get ( '/problems/find-the-difference/submissions/' )
448
+ . replyWithError ( 'unknown error!' ) ;
449
+
450
+ client . getSubmissions ( PROBLEM , function ( e , submissions ) {
451
+ assert . equal ( e . message , 'unknown error!' ) ;
452
+ done ( ) ;
453
+ } ) ;
454
+ } ) ;
386
455
} ) ; // #getSubmissions
387
456
388
457
describe ( '#getSubmission' , function ( ) {
389
- it ( 'should ok' , function ( done ) {
390
- var submission = {
458
+ var SUBMISSION ;
459
+
460
+ beforeEach ( function ( ) {
461
+ SUBMISSION = {
391
462
id : '73790064' ,
392
463
lang : 'cpp' ,
393
464
runtime : '9 ms' ,
394
465
path : '/submissions/detail/73790064/' ,
395
466
state : 'Accepted'
396
467
} ;
468
+ } ) ;
397
469
470
+ it ( 'should ok' , function ( done ) {
398
471
nock ( 'https://leetcode.com' )
399
472
. get ( '/submissions/detail/73790064/' )
400
473
. replyWithFile ( 200 , './test/mock/two-sum.submission.73790064.html.20161006' ) ;
401
474
402
- client . getSubmission ( submission , function ( e , submission ) {
475
+ client . getSubmission ( SUBMISSION , function ( e , submission ) {
403
476
assert . equal ( e , null ) ;
404
477
assert . deepEqual ( submission . code ,
405
478
[
@@ -414,6 +487,29 @@ describe('leetcode_client', function() {
414
487
done ( ) ;
415
488
} ) ;
416
489
} ) ;
490
+
491
+ it ( 'should fail if http error' , function ( done ) {
492
+ nock ( 'https://leetcode.com' )
493
+ . get ( '/submissions/detail/73790064/' )
494
+ . replyWithError ( 'unknown error!' ) ;
495
+
496
+ client . getSubmission ( SUBMISSION , function ( e , submission ) {
497
+ assert . equal ( e . message , 'unknown error!' ) ;
498
+ done ( ) ;
499
+ } ) ;
500
+ } ) ;
501
+
502
+ it ( 'should fail if no matching submission' , function ( done ) {
503
+ nock ( 'https://leetcode.com' )
504
+ . get ( '/submissions/detail/73790064/' )
505
+ . replyWithFile ( 200 , './test/mock/locked.html.20161015' ) ;
506
+
507
+ client . getSubmission ( SUBMISSION , function ( e , submission ) {
508
+ assert . equal ( e , null ) ;
509
+ assert . equal ( submission . code , null ) ;
510
+ done ( ) ;
511
+ } ) ;
512
+ } ) ;
417
513
} ) ; // #getSubmission
418
514
419
515
describe ( '#login' , function ( ) {
@@ -443,5 +539,30 @@ describe('leetcode_client', function() {
443
539
done ( ) ;
444
540
} ) ;
445
541
} ) ;
542
+
543
+ it ( 'should fail if http error' , function ( done ) {
544
+ nock ( config . URL_LOGIN ) . get ( '/' ) . reply ( 200 , '' , {
545
+ 'Set-Cookie' : [
546
+ 'csrftoken=LOGIN_CSRF_TOKEN; Max-Age=31449600; Path=/; secure'
547
+ ]
548
+ } ) ;
549
+ nock ( config . URL_LOGIN ) . post ( '/' ) . replyWithError ( 'unknown error!' ) ;
550
+
551
+ var user = { } ;
552
+ client . login ( user , function ( e , user ) {
553
+ assert . equal ( e . message , 'unknown error!' ) ;
554
+ done ( ) ;
555
+ } ) ;
556
+ } ) ;
557
+
558
+ it ( 'should fail if http error, 2nd' , function ( done ) {
559
+ nock ( config . URL_LOGIN ) . get ( '/' ) . replyWithError ( 'unknown error!' ) ;
560
+
561
+ var user = { } ;
562
+ client . login ( user , function ( e , user ) {
563
+ assert . equal ( e . message , 'unknown error!' ) ;
564
+ done ( ) ;
565
+ } ) ;
566
+ } ) ;
446
567
} ) ; // #login
447
568
} ) ;
0 commit comments