-
-
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: added option for undici proxy agent (#363)
Closes #159 Signed-off-by: Jamie King <[email protected]>
- Loading branch information
1 parent
25ae131
commit 9a6d1ed
Showing
6 changed files
with
114 additions
and
5 deletions.
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
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,79 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { createServer } = require('node:http') | ||
const Fastify = require('fastify') | ||
const get = require('simple-get').concat | ||
const { createProxy } = require('proxy') | ||
const From = require('..') | ||
|
||
const configFormat = { | ||
string: (value) => value, | ||
'url instance': (value) => new URL(value), | ||
object: (value) => ({ uri: value }) | ||
} | ||
|
||
for (const [description, format] of Object.entries(configFormat)) { | ||
test(`use undici ProxyAgent to connect through proxy - configured via ${description}`, async (t) => { | ||
t.plan(5) | ||
const target = await buildServer() | ||
const proxy = await buildProxy() | ||
t.teardown(target.close.bind(target)) | ||
t.teardown(proxy.close.bind(proxy)) | ||
|
||
const targetUrl = `http://localhost:${target.address().port}` | ||
const proxyUrl = `http://localhost:${proxy.address().port}` | ||
|
||
proxy.on('connect', () => { | ||
t.ok(true, 'should connect to proxy') | ||
}) | ||
|
||
target.on('request', (req, res) => { | ||
res.setHeader('content-type', 'application/json') | ||
res.end(JSON.stringify({ hello: 'world' })) | ||
}) | ||
|
||
const instance = Fastify() | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
instance.register(From, { | ||
base: targetUrl, | ||
undici: { | ||
proxy: format(proxyUrl) | ||
} | ||
}) | ||
|
||
instance.get('/', (request, reply) => { | ||
reply.from() | ||
}) | ||
|
||
const executionFlow = () => new Promise((resolve) => { | ||
instance.listen({ port: 0 }, err => { | ||
t.error(err) | ||
|
||
get(`http://localhost:${instance.server.address().port}`, (err, res, data) => { | ||
t.error(err) | ||
t.same(res.statusCode, 200) | ||
t.match(JSON.parse(data.toString()), { hello: 'world' }) | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
|
||
await executionFlow() | ||
}) | ||
} | ||
|
||
function buildServer () { | ||
return new Promise((resolve) => { | ||
const server = createServer() | ||
server.listen(0, () => resolve(server)) | ||
}) | ||
} | ||
|
||
function buildProxy () { | ||
return new Promise((resolve) => { | ||
const server = createProxy(createServer()) | ||
server.listen(0, () => resolve(server)) | ||
}) | ||
} |
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