-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhost-header.test.js
77 lines (64 loc) · 1.95 KB
/
host-header.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict'
const { test } = require('tap')
const Fastify = require('fastify')
const { request, Agent } = require('undici')
const From = require('..')
const nock = require('nock')
test('hostname', async (t) => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
nock('http://httpbin.org')
.get('/ip')
.reply(200, function () {
t.equal(this.req.headers.host, 'httpbin.org')
return { origin: '127.0.0.1' }
})
instance.get('*', (_request, reply) => {
reply.from(null, {
rewriteRequestHeaders: (originalReq, headers) => {
t.equal(headers.host, 'httpbin.org')
t.equal(originalReq.headers.host, `localhost:${instance.server.address().port}`)
return headers
}
})
})
instance.register(From, {
base: 'http://httpbin.org',
http: {} // force the use of Node.js core
})
await instance.listen({ port: 0 })
const res = await request(`http://localhost:${instance.server.address().port}/ip`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json')
t.equal(typeof (await res.body.json()).origin, 'string')
})
test('hostname and port', async (t) => {
const instance = Fastify()
t.teardown(instance.close.bind(instance))
nock('http://httpbin.org:8080')
.get('/ip')
.reply(200, function () {
t.equal(this.req.headers.host, 'httpbin.org:8080')
return { origin: '127.0.0.1' }
})
instance.register(From, {
base: 'http://httpbin.org:8080',
http: true
})
instance.get('*', (_request, reply) => {
reply.from()
})
await instance.listen({ port: 0 })
const res = await request(`http://localhost:${instance.server.address().port}/ip`, {
dispatcher: new Agent({
pipelining: 0
})
})
t.equal(res.statusCode, 200)
t.equal(res.headers['content-type'], 'application/json')
t.equal(typeof (await res.body.json()).origin, 'string')
})