-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathfull-querystring-rewrite.test.js
43 lines (32 loc) · 1.2 KB
/
full-querystring-rewrite.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict'
const t = require('tap')
const Fastify = require('fastify')
const { request } = require('undici')
const From = require('..')
const http = require('node:http')
const instance = Fastify()
t.test('full querystring rewrite', async (t) => {
t.plan(7)
t.teardown(instance.close.bind(instance))
const target = http.createServer((req, res) => {
t.pass('request proxied')
t.equal(req.method, 'GET')
t.equal(req.url, '/world?a=b')
res.statusCode = 205
res.setHeader('Content-Type', 'text/plain')
res.setHeader('x-my-header', 'hello!')
res.end('hello world')
})
instance.get('/hello', (_request, reply) => {
reply.from(`http://localhost:${target.address().port}/world`)
})
t.teardown(target.close.bind(target))
await new Promise(resolve => target.listen({ port: 0 }, resolve))
instance.register(From)
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
t.equal(result.headers['content-type'], 'text/plain')
t.equal(result.headers['x-my-header'], 'hello!')
t.equal(result.statusCode, 205)
t.equal(await result.body.text(), 'hello world')
})