-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: return 502 on invalid upstream status code (#386)
* fix: return 502 on invalid upstream status code * chore: move warning log into error handler Co-authored-by: Dan Castillo <[email protected]> Signed-off-by: Simon Schoonjans <[email protected]> * chore: apply suggestion to always map to badGatewayError --------- Signed-off-by: Simon Schoonjans <[email protected]> Co-authored-by: Dan Castillo <[email protected]>
- Loading branch information
1 parent
7a344ea
commit b313122
Showing
3 changed files
with
62 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
const From = require('..') | ||
const http = require('node:http') | ||
const get = require('simple-get').concat | ||
|
||
const instance = Fastify() | ||
instance.register(From) | ||
|
||
t.plan(8) | ||
t.teardown(instance.close.bind(instance)) | ||
|
||
const target = http.createServer((req, res) => { | ||
t.pass('request proxied') | ||
t.equal(req.method, 'GET') | ||
res.statusCode = 888 | ||
res.end('non-standard status code') | ||
}) | ||
|
||
instance.get('/', (_, reply) => { | ||
reply.from(`http://localhost:${target.address().port}`, { | ||
onResponse: (_, reply, res) => { | ||
t.equal(res.statusCode, 888) | ||
} | ||
}) | ||
}) | ||
|
||
t.teardown(target.close.bind(target)) | ||
|
||
instance.listen({ port: 0 }, (err) => { | ||
t.error(err) | ||
|
||
target.listen({ port: 0 }, (err) => { | ||
t.error(err) | ||
|
||
get( | ||
`http://localhost:${instance.server.address().port}`, | ||
(err, res, data) => { | ||
t.error(err) | ||
t.equal(res.statusCode, 502) | ||
t.same(JSON.parse(data), { | ||
statusCode: 502, | ||
code: 'FST_REPLY_FROM_BAD_GATEWAY', | ||
error: 'Bad Gateway', | ||
message: 'Bad Gateway' | ||
}) | ||
} | ||
) | ||
}) | ||
}) |