From b893bec4e637bb3a8b2700a1a3ade3707497b283 Mon Sep 17 00:00:00 2001 From: Frazer Smith Date: Sat, 27 May 2023 14:31:27 +0100 Subject: [PATCH] fix(index): handle array of content-type headers (#32) --- src/index.js | 5 ++++- src/index.test.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index f55897a..770931a 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,10 @@ const secJSON = require("secure-json-parse"); async function fastifyJsonToXml(server, options) { server.addHook("onSend", async (req, res, payload) => { if ( - res.getHeader("content-type")?.includes("application/json") && + res + .getHeader("content-type") + ?.toString() + ?.includes("application/json") && accepts(req).type(["application/json", "application/xml"]) === "application/xml" ) { diff --git a/src/index.test.js b/src/index.test.js index e66f019..7a9c7d3 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -51,6 +51,13 @@ describe("JSON-To-XML plugin", () => { accept: "application/json", }, }, + { + testName: + "JSON body if 'application/json' before '*/*' in accept header", + headers: { + accept: "application/json, */*", + }, + }, { testName: "JSON body if 'application/json' before 'application/xml' in accept header",