Skip to content

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

@jonahsnider

Description

@jonahsnider

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: help-wantedThis issue/pr is open for contributionsbugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions