Skip to content
Merged
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/http",
"version": "5.34.0",
"version": "5.35.0",
"description": "The Athenna Http server. Built on top of fastify.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
48 changes: 43 additions & 5 deletions src/testing/plugins/request/TestResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { Assert } from '@japa/assert'
import { Macroable } from '@athenna/common'
import { Json, Macroable } from '@athenna/common'
import type { Response } from 'light-my-request'

export class TestResponse extends Macroable {
Expand Down Expand Up @@ -104,7 +104,13 @@ export class TestResponse extends Macroable {
* ```
*/
public assertBodyContainsKey(key: string) {
this.assert.property(this.response.json(), key)
const body = this.response.json()
const value = Json.get(body, key)

this.assert.assert(
value !== undefined,
`The body does not contain the key ${key}`
)
}

/**
Expand All @@ -124,7 +130,10 @@ export class TestResponse extends Macroable {
* ```
*/
public assertBodyNotContainsKey(key: string) {
this.assert.notProperty(this.response.json(), key)
const body = this.response.json()
const value = Json.get(body, key)

this.assert.assert(value === undefined, `The body contains the key ${key}`)
}

/**
Expand All @@ -138,7 +147,22 @@ export class TestResponse extends Macroable {
* ```
*/
public assertBodyContainsAllKeys(keys: string[]) {
this.assert.properties(this.response.json(), keys)
const body = this.response.json()
const seenKeys = new Set()

for (const key of keys) {
const value = Json.get(body, key)

if (value !== undefined) {
seenKeys.add(key)
}
}

if (seenKeys.size !== keys.length) {
return this.assert.fail(
`The body does not contain all keys: ${keys.join(', ')}`
)
}
}

/**
Expand All @@ -158,7 +182,21 @@ export class TestResponse extends Macroable {
* ```
*/
public assertBodyNotContainsAllKeys(keys: string[]) {
this.assert.notAllProperties(this.response.json(), keys)
const body = this.response.json()
const seenKeys = new Set()

for (const key of keys) {
const value = Json.get(body, key)

if (value !== undefined) {
seenKeys.add(key)
}
}

this.assert.assert(
seenKeys.size,
`The body contains keys: ${keys.join(', ')}`
)
}

/**
Expand Down
Loading