diff --git a/src/internal/client.ts b/src/internal/client.ts index d0e7f614..d786ce53 100644 --- a/src/internal/client.ts +++ b/src/internal/client.ts @@ -153,6 +153,7 @@ const requestOptionProperties = [ 'secureProtocol', 'servername', 'sessionIdContext', + 'headers', ] as const export interface RetryOptions { @@ -481,6 +482,11 @@ export class TypedClient { // If custom transportAgent was supplied earlier, we'll inject it here agent: this.transportAgent, } + if (this.reqOptions.headers) { + for (const [k, v] of Object.entries(this.reqOptions.headers)) { + reqOptions.headers[k.toLowerCase()] = v + } + } // Verify if virtual host supported. let virtualHostStyle diff --git a/tests/unit/test.js b/tests/unit/test.js index 1ca7aeb4..eeed1d68 100644 --- a/tests/unit/test.js +++ b/tests/unit/test.js @@ -14,6 +14,7 @@ * limitations under the License. */ +import http from 'node:http' import * as Stream from 'node:stream' import { assert, expect, use } from 'chai' @@ -379,6 +380,39 @@ describe('Client', function () { done() } }) + it('should send custom headers with requests', async () => { + const headers = { 'X-Test-Header': 'test-value' } + + const httpAgent = new http.Agent() + + const client = new Minio.Client({ + endPoint: 'localhost', + port: 9000, + accessKey: 'accesskey', + secretKey: 'secretkey', + useSSL: false, + transportAgent: httpAgent, + }) + client.setRequestOptions({ + headers, + }) + + const promiseConnection = new Promise((resolve, reject) => { + const timeout = setTimeout(() => { + reject(new Error('Timed out')) + }, 5000) + const originalCreateConnection = httpAgent.createConnection + httpAgent.createConnection = (options, callback) => { + clearTimeout(timeout) + resolve(options) + return originalCreateConnection(options, callback) + } + }) + + await client.listBuckets() + const connectionOptions = await promiseConnection + assert.propertyVal(connectionOptions.headers, 'x-test-header', headers['X-Test-Header']) + }) }) describe('Presigned URL', () => { describe('presignedUrl', () => {