1- import 'whatwg -fetch' ;
1+ import 'isomorphic -fetch' ;
22import { ResponseHandler } from "../../src/ResponseHandler"
33import { GraphError } from '../../src/common'
44
55import * as mocha from 'mocha'
66
77import * as assert from 'assert' ;
88
9- const _200_SUPERAGENT_RES_BODY : any = { a : 1 } ;
10- const _200_SUPERAGENT_RES_INIT : ResponseInit = { status : 200 } ;
11- const _200_SUPERAGENT_RES : Response =
12- new Response ( _200_SUPERAGENT_RES_BODY , _200_SUPERAGENT_RES_INIT ) ;
9+ const _200_RES_BODY : any = { a : 1 } ;
10+ const _200_RES_INIT : ResponseInit = { status : 200 } ;
11+ const _200_RES : Response =
12+ new Response ( _200_RES_BODY , _200_RES_INIT ) ;
1313
14- const _500_SUPERAGENT_RES_BODY : any = {
14+ const _500_RES_BODY : any = {
1515 error : {
1616 "code" : "SearchEvents" ,
1717 "message" : "The parameter $search is not currently supported on the Events resource." ,
@@ -20,48 +20,40 @@ const _500_SUPERAGENT_RES_BODY: any = {
2020 "date" : "2016-11-17T18:37:45"
2121 }
2222 }
23- } ;
24- const _500_SUPERAGENT_RES_INIT : ResponseInit = { status : 500 } ;
25- const _500_SUPERAGENT_RES : Response =
26- new Response ( _200_SUPERAGENT_RES_BODY , _200_SUPERAGENT_RES_INIT ) ;
23+ } ;
2724
28- const error : Error = new Error ( ) ;
29- ( error as any ) . statusCode = 404 ;
25+ const _500_RES_INIT : ResponseInit = { status : 500 } ;
26+ const _500_RES : Response =
27+ new Response ( _500_RES_BODY , _500_RES_INIT ) ;
3028
3129describe ( '#handleResponse()' , function ( ) {
3230 it ( 'passes through response to callback' , function ( ) {
33- ResponseHandler . init ( _200_SUPERAGENT_RES , null , _200_SUPERAGENT_RES_BODY , ( err :GraphError , res ) => {
34- assert . equal ( res , _200_SUPERAGENT_RES . body ) ;
31+ ResponseHandler . init ( _200_RES , null , _200_RES_BODY , ( err :GraphError , res ) => {
32+ assert . equal ( res , _200_RES . body ) ;
3533 } ) ;
3634 } ) ;
3735
3836 it ( '200 response => err is null' , function ( ) {
39- ResponseHandler . init ( _200_SUPERAGENT_RES , null , _200_SUPERAGENT_RES_BODY , ( err :GraphError , res ) => {
37+ ResponseHandler . init ( _200_RES , null , _200_RES_BODY , ( err :GraphError , res ) => {
4038 assert . equal ( err , null ) ;
4139 } ) ;
4240 } ) ;
4341} ) ;
4442
4543describe ( '#ParseResponse()' , function ( ) {
4644 it ( 'extracts code and request-id from the JSON body of 500 errors' , function ( ) {
47- ResponseHandler . init ( _500_SUPERAGENT_RES , null , _500_SUPERAGENT_RES_BODY , ( err :GraphError , res ) => {
48- assert . equal ( err . code , _500_SUPERAGENT_RES_BODY . error . code ) ;
49- assert . equal ( err . requestId , _500_SUPERAGENT_RES_BODY . error . innerError [ "request-id" ] ) ;
45+ ResponseHandler . init ( _500_RES , null , _500_RES_BODY , ( err :GraphError , res ) => {
46+ assert . equal ( err . code , _500_RES_BODY . error . code ) ;
47+ assert . equal ( err . requestId , _500_RES_BODY . error . innerError [ "request-id" ] ) ;
5048 } ) ;
5149 } ) ;
5250} ) ;
5351
5452describe ( '#ParseError()' , function ( ) {
55- it ( '500 error => res param in callback is null' , function ( ) {
53+ it ( 'res param in callback is null' , function ( ) {
5654 ResponseHandler . init ( null , null , null , ( err :GraphError , res ) => {
5755 assert . equal ( res , null ) ;
56+ assert . equal ( err . statusCode , - 1 ) ;
5857 } ) ;
5958 } ) ;
60-
61- it ( 'parses a 404 superagent error' , function ( ) {
62- let graphErr = ResponseHandler . ParseError ( error ) ;
63- ResponseHandler . init ( null , null , null , ( err :GraphError , res ) => {
64- assert . equal ( graphErr . statusCode , 404 ) ;
65- } ) ;
66- } )
6759} ) ;
0 commit comments