Skip to content

MockAgent intercept().reply() with single callback provides headers in flat array form #2078

@ckohen

Description

@ckohen

Bug Description

This is the same bug as #1556 but for .reply called with a single callback instead of a callback for data. I believe I have identified the cause:

mockDispatch.data = { ...mockDispatch.data, ...mockDispatch.data.callback(opts) }

This callback is called with the raw options passed as opposed to the mutated options that later gets passed to a "data" callback

Reproducible By

import { MockAgent, setGlobalDispatcher } from 'undici'

const mockAgent = new MockAgent()
const mockPool = mockAgent.get('http://localhost')

setGlobalDispatcher(mockAgent)

mockPool.intercept({
  path: '/foo',
  method: 'GET',
}).reply((options) => {
  console.log('headers in reply:', Object.prototype.toString.call(options.headers), options.headers)
  return { statusCode: 200 };
})

await fetch('http://localhost/foo')

Expected Behavior

As specified in the type definitions, I would expect a Headers object when using fetch()

Logs & Screenshots

headers in reply: [object Array] [
  'accept',
  '*/*',
  'accept-language',
  '*',
  'sec-fetch-mode',
  'cors',
  'user-agent',
  'undici',
  'accept-encoding',
  'gzip, deflate'
]

Environment

  • Windows 10
  • Node v18.15.0
  • Undici 5.21.2
  • Fetch used is global fetch in node, not imported from undici

Additional context

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions