Skip to content

Commit c976b9d

Browse files
authored
formate /_ip with JSON instead (#25574)
1 parent 79285d5 commit c976b9d

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

middleware/remote-ip.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ const noCacheControl = cacheControlFactory(0)
44

55
export default async function remoteIp(req, res, next) {
66
noCacheControl(res)
7-
res.send(
8-
`ip=${req.ip}\t` +
9-
`x-forwarded-for=${req.headers['x-forwarded-for']}\t` +
10-
`fastly-client-ip=${req.headers['fastly-client-ip']}`
11-
)
7+
res.json({
8+
ip: req.ip,
9+
'x-forwarded-for': req.headers['x-forwarded-for'] || null,
10+
'fastly-client-ip': req.headers['fastly-client-ip'] || null,
11+
})
1212
}

tests/routing/remote-ip.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ describe('remote ip debugging', () => {
77
test('basics', async () => {
88
const res = await get('/_ip')
99
expect(res.statusCode).toBe(200)
10-
const kv = res.text.trim().split(/\s/g)
11-
expect(kv[0].startsWith('ip=')).toBeTruthy()
12-
expect(kv[1].startsWith('x-forwarded-for=')).toBeTruthy()
13-
expect(kv[2].startsWith('fastly-client-ip=')).toBeTruthy()
10+
expect(res.header['content-type']).toContain('application/json')
11+
const kv = JSON.parse(res.text)
12+
expect('ip' in kv).toBeTruthy()
13+
expect('x-forwarded-for' in kv).toBeTruthy()
14+
expect('fastly-client-ip' in kv).toBeTruthy()
1415
})
1516

1617
test('carrying the x-forwarded-for header', async () => {
@@ -20,6 +21,7 @@ describe('remote ip debugging', () => {
2021
},
2122
})
2223
expect(res.statusCode).toBe(200)
23-
expect(res.text.includes('x-forwarded-for=123.123.0.1')).toBeTruthy()
24+
const kv = JSON.parse(res.text)
25+
expect(kv['x-forwarded-for']).toBe('123.123.0.1')
2426
})
2527
})

0 commit comments

Comments
 (0)