@athenna/http
Version:
The Athenna Http server. Built on top of fastify.
396 lines (395 loc) • 10.6 kB
JavaScript
/**
* @athenna/http
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Assert } from '@japa/assert';
import { Macroable } from '@athenna/common';
export class TestResponse extends Macroable {
constructor(assert, response) {
super();
this.assert = assert;
this.response = response;
}
/**
* Assert the status code of the response.
*
* @example
* ```js
* response.assertStatusCode(200)
* ```
*/
assertStatusCode(statusCode) {
this.assert.deepEqual(this.response.statusCode, statusCode);
}
/**
* Assert the status code is not the same of the response.
*
* @example
* ```js
* response.assertIsNotStatusCode(200)
* ```
*/
assertIsNotStatusCode(statusCode) {
this.assert.notDeepEqual(this.response.statusCode, statusCode);
}
/**
* Assert body (array or object) to contain a subset of the expected value.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyContains({ id: 1 }) // passes
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyContains([{ id: 1 }, { id: 2 }]) // passes
* ```
*/
assertBodyContains(values) {
this.assert.containsSubset(this.response.json(), values);
}
/**
* Assert body (array or object) to not contain a subset of the expected value.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotContains({ id: 1 }) // fails
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyNotContains([{ id: 3 }]) // passes
* ```
*/
assertBodyNotContains(values) {
this.assert.notContainsSubset(this.response.json(), values);
}
/**
* Assert body to contain a key.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyContainsKey('id') // passes
* ```
*/
assertBodyContainsKey(key) {
this.assert.property(this.response.json(), key);
}
/**
* Assert body to not contain a key.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotContainsKey('id') // fails
* ```
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotContainsKey('createdAt') // passes
* ```
*/
assertBodyNotContainsKey(key) {
this.assert.notProperty(this.response.json(), key);
}
/**
* Assert body to contain all keys.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyContainsAllKeys(['id', 'post']) // passes
* ```
*/
assertBodyContainsAllKeys(keys) {
this.assert.properties(this.response.json(), keys);
}
/**
* Assert body to not contain all keys.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotContainsAllKeys(['id']) // fails
* ```
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotContainsAllKeys(['createdAt']) // passes
* ```
*/
assertBodyNotContainsAllKeys(keys) {
this.assert.notAllProperties(this.response.json(), keys);
}
/**
* Assert body (array or object) to be deep equal to the expected value.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyDeepEqual({ id: 1 }) // fails
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
* ```
*/
assertBodyDeepEqual(values) {
this.assert.deepEqual(this.response.json(), values);
}
/**
* Assert body (array or object) to be not deep equal to the expected value.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyNotDeepEqual({ id: 1 }) // passes
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
* ```
*/
assertBodyNotDeepEqual(values) {
this.assert.notDeepEqual(this.response.json(), values);
}
/**
* Assert body to be an array.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyIsArray() // fails
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyIsArray() // passes
* ```
*/
assertBodyIsArray() {
this.assert.isArray(this.response.json());
}
/**
* Assert body to not be an array.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyIsNotArray() // passes
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyIsNotArray() // fails
* ```
*/
assertBodyIsNotArray() {
this.assert.isNotArray(this.response.json());
}
/**
* Assert body to be an object.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyIsObject() // passes
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyIsObject() // fails
* ```
*/
assertBodyIsObject() {
this.assert.isObject(this.response.json());
}
/**
* Assert body to not be an object.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertBodyIsNotObject() // fails
* ```
* @example
* ```js
* const body = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertBodyIsNotObject() // passes
* ```
*/
assertBodyIsNotObject() {
this.assert.isNotObject(this.response.json());
}
/**
* Assert header (array or object) to contain a subset of the expected value.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderContains({ id: 1 }) // passes
* ```
* @example
* ```js
* const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
* ```
*/
assertHeaderContains(values) {
this.assert.containsSubset(this.response.headers, values);
}
/**
* Assert header (array or object) to not contain a subset of the expected value.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderContains({ id: 1 }) // passes
* ```
* @example
* ```js
* const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertHeaderContains([{ id: 1 }, { id: 2 }]) // passes
* ```
*/
assertHeaderNotContains(values) {
this.assert.notContainsSubset(this.response.headers, values);
}
/**
* Assert header (array or object) to be deep equal to the expected value.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderDeepEqual({ id: 1 }) // fails
* ```
* @example
* ```js
* const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertHeaderDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // passes
* ```
*/
assertHeaderDeepEqual(values) {
this.assert.deepEqual(this.response.headers, values);
}
/**
* Assert header (array or object) to be not deep equal to the expected value.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderNotDeepEqual({ id: 1 }) // passes
* ```
* @example
* ```js
* const header = [{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]
*
* response.assertHeaderNotDeepEqual([{ id: 1, name: 'post 1' }, { id: 2, name: 'post 2'}]) // fails
* ```
*/
assertHeaderNotDeepEqual(values) {
this.assert.notDeepEqual(this.response.headers, values);
}
/**
* Assert header to contain a key.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderContainsKey('id') // passes
* ```
*/
assertHeaderContainsKey(key) {
this.assert.property(this.response.headers, key);
}
/**
* Assert header to not contain a key.
*
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertHeaderNotContainsKey('id') // fails
* ```
* @example
* ```js
* const body = { id: 1, name: 'post 1' }
*
* response.assertHeaderNotContainsKey('createdAt') // passes
* ```
*/
assertHeaderNotContainsKey(key) {
this.assert.notProperty(this.response.headers, key);
}
/**
* Assert header to contain all keys.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderContainsAllKeys(['id', 'post']) // passes
* ```
*/
assertHeaderContainsAllKeys(keys) {
this.assert.properties(this.response.headers, keys);
}
/**
* Assert header to not contain all keys.
*
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderNotContainsAllKeys(['id']) // fails
* ```
* @example
* ```js
* const header = { id: 1, name: 'post 1' }
*
* response.assertHeaderNotContainsAllKeys(['createdAt']) // passes
* ```
*/
assertHeaderNotContainsAllKeys(keys) {
this.assert.notAllProperties(this.response.headers, keys);
}
}