Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trailing slash on origin causes interceptors to not match #3649

Closed
tswaters opened this issue Sep 26, 2024 · 3 comments · Fixed by #3655
Closed

Trailing slash on origin causes interceptors to not match #3649

tswaters opened this issue Sep 26, 2024 · 3 comments · Fixed by #3655
Labels
bug Something isn't working mocks Issues/PRs related to mocks feature

Comments

@tswaters
Copy link

Bug Description

Let me first say, I'm not sure if this is a bug exactly, but it did cause me a headache for an hour or two while I was trying to figure it out.

Reproducible By

const { MockAgent } = require('undici')

const undiciGlobalDispatcherSymbol = Symbol.for('undici.globalDispatcher.1')
const undiciGlobalDispatcher = global[undiciGlobalDispatcherSymbol]
if (!undiciGlobalDispatcher) throw new Error('Could not find the global Undici dispatcher')

const mockAgent = new MockAgent();
mockAgent.disableNetConnect();
global[undiciGlobalDispatcherSymbol] = mockAgent;

const pool = mockAgent.get("https://localhost");

pool.intercept({ path: "/api/some-path" }).reply(200, { ok: true });

const res = await fetch(new URL("/api/some-path", "https://localhost"));
const data = await res.json();

console.log(data);

// restore
global[undiciGlobalDispatcherSymbol] = undiciGlobalDispatcher;

Expected Behavior

Some kind of error or warning saying it's unexpected behaviour, or maybe just removing it for the user?

Logs & Screenshots

The above code will output:

TypeError: fetch failed
    at node:internal/deps/undici/undici:13178:13
    at async main (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/index.js:19:17) {
  [cause]: MockNotMatchedError: Mock dispatch not matched for path '/api/some-path': subsequent request to origin https://localhost was not allowed (net.connect disabled)
      at MockPool.dispatch (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/mock/mock-utils.js:313:19)
      at [dispatch] (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/dispatcher/agent.js:105:23)
      at Intercept (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/interceptor/redirect-interceptor.js:11:16)
      at [Intercepted Dispatch] (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/dispatcher/dispatcher-base.js:156:12)
      at Agent.dispatch (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/dispatcher/dispatcher-base.js:177:40)
      at MockAgent.dispatch (/home/local.epicure.ca/tyler.waters/code/GITHUB/undici-global-node-fetch-interceptor/node_modules/undici/lib/mock/mock-agent.js:55:25)
      at node:internal/deps/undici/undici:10827:55
      at new Promise (<anonymous>)
      at dispatch (node:internal/deps/undici/undici:10827:16)
      at httpNetworkFetch (node:internal/deps/undici/undici:10724:73) {
    code: 'UND_MOCK_ERR_MOCK_NOT_MATCHED'
  }
}

Removing the trailing slash results in:

{ ok: true }

Environment

+ uname -srvmo
Linux 6.10.10-100.fc39.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Sep 12 16:02:41 UTC 2024 x86_64 GNU/Linux
+ npm -v
10.8.1
+ node -p process.versions
{
  node: '20.16.0',
  acorn: '8.11.3',
  ada: '2.8.0',
  ares: '1.31.0',
  base64: '0.5.2',
  brotli: '1.1.0',
  cjs_module_lexer: '1.2.2',
  cldr: '45.0',
  icu: '75.1',
  llhttp: '8.1.2',
  modules: '115',
  napi: '9',
  nghttp2: '1.61.0',
  nghttp3: '0.7.0',
  ngtcp2: '1.1.0',
  openssl: '3.0.13+quic',
  simdutf: '5.2.8',
  tz: '2024a',
  undici: '6.19.2',
  unicode: '15.1',
  uv: '1.46.0',
  uvwasi: '0.0.21',
  v8: '11.3.244.8-node.23',
  zlib: '1.3.0.1-motley-209717d'
}
@tswaters tswaters added the bug Something isn't working label Sep 26, 2024
@metcoder95 metcoder95 added the mocks Issues/PRs related to mocks feature label Sep 30, 2024
@metcoder95
Copy link
Member

Thanks for the report! Would you like to send a PR to address it?

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 30, 2024

@tswaters

Please review #3655

@tswaters
Copy link
Author

tswaters commented Oct 1, 2024

Looks good to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mocks Issues/PRs related to mocks feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants