test(undici): add interceptor tests for undici#180
test(undici): add interceptor tests for undici#180chentsulin wants to merge 2 commits intomswjs:mainfrom
Conversation
| @@ -0,0 +1,190 @@ | |||
| /** | |||
There was a problem hiding this comment.
We can put these test suites into test/integration folder. It's there to test this library's integration with other libraries (like got), as those may have certain corner-cases to account for.
There was a problem hiding this comment.
As I see it, undici is a replacement for the old http and https module, and many libraries include got may use it instead of http in the future. So, I suppose this test should be placed nearby the tests of the http interceptor.
There was a problem hiding this comment.
BTW, I just realized undici.fetch only works on Node 16+.
There was a problem hiding this comment.
That's a great observation. We don't have any official support for Node 16 in our tooling at the moment. I believe the latest supported Node version is 14.
There was a problem hiding this comment.
@chentsulin, well, undici is still a third-party library. Until it's a part of the Node.js standard library, we should treat it as third-party integration.
There was a problem hiding this comment.
Fair enough. We can rethink about that by then.
|
I've rebased your feature branch, as well as adjusted it to the latest test structure refactoring (#182). Your newly added undici tests are now under |
|
Thank you for adding these tests! The support for unidici will roughly follow these steps:
If you have time, feel free to tackle the first step. Gathering knowledge often takes the most time, and I could definitely use your expertise in that. Thank you! |
|
In my earlier research, undici has its own mocking mechanism: import { MockAgent, setGlobalDispatcher, } from 'undici'
const mockAgent = new MockAgent();
setGlobalDispatcher(mockAgent);
// Provide the base url to the request
const mockPool = mockAgent.get('http://localhost:3000');
// intercept the request
mockPool.intercept({
path: '/bank-transfer',
method: 'POST',
headers: {
'X-TOKEN-SECRET': 'SuperSecretToken',
},
body: JSON.stringify({
recepient: '1234567890',
ammount: '100'
})
}).reply(200, {
message: 'transaction processed'
})We should take advantage of this setGlobalDispatcher stuff to implement the interceptor. |
|
@kettanaito, That's what I learned:
|
|
@chentsulin Thanks for sharing your implementation, it was super helpful. I have been playing with it and seeing an issue where undici expects @types/node to be at least 14.18 (this has the Blob type from buffer) in order to import the Dispatcher.DispatchHandlers type for the resolver. However, updating this seems to cause issues with other intercepters, such as XML and http.get. Did you run into anything like that? How did you resolve?
|
|
Thanks for preparing a reference implementation, @chentsulin! I'm not sure to which extend we can reuse it, as with interceptors we're aiming at low-level request interception. Relying on I'd love to start this feature from research. We should first understand what kind of requests does undici make (it has to make a I don't think I'll have time to work on this in the foreseeable future, so anybody is welcome to grab the research and implementation if they find this interesting. |
|
We are not going to support Undici directly. Instead, we will support global fetch in Node.js, which is powered by Undicit and is already available since Node 17. |
See #159.