Skip to content

Commit e3fd2aa

Browse files
authored
Merge pull request #168 from nulib/fix-content-type
Put Content-Type in the headers object
2 parents 4664c25 + da12748 commit e3fd2aa

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function ensureCharacterEncoding(response, defaultEncoding = "UTF-8") {
8888

8989
if (!contentTypeHeader) {
9090
contentTypeHeader = "Content-Type";
91-
response[contentTypeHeader] ||= "application/json; charset=UTF-8";
91+
response.headers[contentTypeHeader] ||= "application/json; charset=UTF-8";
9292
}
9393

9494
const value = parseHeader(response.headers[contentTypeHeader]);

test/unit/api/helpers.test.js

+32
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
baseUrl,
1212
decodeEventBody,
1313
decodeToken,
14+
ensureCharacterEncoding,
1415
isFromReadingRoom,
1516
maybeUseProxiedIp,
1617
normalizeHeaders,
@@ -403,4 +404,35 @@ describe("helpers", () => {
403404
expect(result.queryStringParameters).to.eql({});
404405
});
405406
});
407+
408+
describe("ensureCharacterEncoding", () => {
409+
const response = { statusCode: 200, body: "Hello, World!" };
410+
411+
it("passes through an existing character set", () => {
412+
const result = ensureCharacterEncoding({
413+
...response,
414+
headers: { "Content-Type": "text/plain; charset=ISO-8859-1" },
415+
});
416+
expect(result.headers["Content-Type"]).to.eql(
417+
"text/plain; charset=ISO-8859-1"
418+
);
419+
});
420+
421+
it("adds character set if it's missing", () => {
422+
const result = ensureCharacterEncoding({
423+
...response,
424+
headers: { "Content-Type": "text/plain" },
425+
});
426+
expect(result.headers["Content-Type"]).to.eql(
427+
"text/plain; charset=UTF-8"
428+
);
429+
});
430+
431+
it("adds content type if it's missing", () => {
432+
const result = ensureCharacterEncoding({ ...response, headers: {} });
433+
expect(result.headers["Content-Type"]).to.eql(
434+
"application/json; charset=UTF-8"
435+
);
436+
});
437+
});
406438
});

0 commit comments

Comments
 (0)