-
-
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.
158: Added a hook to get the url base based on URL object (#157)
* Added a hook to get the url base based on request params. Useful for gradual migrate services based on the request data * updated readme with the getUpstream documentation * added getUpstream test for undici and http * increase test coverage * increase test coverage * added disableCache to readme and ts interface Co-authored-by: Yohay <[email protected]>
- Loading branch information
Showing
7 changed files
with
155 additions
and
4 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
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,70 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const http = require('http') | ||
const get = require('simple-get').concat | ||
|
||
const instance = Fastify() | ||
const instanceWithoutBase = Fastify() | ||
instance.register(From, { | ||
base: 'http://localhost', | ||
http: true, | ||
disableCache: true | ||
}) | ||
|
||
instanceWithoutBase.register(From, { | ||
http: true, | ||
disableCache: true | ||
}) | ||
|
||
t.plan(13) | ||
t.tearDown(instance.close.bind(instance)) | ||
t.tearDown(instanceWithoutBase.close.bind(instanceWithoutBase)) | ||
|
||
const target = http.createServer((req, res) => { | ||
t.pass('request proxied') | ||
t.equal(req.method, 'GET') | ||
res.end(req.headers.host) | ||
}) | ||
|
||
instance.get('/test', (request, reply) => { | ||
reply.from('/test', { | ||
getUpstream: (req, base) => { | ||
t.pass('getUpstream called') | ||
return `${base}:${target.address().port}` | ||
} | ||
}) | ||
}) | ||
|
||
instanceWithoutBase.get('/test2', (request, reply) => { | ||
reply.from('/test2', { | ||
getUpstream: () => { | ||
t.pass('getUpstream called') | ||
return `http://localhost:${target.address().port}` | ||
} | ||
}) | ||
}) | ||
|
||
t.tearDown(target.close.bind(target)) | ||
|
||
instance.listen(0, (err) => { | ||
t.error(err) | ||
instanceWithoutBase.listen(0, (err) => { | ||
t.error(err) | ||
target.listen(0, (err) => { | ||
t.error(err) | ||
|
||
get(`http://localhost:${instance.server.address().port}/test`, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
|
||
get(`http://localhost:${instanceWithoutBase.server.address().port}/test2`, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
}) | ||
}) | ||
}) |
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,45 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const http = require('http') | ||
const get = require('simple-get').concat | ||
|
||
const instance = Fastify() | ||
instance.register(From, { | ||
disableCache: true | ||
}) | ||
|
||
t.plan(7) | ||
t.tearDown(instance.close.bind(instance)) | ||
|
||
const target = http.createServer((req, res) => { | ||
t.pass('request proxied') | ||
t.equal(req.method, 'GET') | ||
res.end(req.headers.host) | ||
}) | ||
|
||
instance.get('/test', (request, reply) => { | ||
reply.from('/test', { | ||
getUpstream: () => { | ||
t.pass('getUpstream called') | ||
return `http://localhost:${target.address().port}` | ||
} | ||
}) | ||
}) | ||
|
||
t.tearDown(target.close.bind(target)) | ||
|
||
instance.listen(0, (err) => { | ||
t.error(err) | ||
|
||
target.listen(0, (err) => { | ||
t.error(err) | ||
|
||
get(`http://localhost:${instance.server.address().port}/test`, (err, res) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
}) | ||
}) | ||
}) |
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