Skip to content

Commit d234a10

Browse files
authored
merge request headers (#18)
1 parent 2823640 commit d234a10

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/http-data-source.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,15 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
364364
}
365365
}
366366

367+
const headers = {
368+
...(this.globalRequestOptions?.headers || {}),
369+
...request.headers,
370+
}
371+
367372
const options = {
368373
...request,
369374
...this.globalRequestOptions,
375+
headers,
370376
}
371377

372378
if (options.method === 'GET') {

test/http-data-source.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,3 +1438,55 @@ test('Should be able to pass custom Undici Pool', async (t) => {
14381438

14391439
t.deepEqual(response.body, wanted)
14401440
})
1441+
1442+
test('Should be merge headers', async (t) => {
1443+
t.plan(2)
1444+
1445+
const path = '/'
1446+
1447+
const mockHeaders = {
1448+
'test-a': 'a',
1449+
'test-b': 'b'
1450+
}
1451+
const server = http.createServer((req, res) => {
1452+
t.is(req.method, 'GET')
1453+
res.writeHead(200, {
1454+
'content-type': 'application/json',
1455+
})
1456+
res.write(JSON.stringify({
1457+
'test-a': req.headers['test-a'],
1458+
'test-b': req.headers['test-b']
1459+
}))
1460+
res.end()
1461+
res.socket?.unref()
1462+
})
1463+
1464+
t.teardown(server.close.bind(server))
1465+
1466+
server.listen()
1467+
1468+
const baseURL = `http://localhost:${(server.address() as AddressInfo)?.port}`
1469+
const pool = new Pool(baseURL)
1470+
1471+
const dataSource = new (class extends HTTPDataSource {
1472+
constructor() {
1473+
super(baseURL, {
1474+
pool,
1475+
requestOptions: {
1476+
headers: {
1477+
'test-a': mockHeaders['test-a']
1478+
}
1479+
}
1480+
})
1481+
}
1482+
getFoo() {
1483+
return this.get(path, {headers: {
1484+
'test-b': mockHeaders['test-b']
1485+
}})
1486+
}
1487+
})()
1488+
1489+
const response = await dataSource.getFoo()
1490+
1491+
t.deepEqual(response.body, mockHeaders)
1492+
})

0 commit comments

Comments
 (0)