diff --git a/src/horizon/server.ts b/src/horizon/server.ts index 48c92106d..e039994b3 100644 --- a/src/horizon/server.ts +++ b/src/horizon/server.ts @@ -92,6 +92,9 @@ export class Server { if (opts.authToken) { customHeaders["X-Auth-Token"] = opts.authToken; } + if (opts.headers) { + Object.assign(customHeaders, opts.headers); + } if (Object.keys(customHeaders).length > 0) { AxiosClient.interceptors.request.use((config) => { // merge the custom headers with an existing headers, where customs @@ -822,6 +825,7 @@ export namespace Server { appName?: string; appVersion?: string; authToken?: string; + headers?: Record; } export interface Timebounds { diff --git a/src/rpc/server.ts b/src/rpc/server.ts index e3c518efb..a0d09f2ba 100644 --- a/src/rpc/server.ts +++ b/src/rpc/server.ts @@ -75,7 +75,7 @@ export class Server { constructor(serverURL: string, opts: Server.Options = {}) { this.serverURL = URI(serverURL); - if (opts.headers && Object.keys(opts.headers).length === 0) { + if (opts.headers && Object.keys(opts.headers).length !== 0) { AxiosClient.interceptors.request.use((config: any) => { // merge the custom headers into any existing headers config.headers = Object.assign(config.headers, opts.headers); diff --git a/test/integration/client_headers_test.js b/test/integration/client_headers_test.js index e070d7e0c..bd564c1be 100644 --- a/test/integration/client_headers_test.js +++ b/test/integration/client_headers_test.js @@ -72,4 +72,26 @@ describe("integration tests: client headers", function (done) { .stream({ onerror: (err) => done(err) }); }); }); + + it("sends client via custom headers", function (done) { + let server; + + const requestHandler = (request, response) => { + expect(request.headers["authorization"]).to.be.equal("123456789"); + response.end(); + server.close(() => done()); + }; + + server = http.createServer(requestHandler); + server.listen(port, (err) => { + if (err) { + done(err); + return; + } + + new Horizon.Server(`http://localhost:${port}`, { headers: { "authorization": "123456789" }, allowHttp: true }) + .operations() + .call(); + }); + }); });