@@ -837,23 +837,6 @@ function isPropertyAccessible(obj, key) {
837837}
838838
839839// src/utils/responseUtils.ts
840- var RESPONSE_STATUS_CODES_WITHOUT_BODY = /* @__PURE__ */ new Set ( [
841- 101 ,
842- 103 ,
843- 204 ,
844- 205 ,
845- 304
846- ] ) ;
847- var RESPONSE_STATUS_CODES_WITH_REDIRECT = /* @__PURE__ */ new Set ( [
848- 301 ,
849- 302 ,
850- 303 ,
851- 307 ,
852- 308
853- ] ) ;
854- function isResponseWithoutBody ( status ) {
855- return RESPONSE_STATUS_CODES_WITHOUT_BODY . has ( status ) ;
856- }
857840function createServerErrorResponse ( body ) {
858841 return new Response (
859842 JSON . stringify (
@@ -922,13 +905,17 @@ async function handleRequest(options) {
922905 } ) ;
923906 const requestAbortPromise = new DeferredPromise ( ) ;
924907 if ( options . request . signal ) {
925- options . request . signal . addEventListener (
926- "abort" ,
927- ( ) => {
928- requestAbortPromise . reject ( options . request . signal . reason ) ;
929- } ,
930- { once : true }
931- ) ;
908+ if ( options . request . signal . aborted ) {
909+ requestAbortPromise . reject ( options . request . signal . reason ) ;
910+ } else {
911+ options . request . signal . addEventListener (
912+ "abort" ,
913+ ( ) => {
914+ requestAbortPromise . reject ( options . request . signal . reason ) ;
915+ } ,
916+ { once : true }
917+ ) ;
918+ }
932919 }
933920 const result = await until ( async ( ) => {
934921 const requestListtenersPromise = emitAsync ( options . emitter , "request" , {
@@ -1147,6 +1134,12 @@ function hasConfigurableGlobal(propertyName) {
11471134 if ( typeof descriptor === "undefined" ) {
11481135 return false ;
11491136 }
1137+ if ( typeof descriptor . get === "function" && typeof descriptor . get ( ) === "undefined" ) {
1138+ return false ;
1139+ }
1140+ if ( typeof descriptor . get === "undefined" && descriptor . value == null ) {
1141+ return false ;
1142+ }
11501143 if ( typeof descriptor . set === "undefined" && ! descriptor . configurable ) {
11511144 console . error (
11521145 `[MSW] Failed to apply interceptor: the global \`${ propertyName } \` property is non-configurable. This is likely an issue with your environment. If you are using a framework, please open an issue about this in their repository.`
@@ -1156,6 +1149,83 @@ function hasConfigurableGlobal(propertyName) {
11561149 return true ;
11571150}
11581151
1152+ // src/utils/fetchUtils.ts
1153+ var FetchResponse = class _FetchResponse extends Response {
1154+ static {
1155+ /**
1156+ * Response status codes for responses that cannot have body.
1157+ * @see https://fetch.spec.whatwg.org/#statuses
1158+ */
1159+ this . STATUS_CODES_WITHOUT_BODY = [ 101 , 103 , 204 , 205 , 304 ] ;
1160+ }
1161+ static {
1162+ this . STATUS_CODES_WITH_REDIRECT = [ 301 , 302 , 303 , 307 , 308 ] ;
1163+ }
1164+ static isConfigurableStatusCode ( status ) {
1165+ return status >= 200 && status <= 599 ;
1166+ }
1167+ static isRedirectResponse ( status ) {
1168+ return _FetchResponse . STATUS_CODES_WITH_REDIRECT . includes ( status ) ;
1169+ }
1170+ /**
1171+ * Returns a boolean indicating whether the given response status
1172+ * code represents a response that can have a body.
1173+ */
1174+ static isResponseWithBody ( status ) {
1175+ return ! _FetchResponse . STATUS_CODES_WITHOUT_BODY . includes ( status ) ;
1176+ }
1177+ static setUrl ( url , response ) {
1178+ if ( ! url ) {
1179+ return ;
1180+ }
1181+ if ( response . url != "" ) {
1182+ return ;
1183+ }
1184+ Object . defineProperty ( response , "url" , {
1185+ value : url ,
1186+ enumerable : true ,
1187+ configurable : true ,
1188+ writable : false
1189+ } ) ;
1190+ }
1191+ /**
1192+ * Parses the given raw HTTP headers into a Fetch API `Headers` instance.
1193+ */
1194+ static parseRawHeaders ( rawHeaders ) {
1195+ const headers = new Headers ( ) ;
1196+ for ( let line = 0 ; line < rawHeaders . length ; line += 2 ) {
1197+ headers . append ( rawHeaders [ line ] , rawHeaders [ line + 1 ] ) ;
1198+ }
1199+ return headers ;
1200+ }
1201+ constructor ( body , init = { } ) {
1202+ const status = init . status ?? 200 ;
1203+ const safeStatus = _FetchResponse . isConfigurableStatusCode ( status ) ? status : 200 ;
1204+ const finalBody = _FetchResponse . isResponseWithBody ( status ) ? body : null ;
1205+ super ( finalBody , {
1206+ ...init ,
1207+ status : safeStatus
1208+ } ) ;
1209+ if ( status !== safeStatus ) {
1210+ const stateSymbol = Object . getOwnPropertySymbols ( this ) . find (
1211+ ( symbol ) => symbol . description === "state"
1212+ ) ;
1213+ if ( stateSymbol ) {
1214+ const state = Reflect . get ( this , stateSymbol ) ;
1215+ Reflect . set ( state , "status" , status ) ;
1216+ } else {
1217+ Object . defineProperty ( this , "status" , {
1218+ value : status ,
1219+ enumerable : true ,
1220+ configurable : true ,
1221+ writable : false
1222+ } ) ;
1223+ }
1224+ }
1225+ _FetchResponse . setUrl ( init . url , this ) ;
1226+ }
1227+ } ;
1228+
11591229// src/interceptors/fetch/index.ts
11601230var FetchInterceptor = class _FetchInterceptor extends Interceptor {
11611231 static {
@@ -1195,8 +1265,9 @@ var FetchInterceptor = class _FetchInterceptor extends Interceptor {
11951265 rawResponse
11961266 } ) ;
11971267 const decompressedStream = decompressResponse ( rawResponse ) ;
1198- const response = decompressedStream === null ? rawResponse : new Response ( decompressedStream , rawResponse ) ;
1199- if ( RESPONSE_STATUS_CODES_WITH_REDIRECT . has ( response . status ) ) {
1268+ const response = decompressedStream === null ? rawResponse : new FetchResponse ( decompressedStream , rawResponse ) ;
1269+ FetchResponse . setUrl ( request . url , response ) ;
1270+ if ( FetchResponse . isRedirectResponse ( response . status ) ) {
12001271 if ( request . redirect === "error" ) {
12011272 responsePromise . reject ( createNetworkError ( "unexpected redirect" ) ) ;
12021273 return ;
@@ -1213,12 +1284,6 @@ var FetchInterceptor = class _FetchInterceptor extends Interceptor {
12131284 return ;
12141285 }
12151286 }
1216- Object . defineProperty ( response , "url" , {
1217- writable : false ,
1218- enumerable : true ,
1219- configurable : false ,
1220- value : request . url
1221- } ) ;
12221287 if ( this . emitter . listenerCount ( "response" ) > 0 ) {
12231288 this . logger . info ( 'emitting the "response" event...' ) ;
12241289 await emitAsync ( this . emitter , "response" , {
@@ -1477,8 +1542,9 @@ function parseJson(data) {
14771542
14781543// src/interceptors/XMLHttpRequest/utils/createResponse.ts
14791544function createResponse ( request , body ) {
1480- const responseBodyOrNull = isResponseWithoutBody ( request . status ) ? null : body ;
1481- return new Response ( responseBodyOrNull , {
1545+ const responseBodyOrNull = FetchResponse . isResponseWithBody ( request . status ) ? body : null ;
1546+ return new FetchResponse ( responseBodyOrNull , {
1547+ url : request . responseURL ,
14821548 status : request . status ,
14831549 statusText : request . statusText ,
14841550 headers : createHeadersFromXMLHttpReqestHeaders (
0 commit comments