@@ -2926,3 +2926,145 @@ test('inflightRequest accounting stable with firstChain', function(t) {
2926
2926
CLIENT . get ( '/foobar' , getDone ) ;
2927
2927
CLIENT . get ( '/foobar' , getDone ) ;
2928
2928
} ) ;
2929
+
2930
+ test ( 'async prerouting chain with error' , function ( t ) {
2931
+ SERVER . pre ( async function ( req , res ) {
2932
+ await helper . sleep ( 10 ) ;
2933
+ throw new RestError ( { statusCode : 400 , restCode : 'BadRequest' } , 'bum' ) ;
2934
+ } ) ;
2935
+
2936
+ SERVER . get ( '/hello/:name' , function tester ( req , res , next ) {
2937
+ res . send ( req . params . name ) ;
2938
+ next ( ) ;
2939
+ } ) ;
2940
+
2941
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
2942
+ t . ok ( err ) ;
2943
+ t . equal ( res . statusCode , 400 ) ;
2944
+ t . end ( ) ;
2945
+ } ) ;
2946
+ } ) ;
2947
+
2948
+ test ( 'async prerouting chain with empty rejection' , function ( t ) {
2949
+ SERVER . pre ( async function ( req , res ) {
2950
+ await helper . sleep ( 10 ) ;
2951
+ return Promise . reject ( ) ;
2952
+ } ) ;
2953
+
2954
+ SERVER . get ( '/hello/:name' , function tester ( req , res , next ) {
2955
+ res . send ( req . params . name ) ;
2956
+ next ( ) ;
2957
+ } ) ;
2958
+
2959
+ SERVER . on ( 'Async' , function ( req , res , err , callback ) {
2960
+ t . equal ( err . jse_info . cause , undefined ) ;
2961
+ t . equal ( err . jse_info . method , 'GET' ) ;
2962
+ t . equal ( err . jse_info . path , '/hello/mark' ) ;
2963
+ callback ( ) ;
2964
+ } ) ;
2965
+
2966
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
2967
+ t . ok ( err ) ;
2968
+ t . equal ( res . statusCode , 500 ) ;
2969
+ t . end ( ) ;
2970
+ } ) ;
2971
+ } ) ;
2972
+
2973
+ test ( 'async use chain with error' , function ( t ) {
2974
+ SERVER . use ( async function ( req , res ) {
2975
+ await helper . sleep ( 10 ) ;
2976
+ throw new RestError ( { statusCode : 400 , restCode : 'BadRequest' } , 'bum' ) ;
2977
+ } ) ;
2978
+
2979
+ SERVER . get ( '/hello/:name' , function tester ( req , res , next ) {
2980
+ res . send ( req . params . name ) ;
2981
+ next ( ) ;
2982
+ } ) ;
2983
+
2984
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
2985
+ t . ok ( err ) ;
2986
+ t . equal ( res . statusCode , 400 ) ;
2987
+ t . end ( ) ;
2988
+ } ) ;
2989
+ } ) ;
2990
+
2991
+ test ( 'async handler with error' , function ( t ) {
2992
+ SERVER . get ( '/hello/:name' , async function tester ( req , res ) {
2993
+ await helper . sleep ( 10 ) ;
2994
+ throw new RestError ( { statusCode : 400 , restCode : 'BadRequest' } , 'bum' ) ;
2995
+ } ) ;
2996
+
2997
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
2998
+ t . ok ( err ) ;
2999
+ t . equal ( res . statusCode , 400 ) ;
3000
+ t . end ( ) ;
3001
+ } ) ;
3002
+ } ) ;
3003
+
3004
+ test ( 'async handler with error after send succeeds' , function ( t ) {
3005
+ SERVER . get ( '/hello/:name' , async function tester ( req , res ) {
3006
+ await helper . sleep ( 10 ) ;
3007
+ res . send ( req . params . name ) ;
3008
+ throw new RestError ( { statusCode : 400 , restCode : 'BadRequest' } , 'bum' ) ;
3009
+ } ) ;
3010
+
3011
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
3012
+ t . ok ( ! err ) ;
3013
+ t . equal ( res . statusCode , 200 ) ;
3014
+ t . end ( ) ;
3015
+ } ) ;
3016
+ } ) ;
3017
+
3018
+ test ( 'async handler with error after send succeeds' , function ( t ) {
3019
+ SERVER . get ( '/hello/:name' , async function tester ( req , res ) {
3020
+ res . send ( req . params . name ) ;
3021
+ await helper . sleep ( 20 ) ;
3022
+ throw new RestError ( { statusCode : 400 , restCode : 'BadRequest' } , 'bum' ) ;
3023
+ } ) ;
3024
+
3025
+ SERVER . on ( 'after' , function ( req , res , route , error ) {
3026
+ t . ok ( error ) ;
3027
+ t . end ( ) ;
3028
+ } ) ;
3029
+
3030
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
3031
+ t . ok ( ! err ) ;
3032
+ t . equal ( res . statusCode , 200 ) ;
3033
+ } ) ;
3034
+ } ) ;
3035
+
3036
+ test ( 'async handler without next' , function ( t ) {
3037
+ SERVER . get ( '/hello/:name' , async function tester ( req , res ) {
3038
+ await helper . sleep ( 10 ) ;
3039
+ res . send ( req . params . name ) ;
3040
+ } ) ;
3041
+
3042
+ SERVER . on ( 'after' , function ( req , res , route , error ) {
3043
+ t . ok ( ! error ) ;
3044
+ t . equal ( res . statusCode , 200 ) ;
3045
+ t . end ( ) ;
3046
+ } ) ;
3047
+
3048
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
3049
+ t . ok ( ! err ) ;
3050
+ t . equal ( res . statusCode , 200 ) ;
3051
+ } ) ;
3052
+ } ) ;
3053
+
3054
+ test ( 'async handler resolved with string should re-route' , function ( t ) {
3055
+ SERVER . get ( '/hello/:name' , async function tester ( req , res ) {
3056
+ await helper . sleep ( 10 ) ;
3057
+ return 'getredirected' ;
3058
+ } ) ;
3059
+
3060
+ SERVER . get ( '/redirected' , async function tester ( req , res ) {
3061
+ res . send ( req . params . name ) ;
3062
+ } ) ;
3063
+
3064
+ CLIENT . get ( '/hello/mark' , function ( err , _ , res ) {
3065
+ t . ok ( ! err ) ;
3066
+ t . equal ( res . statusCode , 200 ) ;
3067
+ t . equal ( res . body , '"mark"' ) ;
3068
+ t . end ( ) ;
3069
+ } ) ;
3070
+ } ) ;
0 commit comments