@fastify/express 4.0.9 → 4.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -57,23 +57,30 @@ test('trust proxy protocol', async (t) => {
57
57
  })
58
58
 
59
59
  test('query params still in in the request url after decodeURI', async (t) => {
60
- t.plan(2)
61
60
  const fastify = Fastify()
62
61
 
63
62
  t.after(() => fastify.close())
64
63
 
64
+ // Record what the middleware sees and assert afterwards: Express would
65
+ // swallow an assertion thrown inside the middleware.
66
+ let seen
65
67
  fastify.register(expressPlugin).after(() => {
66
- fastify.use('/admin', function (req, res) {
67
- t.assert.deepStrictEqual(req.originalUrl, '/%61dmin?test=%76alue&test2=%76alue', 'originalUrl is not decoded')
68
- t.assert.deepStrictEqual(req.url, '/admin?test=value&test2=value', 'url is decoded')
69
-
68
+ fastify.use(function (req, res) {
69
+ seen = { originalUrl: req.originalUrl, url: req.url, query: { ...req.query } }
70
70
  res.sendStatus(200)
71
71
  })
72
72
  })
73
73
 
74
74
  const address = await fastify.listen({ port: 0 })
75
75
 
76
- await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
76
+ const response = await fetch(`${address}/%61dmin?test=%76alue&test2=%76alue`)
77
+ t.assert.strictEqual(response.status, 200)
78
+ t.assert.deepStrictEqual(seen, {
79
+ originalUrl: '/%61dmin?test=%76alue&test2=%76alue',
80
+ // Only the path is decoded; the query string is decoded once, by Express.
81
+ url: '/admin?test=%76alue&test2=%76alue',
82
+ query: { test: 'value', test2: 'value' }
83
+ })
77
84
  })
78
85
 
79
86
  test('passing createProxyHandler sets up a Proxy with Express req', async t => {
@@ -182,3 +189,47 @@ test('createProxyHandler has access to Fastify request object', async t => {
182
189
  t.assert.deepStrictEqual(response.headers.get('content-length'), '' + responseText.length)
183
190
  t.assert.deepStrictEqual(JSON.parse(responseText), { hello: 'world' })
184
191
  })
192
+
193
+ test('createProxyHandler keeps the original request on the response', async t => {
194
+ t.plan(5)
195
+
196
+ const fastify = Fastify()
197
+ t.after(() => fastify.close())
198
+
199
+ let rawRequest
200
+ fastify.register(expressPlugin, {
201
+ createProxyHandler: fastifyReq => {
202
+ rawRequest = fastifyReq.raw
203
+ return {}
204
+ }
205
+ })
206
+ .after(() => {
207
+ fastify.use(function (req, res, next) {
208
+ // Express still works with the Proxy while it handles the request
209
+ t.assert.notStrictEqual(req, rawRequest)
210
+ t.assert.strictEqual(res.req, req)
211
+ next()
212
+ })
213
+ })
214
+
215
+ // Node.js reads `res.req` when the response finishes and expects the
216
+ // request it created there, not the Proxy that Express assigned
217
+ const finished = new Promise(resolve => {
218
+ fastify.addHook('onResponse', function (_request, reply, done) {
219
+ resolve(reply.raw.req)
220
+ done()
221
+ })
222
+ })
223
+
224
+ fastify.get('/', function (_request, reply) {
225
+ reply.send({ hello: 'world' })
226
+ })
227
+
228
+ const address = await fastify.listen({ port: 0 })
229
+
230
+ const response = await fetch(address)
231
+
232
+ t.assert.deepStrictEqual(response.status, 200)
233
+ t.assert.deepStrictEqual(await response.json(), { hello: 'world' })
234
+ t.assert.strictEqual(await finished, rawRequest)
235
+ })
@@ -34,7 +34,7 @@ test('POST request without form body works', async t => {
34
34
 
35
35
  const result = await fetch(address + '/hello', {
36
36
  method: 'post',
37
- signal: AbortSignal.timeout(100)
37
+ signal: AbortSignal.timeout(5000)
38
38
  })
39
39
 
40
40
  t.assert.deepStrictEqual(result.status, 200)
@@ -67,7 +67,7 @@ test('POST request with form body and without body-parser works', async t => {
67
67
  const result = await fetch(address + '/hello', {
68
68
  method: 'post',
69
69
  body: new URLSearchParams({ input: 'test' }),
70
- signal: AbortSignal.timeout(100)
70
+ signal: AbortSignal.timeout(5000)
71
71
  })
72
72
 
73
73
  t.assert.deepStrictEqual(result.status, 200)
@@ -133,7 +133,7 @@ test('POST request with form body and body-parser hangs up, compatibility case',
133
133
  const result = await fetch(address + '/hello', {
134
134
  method: 'post',
135
135
  body: new URLSearchParams({ input: 'test' }),
136
- signal: AbortSignal.timeout(100)
136
+ signal: AbortSignal.timeout(5000)
137
137
  })
138
138
 
139
139
  t.assert.deepStrictEqual(result.status, 200)