Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
feat(routes/contact): support application/xml responses (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Feb 22, 2022
1 parent 03f9530 commit 62c6380
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"fastify-swagger": "4.12.0",
"file-stream-rotator": "^0.6.1",
"fluent-json-schema": "^3.0.1",
"jstoxml": "^2.2.9",
"mssql": "^8.0.1",
"pg": "^8.7.1",
"pg-connection-string": "^2.5.0",
Expand Down
5 changes: 3 additions & 2 deletions src/routes/contact/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const contactGetReadSchema = {
summary: "Read community contact",
description: "Return a single community contact record.",
operationId: "getReadContact",
produces: ["application/json"],
produces: ["application/json", "application/xml"],
params: S.object()
.prop(
"id",
Expand Down Expand Up @@ -164,7 +164,7 @@ const contactGetSearchSchema = {
summary: "Search community contact",
description: "Return community contact records.",
operationId: "getSearchContact",
produces: ["application/json"],
produces: ["application/json", "application/xml"],
query: S.object()
.prop(
"match.type",
Expand Down Expand Up @@ -362,6 +362,7 @@ const contactPostSchema = {
description: "Add a new community contact record.",
operationId: "postContact",
consumes: ["application/json"],
produces: ["application/json", "application/xml"],
body: contactBaseSchema
.additionalProperties(false)
.only(["match", "telecom"]),
Expand Down
35 changes: 33 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const rateLimit = require("fastify-rate-limit");
const sensible = require("fastify-sensible");
const staticPlugin = require("fastify-static");
const swagger = require("fastify-swagger");
const { toXML } = require("jstoxml");
const underPressure = require("under-pressure");
const clean = require("./plugins/clean-object");
const convertDateParamOperator = require("./plugins/convert-date-param-operator");
Expand Down Expand Up @@ -114,13 +115,43 @@ async function plugin(server, config) {
// Catch unsupported Accept header media types
.addHook("preValidation", async (req, res) => {
if (
!["application/json"].includes(
req.accepts().type(["application/json"])
!["application/json", "application/xml"].includes(
req
.accepts()
.type(["application/json", "application/xml"])
)
) {
throw res.notAcceptable();
}
})

// Serialize response
.addHook("onSend", async (req, res, payload) => {
let newPayload;

switch (
req
.accepts()
.type(["application/json", "application/xml"])
) {
case "application/xml":
res.header(
"content-type",
"application/xml; charset=utf-8"
);
newPayload = toXML(JSON.parse(payload), {
header: '<?xml version="1.0" encoding="UTF-8"?>',
});
break;

case "application/json":
default:
newPayload = payload;
break;
}

return newPayload;
})
.register(clean)
.register(convertDateParamOperator)
.register(db, config.database)
Expand Down
26 changes: 25 additions & 1 deletion src/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ const expResHeadersJson = {
"content-type": expect.stringContaining("application/json"),
};

const expResHeadersXml = {
...expResHeaders,
"content-type": expect.stringContaining("application/xml"),
};

describe("Server Deployment", () => {
const connectionTests = [
{
Expand Down Expand Up @@ -251,7 +256,7 @@ describe("Server Deployment", () => {
expect(response.statusCode).toBe(406);
});

test("Should return response if media type in `Accept` request header is supported", async () => {
test("Should return response if media type in `Accept` request header is `application/json`", async () => {
const response = await server.inject({
method: "GET",
url: `/contact/${testId}`,
Expand All @@ -264,6 +269,25 @@ describe("Server Deployment", () => {
expect(response.headers).toEqual(expResHeadersJson);
expect(response.statusCode).not.toBe(406);
});

test("Should return response if media type in `Accept` request header is `application/xml`", async () => {
const response = await server.inject({
method: "GET",
url: `/contact/${testId}`,
headers: {
accept: "application/xml",
authorization: "Bearer testtoken",
},
});

expect(response.payload).toEqual(
expect.stringContaining(
'<?xml version="1.0" encoding="UTF-8"?>'
)
);
expect(response.headers).toEqual(expResHeadersXml);
expect(response.statusCode).not.toBe(406);
});
});
});
});
Expand Down

0 comments on commit 62c6380

Please sign in to comment.