Skip to content

Commit

Permalink
fix(handler-kit-azure-func): updated httpAzureFunction
Browse files Browse the repository at this point in the history
Refs: #45
  • Loading branch information
silvicir authored May 9, 2024
1 parent d225939 commit 5a252aa
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-books-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pagopa/handler-kit-azure-func": patch
---

Fixed body response type
54 changes: 53 additions & 1 deletion packages/handler-kit-azure-func/src/__test__/function.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,64 @@ describe("httpAzureFunction", () => {
ctx
);
expect(CtxErrorSpy).toHaveBeenCalled();
expect(response.json()).resolves.toEqual(
await expect(response.json()).resolves.toEqual(
expect.objectContaining({
status: 500,
})
);
});

it("it should return 200 and a string body when the handler returns a response with Content-Type equal to application/entity-statement+jwt and a string body", async () => {
const handler = H.of(() =>
pipe(
RTE.of("jwt"),
RTE.map(
flow(
H.success,
H.withHeader("Content-Type", "application/entity-statement+jwt")
)
),
RTE.orElseW(flow(H.toProblemJson, H.problemJson, RTE.right))
)
);
const message = new HttpRequest({
url: "https://my-request.pagopa.it",
method: "GET",
});
const response = await httpAzureFunction(handler)({})(message, ctx);

expect(response.status).toEqual(200);
expect(response.headers.get("content-type")).toEqual(
"application/entity-statement+jwt"
);
await expect(response.text()).resolves.toEqual("jwt");
});

it("it should return 500 when the handler returns a response with Content-Type equal to application/entity-statement+jwt but an object body", async () => {
const handler = H.of(() =>
pipe(
RTE.of({ jwt: "jwt" }),
RTE.map(
flow(
H.success,
H.withHeader("Content-Type", "application/entity-statement+jwt")
)
),
RTE.orElseW(flow(H.toProblemJson, H.problemJson, RTE.right))
)
);
const message = new HttpRequest({
url: "https://my-request.pagopa.it",
method: "GET",
});
const response = await httpAzureFunction(handler)({})(message, ctx);

expect(response.status).toEqual(500);
expect(response.headers.get("content-type")).toEqual(
"application/problem+json"
);
await expect(response.text()).resolves.toEqual("Internal server error");
});
});

describe("azureFunction", () => {
Expand Down
43 changes: 35 additions & 8 deletions packages/handler-kit-azure-func/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,41 @@ const HttpRequestFromAzure = AzureHttpRequestC.pipe(
"HttpRequestFromAzure"
);

const toAzureHttpResponse = (
res: H.HttpResponse<unknown, H.HttpStatusCode>
): HttpResponse =>
new HttpResponse({
status: res.statusCode,
jsonBody: res.body,
headers: res.headers,
});
const toAzureHttpResponse: ({
statusCode,
body,
headers,
}: H.HttpResponse<unknown, H.HttpStatusCode>) => HttpResponse = ({
statusCode,
body,
headers,
}) => {
// If the content-type is json or problem+json, we use jsonBody which will be JSON-serialized
if (
headers["Content-Type"] === "application/json" ||
headers["Content-Type"] === "application/problem+json"
) {
return new HttpResponse({
status: statusCode,
jsonBody: body,
headers,
});
}
// In other cases, we use the 'body' property
return typeof body === "string"
? new HttpResponse({
status: statusCode,
body,
headers,
})
: new HttpResponse({
status: 500,
body: "Internal server error",
headers: {
"Content-Type": "application/problem+json",
},
});
};

// Prevent HTTP triggered Azure Functions from crashing
// If an handler returns with an error (RTE.left),
Expand Down

0 comments on commit 5a252aa

Please sign in to comment.