Skip to content

test(undici): add interceptor tests for undici#180

Closed
chentsulin wants to merge 2 commits intomswjs:mainfrom
chentsulin:undici
Closed

test(undici): add interceptor tests for undici#180
chentsulin wants to merge 2 commits intomswjs:mainfrom
chentsulin:undici

Conversation

@chentsulin
Copy link
Copy Markdown

See #159.

@chentsulin chentsulin mentioned this pull request Nov 10, 2021
@@ -0,0 +1,190 @@
/**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I just realized undici.fetch only works on Node 16+.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. We can rethink about that by then.

@kettanaito
Copy link
Copy Markdown
Member

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 test/third-party/undici.

@kettanaito
Copy link
Copy Markdown
Member

Thank you for adding these tests!

The support for unidici will roughly follow these steps:

  1. Research how undici performs requests internally. See what modules it utilizes, what modules can be extended to support interception.
  2. Discuss the findings, assess the support strategy.
  3. If agreed, create a new interceptors/undici interceptor with the barebone implementation to cover the tests you've added.
  4. Ensure sufficient test coverage, merge and release the change.

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!

@chentsulin
Copy link
Copy Markdown
Author

chentsulin commented Nov 11, 2021

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.

@chentsulin
Copy link
Copy Markdown
Author

chentsulin commented Nov 12, 2021

@kettanaito,
I don't have enough knowledge to experiment and finish all implementation details, but I did a naive and maybe buggy implementation for you to refer: a7e21d1

That's what I learned:

@jtlopez18
Copy link
Copy Markdown

@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?

@kettanaito, I don't have enough knowledge to experiment and finish all implementation details, but I did a naive and maybe buggy implementation for you to refer: a7e21d1

@kettanaito
Copy link
Copy Markdown
Member

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 MockAgent is a high-level thing, meaning we're going deeper into implementation specifics rather than relying on Node.js primitives.

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 Socket connection at least, there are no other ways to request things in Node.js) and, ideally, model our interceptor around that.

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.

@kettanaito
Copy link
Copy Markdown
Member

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.

@kettanaito kettanaito closed this Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants