Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect custom headers when using rpc.Server #988

Merged
merged 4 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});
Loading