Skip to content

Commit 4c69bd6

Browse files
author
dustin deus
committed
fix: overwrite request options, add test
1 parent 7de3171 commit 4c69bd6

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/http-data-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
317317
}
318318

319319
const options = {
320-
...this.globalRequestOptions,
321320
...request,
321+
...this.globalRequestOptions,
322322
}
323323

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

test/http-data-source.test.ts

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ test('Should be possible to pass a request context', async (t) => {
443443
async getFoo() {
444444
return await this.get(path, {
445445
context: {
446-
a: '1'
447-
}
446+
a: '1',
447+
},
448448
})
449449
}
450450
})()
@@ -603,6 +603,52 @@ test('Should be able to modify request in willSendRequest', async (t) => {
603603
t.deepEqual(response.body, { name: 'foo' })
604604
})
605605

606+
test('Should be able to define base headers for every request', async (t) => {
607+
t.plan(4)
608+
609+
const path = '/'
610+
611+
const wanted = { name: 'foo' }
612+
613+
const server = http.createServer((req, res) => {
614+
t.is(req.method, 'GET')
615+
t.deepEqual(req.headers['x-foo'], 'bar')
616+
res.write(JSON.stringify(wanted))
617+
res.end()
618+
res.socket?.unref()
619+
})
620+
621+
t.teardown(server.close.bind(server))
622+
623+
server.listen()
624+
625+
const baseURL = `http://localhost:${(server.address() as AddressInfo)?.port}`
626+
627+
const dataSource = new (class extends HTTPDataSource {
628+
constructor() {
629+
super(baseURL, {
630+
requestOptions: {
631+
headers: {
632+
'X-Foo': 'bar',
633+
},
634+
},
635+
})
636+
}
637+
async onRequest(request: Request) {
638+
t.deepEqual(request.headers, {
639+
'X-Foo': 'bar',
640+
})
641+
}
642+
getFoo() {
643+
return this.get(path)
644+
}
645+
})()
646+
647+
const response = await dataSource.getFoo()
648+
649+
t.deepEqual(response.body, { name: 'foo' })
650+
})
651+
606652
test('Initialize data source with cache and context', async (t) => {
607653
t.plan(3)
608654

0 commit comments

Comments
 (0)