Skip to content

Commit

Permalink
Merge pull request #168 from nulib/fix-content-type
Browse files Browse the repository at this point in the history
Put Content-Type in the headers object
  • Loading branch information
mbklein authored Oct 2, 2023
2 parents 4664c25 + da12748 commit e3fd2aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function ensureCharacterEncoding(response, defaultEncoding = "UTF-8") {

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

const value = parseHeader(response.headers[contentTypeHeader]);
Expand Down
32 changes: 32 additions & 0 deletions test/unit/api/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
baseUrl,
decodeEventBody,
decodeToken,
ensureCharacterEncoding,
isFromReadingRoom,
maybeUseProxiedIp,
normalizeHeaders,
Expand Down Expand Up @@ -403,4 +404,35 @@ describe("helpers", () => {
expect(result.queryStringParameters).to.eql({});
});
});

describe("ensureCharacterEncoding", () => {
const response = { statusCode: 200, body: "Hello, World!" };

it("passes through an existing character set", () => {
const result = ensureCharacterEncoding({
...response,
headers: { "Content-Type": "text/plain; charset=ISO-8859-1" },
});
expect(result.headers["Content-Type"]).to.eql(
"text/plain; charset=ISO-8859-1"
);
});

it("adds character set if it's missing", () => {
const result = ensureCharacterEncoding({
...response,
headers: { "Content-Type": "text/plain" },
});
expect(result.headers["Content-Type"]).to.eql(
"text/plain; charset=UTF-8"
);
});

it("adds content type if it's missing", () => {
const result = ensureCharacterEncoding({ ...response, headers: {} });
expect(result.headers["Content-Type"]).to.eql(
"application/json; charset=UTF-8"
);
});
});
});

0 comments on commit e3fd2aa

Please sign in to comment.