@@ -78,6 +78,24 @@ describe('leetcode_client', function() {
7878 done ( ) ;
7979 } ) ;
8080 } ) ;
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+ } ) ;
8199 } ) ;
82100
83101 describe ( '#getProblems' , function ( ) {
@@ -232,6 +250,17 @@ describe('leetcode_client', function() {
232250 done ( ) ;
233251 } ) ;
234252 } ) ;
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+ } ) ;
235264 } ) ; // #getProblem
236265
237266 describe ( '#testProblem' , function ( ) {
@@ -298,6 +327,9 @@ describe('leetcode_client', function() {
298327 . post ( '/problems/find-the-difference/submit/' )
299328 . reply ( 200 , '{"submission_id": "id1"}' ) ;
300329
330+ nock ( 'https://leetcode.com' )
331+ . get ( '/submissions/detail/id1/check/' )
332+ . reply ( 200 , '{"state": "STARTED"}' ) ;
301333 nock ( 'https://leetcode.com' )
302334 . get ( '/submissions/detail/id1/check/' )
303335 . reply ( 200 , '{"state": "SUCCESS"}' ) ;
@@ -319,6 +351,21 @@ describe('leetcode_client', function() {
319351 done ( ) ;
320352 } ) ;
321353 } ) ;
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+ } ) ;
322369 } ) ; // #submitProblem
323370
324371 describe ( '#starProblem' , function ( ) {
@@ -345,6 +392,17 @@ describe('leetcode_client', function() {
345392 done ( ) ;
346393 } ) ;
347394 } ) ;
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+ } ) ;
348406 } ) ; // #starProblem
349407
350408 describe ( '#getSubmissions' , function ( ) {
@@ -383,23 +441,38 @@ describe('leetcode_client', function() {
383441 done ( ) ;
384442 } ) ;
385443 } ) ;
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+ } ) ;
386455 } ) ; // #getSubmissions
387456
388457 describe ( '#getSubmission' , function ( ) {
389- it ( 'should ok' , function ( done ) {
390- var submission = {
458+ var SUBMISSION ;
459+
460+ beforeEach ( function ( ) {
461+ SUBMISSION = {
391462 id : '73790064' ,
392463 lang : 'cpp' ,
393464 runtime : '9 ms' ,
394465 path : '/submissions/detail/73790064/' ,
395466 state : 'Accepted'
396467 } ;
468+ } ) ;
397469
470+ it ( 'should ok' , function ( done ) {
398471 nock ( 'https://leetcode.com' )
399472 . get ( '/submissions/detail/73790064/' )
400473 . replyWithFile ( 200 , './test/mock/two-sum.submission.73790064.html.20161006' ) ;
401474
402- client . getSubmission ( submission , function ( e , submission ) {
475+ client . getSubmission ( SUBMISSION , function ( e , submission ) {
403476 assert . equal ( e , null ) ;
404477 assert . deepEqual ( submission . code ,
405478 [
@@ -414,6 +487,29 @@ describe('leetcode_client', function() {
414487 done ( ) ;
415488 } ) ;
416489 } ) ;
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+ } ) ;
417513 } ) ; // #getSubmission
418514
419515 describe ( '#login' , function ( ) {
@@ -443,5 +539,30 @@ describe('leetcode_client', function() {
443539 done ( ) ;
444540 } ) ;
445541 } ) ;
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+ } ) ;
446567 } ) ; // #login
447568} ) ;
0 commit comments