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

Headers in mock dispatcher #intercept() are case-sensitive #1579

Closed
jonahsnider opened this issue Jul 27, 2022 · 1 comment · Fixed by #1585
Closed

Headers in mock dispatcher #intercept() are case-sensitive #1579

jonahsnider opened this issue Jul 27, 2022 · 1 comment · Fixed by #1585
Labels
bug Something isn't working good first issue Good for newcomers Status: help-wanted This issue/pr is open for contributions

Comments

@jonahsnider
Copy link

jonahsnider commented Jul 27, 2022

Bug Description

The keys of the headers object given to mock dispatcher #intercept() are case sensitive.

Reproducible By

import undici from 'undici';

const mockAgent = new undici.MockAgent();
mockAgent.disableNetConnect();
undici.setGlobalDispatcher(mockAgent);

const mockPool = mockAgent.get('https://example.com');

// Works
mockPool
  .intercept({
    path: '/',
    headers: {
      authorization: 'Bearer 12345',
    },
  })
  .reply(200);

await undici.fetch('https://example.com', {
  headers: {
    Authorization: 'Bearer 12345',
  },
});

// Doesn't work
mockPool
  .intercept({
    path: '/',
    headers: {
      Authorization: 'Bearer 12345',
    },
  })
  .reply(200);

await undici.fetch('https://example.com', {
  headers: {
    Authorization: 'Bearer 12345',
  },
});

Expected Behavior

Both undici.fetch() calls should succeed.

Logs & Screenshots

/Users/me/my-library/node_modules/undici/lib/fetch/index.js:197
        Object.assign(new TypeError('fetch failed'), { cause: response.error })
                      ^

TypeError: fetch failed
    at Object.processResponse (/Users/me/my-library/node_modules/undici/lib/fetch/index.js:197:23)
    at /Users/me/my-library/node_modules/undici/lib/fetch/index.js:930:38
    at node:internal/process/task_queues:140:7
    at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
    at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8) {
  cause: MockNotMatchedError: Mock dispatch not matched for headers '["authorization","Bearer 12345","accept","*/*","accept-language","*","sec-fetch-mode","cors","user-agent","undici","accept-encoding","br, gzip, deflate"]': subsequent request to origin https://example.com was not allowed (net.connect disabled)
      at MockPool.dispatch (/Users/me/my-library/node_modules/undici/lib/mock/mock-utils.js:316:19)
      at [dispatch] (/Users/me/my-library/node_modules/undici/lib/agent.js:117:23)
      at Agent.dispatch (/Users/me/my-library/node_modules/undici/lib/dispatcher-base.js:146:29)
      at MockAgent.dispatch (/Users/me/my-library/node_modules/undici/lib/mock/mock-agent.js:65:25)
      at /Users/me/my-library/node_modules/undici/lib/fetch/index.js:1912:79
      at new Promise (<anonymous>)
      at dispatch (/Users/me/my-library/node_modules/undici/lib/fetch/index.js:1912:12)
      at httpNetworkFetch (/Users/me/my-library/node_modules/undici/lib/fetch/index.js:1724:61)
      at httpNetworkOrCacheFetch (/Users/me/my-library/node_modules/undici/lib/fetch/index.js:1429:35)
      at httpFetch (/Users/me/my-library/node_modules/undici/lib/fetch/index.js:1017:39) {
    code: 'UND_MOCK_ERR_MOCK_NOT_MATCHED'
  }
}

Node.js v18.6.0

Environment

Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000 arm64

{
  node: '18.6.0',
  v8: '10.2.154.13-node.8',
  uv: '1.43.0',
  zlib: '1.2.11',
  brotli: '1.0.9',
  ares: '1.18.1',
  modules: '108',
  nghttp2: '1.47.0',
  napi: '8',
  llhttp: '6.0.7',
  openssl: '3.0.5+quic',
  cldr: '41.0',
  icu: '71.1',
  tz: '2022a',
  unicode: '14.0',
  ngtcp2: '0.1.0-DEV',
  nghttp3: '0.1.0-DEV'
}

Additional context

@jonahsnider jonahsnider added the bug Something isn't working label Jul 27, 2022
@jonahsnider jonahsnider changed the title Headers in MockAgent#intercept() are case-sensitive Headers in mock dispatcher #intercept() are case-sensitive Jul 27, 2022
@ronag
Copy link
Member

ronag commented Jul 27, 2022

PR welcome!

@ronag ronag added the Status: help-wanted This issue/pr is open for contributions label Jul 27, 2022
@mcollina mcollina added the good first issue Good for newcomers label Jul 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers Status: help-wanted This issue/pr is open for contributions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants