Skip to content

Commit

Permalink
feat: add basic support for HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock committed Dec 18, 2023
1 parent 8325080 commit e8c5e68
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/ctMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e8c5e68

Please sign in to comment.