feat(rest): use undici (#7747)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: ckohen <chaikohen@gmail.com>
This commit is contained in:
Khafra
2022-05-12 16:49:15 -04:00
committed by GitHub
parent 4515a1ea80
commit d1ec8c37ff
19 changed files with 964 additions and 605 deletions

View File

@@ -1,11 +1,33 @@
import nock from 'nock';
import { DefaultRestOptions, REST } from '../src';
import { MockAgent, setGlobalDispatcher } from 'undici';
import { Interceptable } from 'undici/types/mock-interceptor';
import { genPath } from './util';
import { REST } from '../src';
const api = new REST();
nock(`${DefaultRestOptions.api}/v${DefaultRestOptions.version}`).get('/simpleGet').reply(200, { test: true });
let mockAgent: MockAgent;
let mockPool: Interceptable;
beforeEach(() => {
mockAgent = new MockAgent();
mockAgent.disableNetConnect();
setGlobalDispatcher(mockAgent);
mockPool = mockAgent.get('https://discord.com');
});
afterEach(async () => {
await mockAgent.close();
});
test('no token', async () => {
mockPool
.intercept({
path: genPath('/simpleGet'),
method: 'GET',
})
.reply(200, 'Well this is awkward...');
const promise = api.get('/simpleGet');
await expect(promise).rejects.toThrowError('Expected token to be set for this request, but none was present');
await expect(promise).rejects.toBeInstanceOf(Error);