@athenna/http
Version:
The Athenna Http server. Built on top of fastify.
119 lines (118 loc) • 3.33 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 { Server } from '#src';
import { Assert } from '@japa/assert';
import { Macroable } from '@athenna/common';
import { TestResponse } from '#src/testing/plugins/request/TestResponse';
export class TestRequest extends Macroable {
constructor() {
super(...arguments);
/**
* Japa assert class instance.
*/
this.assert = new Assert();
}
/**
* Instantiate TestResponse class from API response.
*/
createResponse(response) {
return new TestResponse(this.assert, response);
}
/**
* Make a GET request to the given URL and options.
*
* @example
* ```js
* const response = await request.get('/users')
*
* response.assertStatusCode(200)
* ```
*/
async get(url, options = {}) {
return Server.request({ url, method: 'GET', ...options }).then(res => this.createResponse(res));
}
/**
* Make a HEAD request to the given URL and options.
*
* @example
* ```js
* const response = await request.head('/users')
*
* response.assertStatusCode(200)
* ```
*/
async head(url, options = {}) {
return Server.request({ url, method: 'HEAD', ...options }).then(res => this.createResponse(res));
}
/**
* Make a OPTIONS request to the given URL and options.
*
* @example
* ```js
* const response = await request.options('/users')
*
* response.assertStatusCode(200)
* ```
*/
async options(url, options = {}) {
return Server.request({ url, method: 'OPTIONS', ...options }).then(res => this.createResponse(res));
}
/**
* Make a POST request to the given URL and options.
*
* @example
* ```js
* const response = await request.post('/users')
*
* response.assertStatusCode(200)
* ```
*/
async post(url, options = {}) {
return Server.request({ url, method: 'POST', ...options }).then(res => this.createResponse(res));
}
/**
* Make a PUT request to the given URL and options.
*
* @example
* ```js
* const response = await request.put('/users')
*
* response.assertStatusCode(200)
* ```
*/
async put(url, options = {}) {
return Server.request({ url, method: 'PUT', ...options }).then(res => this.createResponse(res));
}
/**
* Make a PATCH request to the given URL and options.
*
* @example
* ```js
* const response = await request.patch('/users')
*
* response.assertStatusCode(200)
* ```
*/
async patch(url, options = {}) {
return Server.request({ url, method: 'PATCH', ...options }).then(res => this.createResponse(res));
}
/**
* Make a DELETE request to the given URL and options.
*
* @example
* ```js
* const response = await request.delete('/users')
*
* response.assertStatusCode(200)
* ```
*/
async delete(url, options = {}) {
return Server.request({ url, method: 'DELETE', ...options }).then(res => this.createResponse(res));
}
}