From e8c5e683765e71149eff1052692ab6d2edf4fa78 Mon Sep 17 00:00:00 2001 From: Mike de Bock <1261006+mikedebock@users.noreply.github.com> Date: Mon, 18 Dec 2023 11:37:15 +0100 Subject: [PATCH] feat: add basic support for HEAD requests --- src/ctMock.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ctMock.ts b/src/ctMock.ts index 8ec12abb..240e7848 100644 --- a/src/ctMock.ts +++ b/src/ctMock.ts @@ -193,6 +193,35 @@ export class CommercetoolsMock { headers: mapHeaderType(res.headers), }) }), + http.head(`${this.options.apiHost}/*`, async ({ request }) => { + const body = await request.text() + const url = new URL(request.url) + const headers = copyHeaders(request.headers) + + const res = await inject(server) + .get(url.pathname + '?' + url.searchParams.toString()) + .body(body) + .headers({ ...headers, method: 'GET' }) + .end() + + if (res.statusCode === 200) { + const parsedBody = JSON.parse(res.body) + const resultCount = + 'count' in parsedBody + ? parsedBody.count + : Object.keys(parsedBody).length + + return new HttpResponse(null, { + status: resultCount > 0 ? 200 : 404, + headers: mapHeaderType(res.headers), + }) + } + + return new HttpResponse(null, { + status: res.statusCode, + headers: mapHeaderType(res.headers), + }) + }), http.get(`${this.options.apiHost}/*`, async ({ request }) => { const body = await request.text() const url = new URL(request.url)