@athenna/http
Version:
The Athenna Http server. Built on top of fastify.
194 lines (193 loc) • 4.82 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);
/**
* Headers that will be defined in all requests.
*/
this.headers = {};
/**
* 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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
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 = {}) {
options.headers = {
...this.headers,
...options.headers
};
return Server.request({
url,
method: 'DELETE',
...options
}).then(res => this.createResponse(res));
}
/**
* Define the authorization access token into all requests.
*
* @example
* ```js
* const token ='Bearer ...'
* const response = await request.authorize(token).get('/users')
*
* response.assertStatusCode(200)
* ```
*/
authorize(accessToken) {
this.headers.authorization = accessToken;
return this;
}
}