Skip to content

Commit

Permalink
feat: add support for HEAD requests (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedebock authored Dec 18, 2023
1 parent 8325080 commit 1197e45
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/happy-mails-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@labdigital/commercetools-mock': minor
---

Add support for HEAD requests
31 changes: 31 additions & 0 deletions src/ctMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,37 @@ 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)
.end()

if (res.statusCode === 200) {
const parsedBody = JSON.parse(res.body)
// Check if we have a count property (e.g. for query-lookups)
// or if we have a result object (e.g. for single lookups)
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
18 changes: 17 additions & 1 deletion src/services/custom-object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ describe('CustomObject retrieve', () => {
ctMock.clear()
})

test('createget', async () => {
test('exists', async () => {
const response = await supertest(ctMock.app)
.head('/dummy/custom-objects/my-container/my-key')
.send()

expect(response.status).toBe(200)
})

test('non-existent', async () => {
const response = await supertest(ctMock.app)
.head('/dummy/custom-objects/invalid-container/invalid')
.send()

expect(response.status).toBe(404)
})

test('get', async () => {
const response = await supertest(ctMock.app)
.get('/dummy/custom-objects/my-container/my-key')
.send()
Expand Down
20 changes: 20 additions & 0 deletions src/services/customer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ describe('Customer Update Actions', () => {
ctMock.clear()
})

test('exists', async () => {
assert(customer, 'customer not created')

const response = await supertest(ctMock.app)
.head(`/dummy/customers/${customer.id}`)
.send()

expect(response.status).toBe(200)
})

test('non-existent', async () => {
assert(customer, 'customer not created')

const response = await supertest(ctMock.app)
.head(`/dummy/customers/invalid-id`)
.send()

expect(response.status).toBe(404)
})

test('changeEmail', async () => {
assert(customer, 'customer not created')

Expand Down

0 comments on commit 1197e45

Please sign in to comment.