mswjs / interceptors

Low-level network interception library.
https://npm.im/@mswjs/interceptors
MIT License
523 stars 118 forks source link

feat: implement a `RequestController` class #595

Open kettanaito opened 3 days ago

kettanaito commented 3 days ago

[!WARNING] This is a breaking change. The way you handle requests changes from request.respondWith() to controller.respondWith().

This change also lays foundation for supporting controller.abort() (or even better—use AbortSignal) to abort any intercepted request. This would be awesome to ship in the future.

Roadmap

kettanaito commented 2 days ago

Suspicious test

stderr | modules/XMLHttpRequest/compliance/xhr-middleware-exception.test.ts > handles exceptions as instructed in "unhandledException" listener (request error)
Error: Error: connect ECONNREFUSED 127.0.0.1:80
    at Object.dispatchError (/Users/kettanaito/Projects/mswjs/interceptors/node_modules/.pnpm/jsdom@16.7.0/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:63:19)
    at EventEmitter.<anonymous> (/Users/kettanaito/Projects/mswjs/interceptors/node_modules/.pnpm/jsdom@16.7.0/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:655:18)
    at EventEmitter.emit (node:events:525:35)
    at Request.<anonymous> (/Users/kettanaito/Projects/mswjs/interceptors/node_modules/.pnpm/jsdom@16.7.0/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:401:14)
    at Request.emit (node:events:513:28)
    at ClientRequest.<anonymous> (/Users/kettanaito/Projects/mswjs/interceptors/node_modules/.pnpm/jsdom@16.7.0/node_modules/jsdom/lib/jsdom/living/helpers/http-request.js:121:14)
    at ClientRequest.emit (node:events:513:28)
    at Socket.socketErrorListener (node:_http_client:502:9)
    at Socket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8) undefined

   · sets "credentials" to "omit" on isomorphic request when "withCredentials" is not set
   · sets "credentials" to "omit" on isomorphic request when "withCredentials" is false
   · responds with an ArrayBuffer when "responseType" equals "arraybuffer"
Cancelling test run. Press CTRL+c again to exit forcefully.

Not sure if this is intended. The test passes but the error looks weird. Need to check if it's intentional, and fix it if it's not.