@@ -15,9 +15,6 @@ import chaiString from "chai-string";
15
15
import FormData from "form-data" ;
16
16
import FormDataNode from "formdata-node" ;
17
17
import delay from "delay" ;
18
- import AbortControllerMysticatea from "abort-controller" ;
19
- import abortControllerPolyfill from "abortcontroller-polyfill/dist/abortcontroller.js" ;
20
- const AbortControllerPolyfill = abortControllerPolyfill . AbortController ;
21
18
22
19
// Test subjects
23
20
import fetch , { Headers , Request , Response } from "@remix-run/web-fetch" ;
@@ -985,246 +982,6 @@ describe("node-fetch", () => {
985
982
} ) ;
986
983
} ) ;
987
984
988
- const testAbortController = (
989
- name ,
990
- buildAbortController ,
991
- moreTests = null
992
- ) => {
993
- describe ( `AbortController (${ name } )` , ( ) => {
994
- let controller ;
995
-
996
- beforeEach ( ( ) => {
997
- controller = buildAbortController ( ) ;
998
- } ) ;
999
-
1000
- it ( "should support request cancellation with signal" , ( ) => {
1001
- const fetches = [
1002
- fetch ( `${ base } timeout` , {
1003
- method : "POST" ,
1004
- signal : controller . signal ,
1005
- headers : {
1006
- "Content-Type" : "application/json" ,
1007
- body : JSON . stringify ( { hello : "world" } ) ,
1008
- } ,
1009
- } ) ,
1010
- ] ;
1011
- setTimeout ( ( ) => {
1012
- controller . abort ( ) ;
1013
- } , 100 ) ;
1014
-
1015
- return Promise . all (
1016
- fetches . map ( ( fetched ) =>
1017
- expect ( fetched )
1018
- . to . eventually . be . rejected . and . be . an . instanceOf ( Error )
1019
- . and . include ( {
1020
- type : "aborted" ,
1021
- name : "AbortError" ,
1022
- } )
1023
- )
1024
- ) ;
1025
- } ) ;
1026
-
1027
- it ( "should support multiple request cancellation with signal" , ( ) => {
1028
- const fetches = [
1029
- fetch ( `${ base } timeout` , { signal : controller . signal } ) ,
1030
- fetch ( `${ base } timeout` , {
1031
- method : "POST" ,
1032
- signal : controller . signal ,
1033
- headers : {
1034
- "Content-Type" : "application/json" ,
1035
- body : JSON . stringify ( { hello : "world" } ) ,
1036
- } ,
1037
- } ) ,
1038
- ] ;
1039
- setTimeout ( ( ) => {
1040
- controller . abort ( ) ;
1041
- } , 100 ) ;
1042
-
1043
- return Promise . all (
1044
- fetches . map ( ( fetched ) =>
1045
- expect ( fetched )
1046
- . to . eventually . be . rejected . and . be . an . instanceOf ( Error )
1047
- . and . include ( {
1048
- type : "aborted" ,
1049
- name : "AbortError" ,
1050
- } )
1051
- )
1052
- ) ;
1053
- } ) ;
1054
-
1055
- it ( "should reject immediately if signal has already been aborted" , ( ) => {
1056
- const url = `${ base } timeout` ;
1057
- const options = {
1058
- signal : controller . signal ,
1059
- } ;
1060
- controller . abort ( ) ;
1061
- const fetched = fetch ( url , options ) ;
1062
- return expect ( fetched )
1063
- . to . eventually . be . rejected . and . be . an . instanceOf ( Error )
1064
- . and . include ( {
1065
- type : "aborted" ,
1066
- name : "AbortError" ,
1067
- } ) ;
1068
- } ) ;
1069
-
1070
- it ( "should allow redirects to be aborted" , ( ) => {
1071
- const request = new Request ( `${ base } redirect/slow` , {
1072
- signal : controller . signal ,
1073
- } ) ;
1074
- setTimeout ( ( ) => {
1075
- controller . abort ( ) ;
1076
- } , 20 ) ;
1077
- return expect ( fetch ( request ) )
1078
- . to . be . eventually . rejected . and . be . an . instanceOf ( Error )
1079
- . and . have . property ( "name" , "AbortError" ) ;
1080
- } ) ;
1081
-
1082
- it ( "should allow redirected response body to be aborted" , ( ) => {
1083
- const request = new Request ( `${ base } redirect/slow-stream` , {
1084
- signal : controller . signal ,
1085
- } ) ;
1086
- return expect (
1087
- fetch ( request ) . then ( ( res ) => {
1088
- expect ( res . headers . get ( "content-type" ) ) . to . equal ( "text/plain" ) ;
1089
- const result = res . text ( ) ;
1090
- controller . abort ( ) ;
1091
- return result ;
1092
- } )
1093
- )
1094
- . to . be . eventually . rejected . and . be . an . instanceOf ( Error )
1095
- . and . have . property ( "name" , "AbortError" ) ;
1096
- } ) ;
1097
-
1098
- it ( "should reject response body with AbortError when aborted before stream has been read completely" , ( ) => {
1099
- return expect (
1100
- fetch ( `${ base } slow` , { signal : controller . signal } )
1101
- ) . to . eventually . be . fulfilled . then ( ( res ) => {
1102
- const promise = res . text ( ) ;
1103
- controller . abort ( ) ;
1104
- return expect ( promise )
1105
- . to . eventually . be . rejected . and . be . an . instanceof ( Error )
1106
- . and . have . property ( "name" , "AbortError" ) ;
1107
- } ) ;
1108
- } ) ;
1109
-
1110
- it ( "should reject response body methods immediately with AbortError when aborted before stream is disturbed" , ( ) => {
1111
- return expect (
1112
- fetch ( `${ base } slow` , { signal : controller . signal } )
1113
- ) . to . eventually . be . fulfilled . then ( ( res ) => {
1114
- controller . abort ( ) ;
1115
- return expect ( res . text ( ) )
1116
- . to . eventually . be . rejected . and . be . an . instanceof ( Error )
1117
- . and . have . property ( "name" , "AbortError" ) ;
1118
- } ) ;
1119
- } ) ;
1120
-
1121
- it ( "should emit error event to response body with an AbortError when aborted before underlying stream is closed" , ( done ) => {
1122
- expect (
1123
- fetch ( `${ base } slow` , { signal : controller . signal } )
1124
- ) . to . eventually . be . fulfilled . then ( ( res ) => {
1125
- const collect = async ( ) => {
1126
- try {
1127
- return await res . arrayBuffer ( ) ;
1128
- } catch ( error ) {
1129
- expect ( error )
1130
- . to . be . an . instanceof ( Error )
1131
- . and . have . property ( "name" , "AbortError" ) ;
1132
- done ( ) ;
1133
- }
1134
- } ;
1135
-
1136
- collect ( ) ;
1137
- controller . abort ( ) ;
1138
- } ) ;
1139
- } ) ;
1140
-
1141
- it ( "should cancel request body of type Stream with AbortError when aborted" , ( ) => {
1142
- const body = new stream . Readable ( { objectMode : true } ) ;
1143
- body . _read = ( ) => { } ;
1144
- const promise = fetch ( `${ base } slow` , {
1145
- signal : controller . signal ,
1146
- body,
1147
- method : "POST" ,
1148
- } ) ;
1149
-
1150
- const result = Promise . all ( [
1151
- new Promise ( ( resolve , reject ) => {
1152
- body . on ( "error" , ( error ) => {
1153
- try {
1154
- expect ( error )
1155
- . to . be . an . instanceof ( Error )
1156
- . and . have . property ( "name" , "AbortError" ) ;
1157
- resolve ( ) ;
1158
- } catch ( error_ ) {
1159
- reject ( error_ ) ;
1160
- }
1161
- } ) ;
1162
- } ) ,
1163
- expect ( promise )
1164
- . to . eventually . be . rejected . and . be . an . instanceof ( Error )
1165
- . and . have . property ( "name" , "AbortError" ) ,
1166
- ] ) ;
1167
-
1168
- controller . abort ( ) ;
1169
-
1170
- return result ;
1171
- } ) ;
1172
-
1173
- if ( moreTests ) {
1174
- moreTests ( ) ;
1175
- }
1176
- } ) ;
1177
- } ;
1178
-
1179
- testAbortController (
1180
- "polyfill" ,
1181
- ( ) => new AbortControllerPolyfill ( ) ,
1182
- ( ) => {
1183
- it ( "should remove internal AbortSignal event listener after request is aborted" , ( ) => {
1184
- const controller = new AbortControllerPolyfill ( ) ;
1185
- const { signal } = controller ;
1186
-
1187
- setTimeout ( ( ) => {
1188
- controller . abort ( ) ;
1189
- } , 20 ) ;
1190
-
1191
- return expect ( fetch ( `${ base } timeout` , { signal } ) )
1192
- . to . eventually . be . rejected . and . be . an . instanceof ( Error )
1193
- . and . have . property ( "name" , "AbortError" )
1194
- . then ( ( ) => {
1195
- return expect ( signal . listeners . abort . length ) . to . equal ( 0 ) ;
1196
- } ) ;
1197
- } ) ;
1198
-
1199
- it ( "should remove internal AbortSignal event listener after request and response complete without aborting" , ( ) => {
1200
- const controller = new AbortControllerPolyfill ( ) ;
1201
- const { signal } = controller ;
1202
- const fetchHtml = fetch ( `${ base } html` , { signal } ) . then ( ( res ) =>
1203
- res . text ( )
1204
- ) ;
1205
- const fetchResponseError = fetch ( `${ base } error/reset` , { signal } ) ;
1206
- const fetchRedirect = fetch ( `${ base } redirect/301` , { signal } ) . then (
1207
- ( res ) => res . json ( )
1208
- ) ;
1209
- return Promise . all ( [
1210
- expect ( fetchHtml ) . to . eventually . be . fulfilled . and . equal (
1211
- "<html></html>"
1212
- ) ,
1213
- expect ( fetchResponseError ) . to . be . eventually . rejected ,
1214
- expect ( fetchRedirect ) . to . eventually . be . fulfilled ,
1215
- ] ) . then ( ( ) => {
1216
- expect ( signal . listeners . abort . length ) . to . equal ( 0 ) ;
1217
- } ) ;
1218
- } ) ;
1219
- }
1220
- ) ;
1221
-
1222
- testAbortController ( "mysticatea" , ( ) => new AbortControllerMysticatea ( ) ) ;
1223
-
1224
- if ( process . version > "v15" ) {
1225
- testAbortController ( "native" , ( ) => new AbortController ( ) ) ;
1226
- }
1227
-
1228
985
it ( "should throw a TypeError if a signal is not of type AbortSignal or EventTarget" , ( ) => {
1229
986
return Promise . all ( [
1230
987
expect ( fetch ( `${ base } inspect` , { signal : { } } ) )
0 commit comments