Skip to content

Commit 75cd882

Browse files
committed
fix: #1700 cannot set Authorization via extraHeaders
1 parent 77806c7 commit 75cd882

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

src/JsonService.test.ts

+82-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ describe("JsonService", () => {
1717
"Custom-Header-1": "this-is-header-1",
1818
"Custom-Header-2": "this-is-header-2",
1919
"acCept" : "application/fake",
20-
"AuthoriZation" : "not good",
2120
"Content-Type": "application/fail",
2221
};
2322
const dynamicExtraHeaders = {
@@ -26,7 +25,6 @@ describe("JsonService", () => {
2625
return "my-name-is-header-2";
2726
},
2827
"acCept" : () => "nothing",
29-
"AuthoriZation" : () => "not good",
3028
"Content-Type": "application/fail",
3129
};
3230

@@ -587,4 +585,86 @@ describe("JsonService", () => {
587585
expect(result).toEqual(json);
588586
});
589587
});
588+
589+
describe("_appendExtraHeaders", () => {
590+
it("should add extra static headers", () => {
591+
// arrange
592+
const headers = {
593+
"Accept": "application/json",
594+
};
595+
subject["_extraHeaders"] = {
596+
"foo": "bar",
597+
};
598+
599+
// act
600+
subject["_appendExtraHeaders"](headers);
601+
602+
// assert
603+
expect(headers).toMatchObject({
604+
"Accept": "application/json",
605+
"foo": "bar",
606+
});
607+
});
608+
609+
it("should add extra dynamic headers", () => {
610+
// arrange
611+
const headers = {
612+
"Accept": "application/json",
613+
};
614+
subject["_extraHeaders"] = {
615+
"foo": () => {
616+
return "bar";
617+
},
618+
};
619+
620+
// act
621+
subject["_appendExtraHeaders"](headers);
622+
623+
// assert
624+
expect(headers).toMatchObject({
625+
"Accept": "application/json",
626+
"foo": "bar",
627+
});
628+
});
629+
630+
it("should skip protected special headers", () => {
631+
// arrange
632+
const headers = {
633+
"Accept": "application/json",
634+
};
635+
subject["_extraHeaders"] = {
636+
"foo": "bar",
637+
"accept": "application/xml",
638+
};
639+
640+
// act
641+
subject["_appendExtraHeaders"](headers);
642+
643+
// assert
644+
expect(headers).toMatchObject({
645+
"Accept": "application/json",
646+
"foo": "bar",
647+
});
648+
});
649+
650+
it("should skip override special headers", () => {
651+
// arrange
652+
const headers = {
653+
"Authorization": "Bearer 1",
654+
};
655+
subject["_extraHeaders"] = {
656+
"foo": "bar",
657+
"Authorization": "Bearer 2",
658+
};
659+
660+
// act
661+
subject["_appendExtraHeaders"](headers);
662+
663+
// assert
664+
expect(headers).toMatchObject({
665+
"Authorization": "Bearer 1",
666+
"foo": "bar",
667+
});
668+
});
669+
});
590670
});

src/JsonService.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class JsonService {
9191
headers["Authorization"] = "Bearer " + token;
9292
}
9393

94-
this.appendExtraHeaders(headers);
94+
this._appendExtraHeaders(headers);
9595

9696
let response: Response;
9797
try {
@@ -147,7 +147,7 @@ export class JsonService {
147147
headers["Authorization"] = "Basic " + basicAuth;
148148
}
149149

150-
this.appendExtraHeaders(headers);
150+
this._appendExtraHeaders(headers);
151151

152152
let response: Response;
153153
try {
@@ -194,22 +194,29 @@ export class JsonService {
194194
return json;
195195
}
196196

197-
private appendExtraHeaders(
197+
private _appendExtraHeaders(
198198
headers: Record<string, string>,
199199
): void {
200200
const logger = this._logger.create("appendExtraHeaders");
201201
const customKeys = Object.keys(this._extraHeaders);
202202
const protectedHeaders = [
203-
"authorization",
204203
"accept",
205204
"content-type",
206205
];
206+
const preventOverride = [
207+
"authorization",
208+
];
207209
if (customKeys.length === 0) {
208210
return;
209211
}
210212
customKeys.forEach((headerName) => {
211213
if (protectedHeaders.includes(headerName.toLocaleLowerCase())) {
212-
logger.warn("Protected header could not be overridden", headerName, protectedHeaders);
214+
logger.warn("Protected header could not be set", headerName, protectedHeaders);
215+
return;
216+
}
217+
if (preventOverride.includes(headerName.toLocaleLowerCase()) &&
218+
Object.keys(headers).includes(headerName)) {
219+
logger.warn("Header could not be overridden", headerName, preventOverride);
213220
return;
214221
}
215222
const content = (typeof this._extraHeaders[headerName] === "function") ?

0 commit comments

Comments
 (0)