fastify-http-proxy
Advanced tools
Comparing version 6.1.0 to 6.2.0
@@ -120,3 +120,3 @@ 'use strict' | ||
let rewritePrefix = opts.rewritePrefix || new URL(opts.upstream).pathname | ||
let rewritePrefix = opts.rewritePrefix || (opts.upstream ? new URL(opts.upstream).pathname : '/') | ||
@@ -131,3 +131,3 @@ if (!prefix.endsWith('/') && rewritePrefix.endsWith('/')) { | ||
async function httpProxy (fastify, opts) { | ||
if (!opts.upstream) { | ||
if (!opts.upstream && !(opts.upstream === '' && opts.replyOptions && typeof opts.replyOptions.getUpstream === 'function')) { | ||
throw new Error('upstream must be specified') | ||
@@ -134,0 +134,0 @@ } |
{ | ||
"name": "fastify-http-proxy", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "proxy http requests, for Fastify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
107
test/test.js
@@ -65,2 +65,27 @@ 'use strict' | ||
test('dynamic upstream for basic proxy', async t => { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: '', | ||
replyOptions: { | ||
getUpstream: function (original, base) { | ||
return `http://localhost:${origin.server.address().port}` | ||
} | ||
} | ||
}) | ||
await server.listen(0) | ||
t.teardown(server.close.bind(server)) | ||
const resultRoot = await got( | ||
`http://localhost:${server.server.address().port}` | ||
) | ||
t.equal(resultRoot.body, 'this is root') | ||
const resultA = await got( | ||
`http://localhost:${server.server.address().port}/a` | ||
) | ||
t.equal(resultA.body, 'this is a') | ||
}) | ||
test('redirects passthrough', async t => { | ||
@@ -87,2 +112,28 @@ const server = Fastify() | ||
test('dynamic upstream for redirects passthrough', async t => { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: '', | ||
replyOptions: { | ||
getUpstream: function (original, base) { | ||
return `http://localhost:${origin.server.address().port}` | ||
} | ||
} | ||
}) | ||
await server.listen(0) | ||
t.teardown(server.close.bind(server)) | ||
const { | ||
headers: { location }, | ||
statusCode | ||
} = await got( | ||
`http://localhost:${server.server.address().port}/redirect`, { | ||
followRedirect: false | ||
} | ||
) | ||
t.equal(location, 'https://fastify.io') | ||
t.equal(statusCode, 302) | ||
}) | ||
test('no upstream will throw', async t => { | ||
@@ -126,2 +177,33 @@ const server = Fastify() | ||
test('dynamic upstream for prefixed proxy', async t => { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: '', | ||
prefix: '/my-prefix', | ||
replyOptions: { | ||
getUpstream: function (original, base) { | ||
return `http://localhost:${origin.server.address().port}` | ||
} | ||
} | ||
}) | ||
await server.listen(0) | ||
t.teardown(server.close.bind(server)) | ||
const resultRoot = await got( | ||
`http://localhost:${server.server.address().port}/my-prefix/` | ||
) | ||
t.equal(resultRoot.body, 'this is root') | ||
const withoutSlash = await got( | ||
`http://localhost:${server.server.address().port}/my-prefix` | ||
) | ||
t.equal(withoutSlash.body, 'this is root') | ||
const resultA = await got( | ||
`http://localhost:${server.server.address().port}/my-prefix/a` | ||
) | ||
t.equal(resultA.body, 'this is a') | ||
}) | ||
test('posting stuff', async t => { | ||
@@ -147,2 +229,27 @@ const server = Fastify() | ||
test('dynamic upstream for posting stuff', async t => { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: '', | ||
replyOptions: { | ||
getUpstream: function (original, base) { | ||
return `http://localhost:${origin.server.address().port}` | ||
} | ||
} | ||
}) | ||
await server.listen(0) | ||
t.teardown(server.close.bind(server)) | ||
const resultRoot = await got( | ||
`http://localhost:${server.server.address().port}/this-has-data`, | ||
{ | ||
method: 'POST', | ||
json: { hello: 'world' }, | ||
responseType: 'json' | ||
} | ||
) | ||
t.same(resultRoot.body, { something: 'posted' }) | ||
}) | ||
test('skip proxying the incoming payload', async t => { | ||
@@ -149,0 +256,0 @@ const server = Fastify() |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47103
1209