Skip to content

Commit

Permalink
fix: Agent.Options.factory should accept URL object or string as para…
Browse files Browse the repository at this point in the history
…meter (nodejs#2295)
  • Loading branch information
nicole0707 authored and crysmags committed Feb 27, 2024
1 parent 89616ef commit 3a5dd13
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
73 changes: 73 additions & 0 deletions test/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
request,
stream,
pipeline,
Pool,
setGlobalDispatcher,
getGlobalDispatcher
} = require('../')
Expand Down Expand Up @@ -267,6 +268,78 @@ test('multiple connections', t => {
})
})

test('agent factory supports URL parameter', (t) => {
t.plan(2)

const noopHandler = {
onConnect () {},
onHeaders () {},
onData () {},
onComplete () {
server.close()
},
onError (err) {
throw err
}
}

const dispatcher = new Agent({
factory: (origin, opts) => {
t.ok(origin instanceof URL)
return new Pool(origin, opts)
}
})

const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.end('asd')
})

server.listen(0, () => {
t.doesNotThrow(() => dispatcher.dispatch({
origin: new URL(`http://localhost:${server.address().port}`),
path: '/',
method: 'GET'
}, noopHandler))
})
})

test('agent factory supports string parameter', (t) => {
t.plan(2)

const noopHandler = {
onConnect () {},
onHeaders () {},
onData () {},
onComplete () {
server.close()
},
onError (err) {
throw err
}
}

const dispatcher = new Agent({
factory: (origin, opts) => {
t.ok(typeof origin === 'string')
return new Pool(origin, opts)
}
})

const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.end('asd')
})

server.listen(0, () => {
t.doesNotThrow(() => dispatcher.dispatch({
origin: `http://localhost:${server.address().port}`,
path: '/',
method: 'GET'
}, noopHandler))
})
})

test('with globalAgent', t => {
t.plan(6)
const wanted = 'payload'
Expand Down
2 changes: 1 addition & 1 deletion types/agent.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare class Agent extends Dispatcher{
declare namespace Agent {
export interface Options extends Pool.Options {
/** Default: `(origin, opts) => new Pool(origin, opts)`. */
factory?(origin: URL, opts: Object): Dispatcher;
factory?(origin: string | URL, opts: Object): Dispatcher;
/** Integer. Default: `0` */
maxRedirections?: number;

Expand Down

0 comments on commit 3a5dd13

Please sign in to comment.