refactor: replace deprecated request library with got

Closes #35
This commit is contained in:
Jovan Krunić
2020-12-17 10:38:30 +01:00
parent dfc0a3aed9
commit 9a939e9ccd
4 changed files with 202 additions and 167 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 StApps
* Copyright (C) 2018-2020 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
@@ -17,6 +17,8 @@ import {suite, test} from '@testdeck/mocha';
import nock from 'nock';
import {HttpClient} from '../src/http-client';
// TODO: use after each to clean up the nock (then there is no need for numerated resource links)
@suite()
export class HttpClientSpec {
@@ -27,6 +29,10 @@ export class HttpClientSpec {
}).not.to.throw();
}
async after() {
nock.cleanAll();
}
@test
async request() {
const client = new HttpClient();
@@ -51,10 +57,7 @@ export class HttpClientSpec {
.reply(200, 'foo');
const response = await client.request({
body: {
foo: 'bar',
},
url: new URL('http://www.example.com/resource'),
url: new URL('http://www.example.com/resource')
});
expect(response.body).to.be.equal('foo');
@@ -63,17 +66,24 @@ export class HttpClientSpec {
@test
async requestWithError() {
const client = new HttpClient();
let caughtErr;
nock('http://www.example.com')
.get('/resource')
.replyWithError('foo');
return client.request({
body: {
foo: 'bar',
},
url: new URL('http://www.example.com/resource'),
}).should.be.rejected;
try {
await client.request({
body: {
foo: 'bar',
},
url: new URL('http://www.example.com/resource'),
});
} catch (err) {
caughtErr = err;
}
expect(caughtErr).not.to.be.undefined;
}
@test