@@ -11,6 +11,7 @@ function makeOpts(url) {
1111 if ( core . isLogin ( ) ) {
1212 var user = core . getUser ( ) ;
1313 opts . headers [ 'Cookie' ] = 'PHPSESSID=' + user . session_id + ';csrftoken=' + user . session_csrf + ';' ;
14+ //opts.headers['X-CSRFToken'] = user.session_csrf;
1415 }
1516 return opts ;
1617}
@@ -102,4 +103,54 @@ leetcode_client.login = function(user, cb) {
102103 } ) ;
103104} ;
104105
106+ function verify_test ( opts , jobs , results , cb ) {
107+ if ( jobs . length == 0 )
108+ return cb ( null , results ) ;
109+
110+ opts . url = config . VERIFY_URL . replace ( '$id' , jobs [ 0 ] . id ) ;
111+ request . get ( opts , function ( e , resp , body ) {
112+ if ( e ) return cb ( e ) ;
113+ if ( resp . statusCode != 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
114+
115+ var result = JSON . parse ( body ) ;
116+ if ( result . state == 'SUCCESS' ) {
117+ result . name = jobs [ 0 ] . name ;
118+ results . push ( result ) ;
119+ jobs . shift ( ) ;
120+ }
121+
122+ setImmediate ( verify_test , opts , jobs , results , cb ) ;
123+ } ) ;
124+ }
125+
126+ leetcode_client . testProblem = function ( problem , cb ) {
127+ var opts = makeOpts ( ) ;
128+ opts . url = config . TEST_URL . replace ( '$key' , problem . key ) ;
129+ opts . headers [ 'Origin' ] = config . BASE_URL ;
130+ opts . headers [ 'Referer' ] = problem . link ;
131+ opts . headers [ 'X-Requested-With' ] = 'XMLHttpRequest' ;
132+ opts . json = true ;
133+ opts . body = {
134+ data_input : problem . testcase ,
135+ lang : h . fileLang ( problem . file ) ,
136+ question_id : parseInt ( problem . id ) ,
137+ test_mode : false ,
138+ typed_code : h . fileData ( problem . file )
139+ } ;
140+
141+ request . post ( opts , function ( e , resp , body ) {
142+ if ( e ) return cb ( e ) ;
143+ if ( resp . statusCode != 200 ) return cb ( 'HTTP failed:' + resp . statusCode ) ;
144+
145+ opts . json = false ;
146+ opts . body = null ;
147+
148+ var jobs = [
149+ { name : 'Your' , id : body . interpret_id } ,
150+ { name : 'Expected' , id : body . interpret_expected_id }
151+ ] ;
152+ verify_test ( opts , jobs , [ ] , cb ) ;
153+ } ) ;
154+ } ;
155+
105156module . exports = leetcode_client ;
0 commit comments