Skip to content

Commit

Permalink
Respect custom headers when using Servers (stellar#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
celestialkylin authored and BlaineHeffron committed Jun 20, 2024
1 parent aa9c8f6 commit d28cc93
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/horizon/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -822,6 +825,7 @@ export namespace Server {
appName?: string;
appVersion?: string;
authToken?: string;
headers?: Record<string, string>;
}

export interface Timebounds {
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions test/integration/client_headers_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit d28cc93

Please sign in to comment.