feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -13,27 +13,23 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from '@testdeck/mocha';
import nock from 'nock';
import {HttpClient} from '../src/http-client.js';
import {HttpClient} from '../src/index.js';
// TODO: use after each to clean up the nock (then there is no need for numerated resource links)
@suite()
export class HttpClientSpec {
@test
async construct() {
describe('HttpClient', function () {
afterEach(function () {
nock.cleanAll();
})
it('should construct', function () {
expect(() => {
return new HttpClient();
}).not.to.throw();
}
});
async after() {
nock.cleanAll();
}
@test
async request() {
it('should request', async function () {
const client = new HttpClient();
nock('http://www.example.com').get('/resource').reply(200, 'foo');
@@ -43,10 +39,9 @@ export class HttpClientSpec {
});
expect(response.body).to.be.equal('foo');
}
});
@test
async requestWithBody() {
it('should request with body', async function () {
const client = new HttpClient();
nock('http://www.example.com').get('/resource').reply(200, 'foo');
@@ -56,12 +51,11 @@ export class HttpClientSpec {
});
expect(response.body).to.be.equal('foo');
}
});
@test
async requestWithError() {
it('should request with error', async function () {
const client = new HttpClient();
let caughtErr;
let caughtError;
nock('http://www.example.com').get('/resource').replyWithError('foo');
@@ -72,15 +66,14 @@ export class HttpClientSpec {
},
url: new URL('http://www.example.com/resource'),
});
} catch (err) {
caughtErr = err;
} catch (error) {
caughtError = error;
}
expect(caughtErr).not.to.be.undefined;
}
expect(caughtError).not.to.be.undefined;
});
@test
async requestWithHeaders() {
it('should request with headers', async function () {
const client = new HttpClient();
nock('http://www.example.com').get('/resource').reply(200, 'foo');
@@ -93,10 +86,9 @@ export class HttpClientSpec {
});
expect(response.body).to.be.equal('foo');
}
});
@test
async requestWithMethodGet() {
it('should request with method GET', async function () {
const client = new HttpClient();
nock('http://www.example.com').get('/resource').reply(200, 'foo');
@@ -107,10 +99,9 @@ export class HttpClientSpec {
});
expect(response.body).to.be.equal('foo');
}
});
@test
async requestWithMethodPost() {
it('should request with method POST', async function () {
const client = new HttpClient();
nock('http://www.example.com').post('/resource').reply(200, 'foo');
@@ -121,5 +112,5 @@ export class HttpClientSpec {
});
expect(response.body).to.be.equal('foo');
}
}
});
});