Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 12 additions & 262 deletions lambdas/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lambdas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
"@middy/http-cors": "7.2.1",
"@middy/http-error-handler": "7.2.1",
"@middy/http-security-headers": "7.2.1",
"axios": "1.15.0",
"cookie": "1.1.1",
"dayjs": "1.11.20",
"esbuild": "0.28.0",
"jsonwebtoken": "9.0.3",
"jwks-rsa": "4.0.1",
"pg": "8.20.0",
"titlecase": "1.1.3",
"undici": "8.1.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed, it was determined that it was never being used properly and was redundant

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See 91bacc1

"uuid": "13.0.0",
"zod": "4.3.6"
},
Expand Down
2 changes: 1 addition & 1 deletion lambdas/src/get-results-lambda/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe("init", () => {
initFn: init,
components: [
{
mock: FetchHttpClient as jest.Mock,
mock: FetchHttpClient as unknown as jest.Mock,
times: 1,
},
{
Expand Down
33 changes: 30 additions & 3 deletions lambdas/src/lib/http/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ describe("FetchHttpClient", () => {
expect(result).toEqual({ token: "abc" });
});

it("post preserves URLSearchParams bodies", async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: async () => ({ token: "abc" }),
});

await client.post<{ token: string }>(
"https://example.com/token",
new URLSearchParams({
grant_type: "authorization_code",
code: "auth-code",
}),
{ Accept: "application/json" },
"application/x-www-form-urlencoded",
);

expect(global.fetch).toHaveBeenCalledWith(
"https://example.com/token",
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded",
}),
body: "grant_type=authorization_code&code=auth-code",
}),
);
});

it("post throws HttpError with body on failure", async () => {
(global.fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
Expand Down Expand Up @@ -88,9 +117,7 @@ describe("FetchHttpClient", () => {
text: async () => "bad gateway",
});

await expect(
client.postRaw("https://example.com", "payload"),
).rejects.toEqual(
await expect(client.postRaw("https://example.com", "payload")).rejects.toEqual(
expect.objectContaining<HttpError>({
name: "HttpError",
message: "HTTP POST request failed with status: 502",
Expand Down
36 changes: 30 additions & 6 deletions lambdas/src/lib/http/http-client.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Agent } from "undici";

export interface HttpClient {
get<T>(url: string, headers?: Record<string, string>): Promise<T>;
post<T>(
url: string,
body: any,
body: unknown,
headers?: Record<string, string>,
contentType?: string,
): Promise<T>;
postRaw(
url: string,
body: any,
body: unknown,
headers?: Record<string, string>,
contentType?: string,
): Promise<Response>;
Expand All @@ -26,6 +28,26 @@
}

export class FetchHttpClient implements HttpClient {
private readonly dispatcher?: Agent;

private static serializeBody(body: unknown): string | null | undefined {
if (body == null || typeof body === "string") {
return body;
}

if (body instanceof URLSearchParams) {
return body.toString();

Check warning on line 39 in lambdas/src/lib/http/http-client.ts

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'body' will use Object's default stringification format ('[object Object]') when stringified.

See more on https://sonarcloud.io/project/issues?id=NHSDigital_hometest-service&issues=AZ2MY3QSFzfilvcDxFMw&open=AZ2MY3QSFzfilvcDxFMw&pullRequest=348
}

return JSON.stringify(body);
}

constructor(options?: { rejectUnauthorized?: boolean }) {
if (options?.rejectUnauthorized === false) {
this.dispatcher = new Agent({ connect: { rejectUnauthorized: false } });
}
}

async get<T>(url: string, headers?: Record<string, string>): Promise<T> {
const response = await fetch(url, {
method: "GET",
Expand All @@ -49,7 +71,7 @@

async post<T>(
url: string,
body: any,
body: unknown,
headers?: Record<string, string>,
contentType: string = "application/json",
): Promise<T> {
Expand All @@ -60,7 +82,8 @@
"Content-Type": contentType,
...headers,
},
body: typeof body === "string" ? body : JSON.stringify(body),
body: FetchHttpClient.serializeBody(body),
...(this.dispatcher ? ({ dispatcher: this.dispatcher } as unknown as RequestInit) : {}),
});

if (!response.ok) {
Expand All @@ -77,7 +100,7 @@

async postRaw(
url: string,
body: any,
body: unknown,
headers?: Record<string, string>,
contentType: string = "application/json",
): Promise<Response> {
Expand All @@ -88,7 +111,8 @@
"Content-Type": contentType,
...headers,
},
body: typeof body === "string" ? body : JSON.stringify(body),
body: FetchHttpClient.serializeBody(body),
...(this.dispatcher ? ({ dispatcher: this.dispatcher } as unknown as RequestInit) : {}),
});

if (!response.ok) {
Expand Down
Loading
Loading