Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setting of methods for proxying #149

Merged
merged 4 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ configuration passed to the route.

Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `fastify-reply-from`.

### httpMethods
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.

## websocket

This module has _partial_ support for forwarding websockets by passing a
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface FastifyHttpProxyOptions extends FastifyReplyFromOptions {
websocket?: boolean;
wsClientOptions?: ClientOptions;
wsServerOptions?: ServerOptions;
httpMethods?: string[];
}

declare const fastifyHttpProxy: FastifyPlugin<FastifyHttpProxyOptions>;
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ async function httpProxy (fastify, opts) {

fastify.route({
url: '/',
method: httpMethods,
method: opts.httpMethods || httpMethods,
preHandler,
config: opts.config || {},
handler
})
fastify.route({
url: '/*',
method: httpMethods,
method: opts.httpMethods || httpMethods,
preHandler,
config: opts.config || {},
handler
Expand Down
1 change: 1 addition & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ app.register(fastifyHttpProxy, {
http2: false,
config: { key: 1 },
replyOptions: { contentType: "application/json" },
httpMethods: ["DELETE", "GET", "HEAD", "PATCH", "POST", "PUT", "OPTIONS"],
preHandler: (request, reply) => {
expectType<RawRequestDefaultExpression>(request.raw);
expectType<RawReplyDefaultExpression>(reply.raw);
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,36 @@ async function run () {
}
t.fail()
})

test('settings of method types', async t => {
const server = Fastify()
server.register(proxy, {
upstream: `http://localhost:${origin.server.address().port}`,
httpMethods: ['POST']
})

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.deepEqual(resultRoot.body, { something: 'posted' })

let errored = false
try {
await await got(`http://localhost:${server.server.address().port}/a`)
} catch (err) {
t.equal(err.response.statusCode, 404)
errored = true
}
t.ok(errored)
})
}

run()