-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: duck type undici option (#321)
- Loading branch information
1 parent
5c106e6
commit dc2e94d
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const undici = require('undici') | ||
const Fastify = require('fastify') | ||
const From = require('..') | ||
|
||
class CustomDispatcher { | ||
constructor (...args) { | ||
this._dispatcher = new undici.Pool(...args) | ||
} | ||
|
||
request (...args) { | ||
return this._dispatcher.request(...args) | ||
} | ||
|
||
close (...args) { | ||
return this._dispatcher.close(...args) | ||
} | ||
|
||
destroy (...args) { | ||
return this._dispatcher.destroy(...args) | ||
} | ||
} | ||
|
||
test('use a custom instance of \'undici\'', async t => { | ||
const target = Fastify({ | ||
keepAliveTimeout: 1 | ||
}) | ||
|
||
target.get('/', (req, reply) => { | ||
t.pass('request proxied') | ||
|
||
reply.headers({ | ||
'Content-Type': 'text/plain', | ||
'x-my-header': 'hello!' | ||
}) | ||
|
||
reply.statusCode = 205 | ||
reply.send('hello world') | ||
}) | ||
|
||
await target.listen({ port: 3001 }) | ||
t.teardown(async () => { | ||
await target.close() | ||
}) | ||
|
||
const instance = Fastify({ | ||
keepAliveTimeout: 1 | ||
}) | ||
|
||
instance.register(From, { | ||
undici: new CustomDispatcher('http://localhost:3001') | ||
}) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from('http://myserver.local') | ||
}) | ||
|
||
await instance.listen({ port: 0 }) | ||
t.teardown(async () => { | ||
await instance.close() | ||
}) | ||
|
||
const res = await undici.request(`http://localhost:${instance.server.address().port}`) | ||
|
||
t.equal(res.headers['content-type'], 'text/plain') | ||
t.equal(res.headers['x-my-header'], 'hello!') | ||
t.equal(res.statusCode, 205) | ||
|
||
const data = await res.body.text() | ||
t.equal(data, 'hello world') | ||
}) |