Files
openstapps/test/httpClient.spec.ts
Karl-Philipp Wulfert 4839f941c6 feat: add api
2018-11-29 17:38:57 +01:00

141 lines
3.2 KiB
TypeScript

/*
* Copyright (C) 2018 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.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as chai from 'chai';
import {expect} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies';
import {suite, test} from 'mocha-typescript';
import * as nock from 'nock';
import {HttpClient} from '../src/httpClient';
chai.should();
chai.use(chaiSpies);
chai.use(chaiAsPromised);
const sandbox = chai.spy.sandbox();
@suite()
export class ConnectorClientSpec {
async after() {
sandbox.restore();
}
@test
async construct() {
expect(() => {
return new HttpClient();
}).not.to.throw();
}
@test
async request() {
const client = new HttpClient();
nock('http://www.example.com')
.get('/resource')
.reply(200, 'foo');
const response = await client.request({
url: new URL('http://www.example.com/resource'),
});
expect(response.body).to.be.equal('foo');
}
@test
async requestWithBody() {
const client = new HttpClient();
nock('http://www.example.com')
.get('/resource')
.reply(200, 'foo');
const response = await client.request({
body: {
foo: 'bar',
},
url: new URL('http://www.example.com/resource'),
});
expect(response.body).to.be.equal('foo');
}
@test
async requestWithError() {
const client = new HttpClient();
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;
}
@test
async requestWithHeaders() {
const client = new HttpClient();
nock('http://www.example.com')
.get('/resource')
.reply(200, 'foo');
const response = await client.request({
headers: {
'X-StApps-Version': 'foo.bar.foobar',
},
url: new URL('http://www.example.com/resource'),
});
expect(response.body).to.be.equal('foo');
}
@test
async requestWithMethodGet() {
const client = new HttpClient();
nock('http://www.example.com')
.get('/resource')
.reply(200, 'foo');
const response = await client.request({
method: 'GET',
url: new URL('http://www.example.com/resource'),
});
expect(response.body).to.be.equal('foo');
}
@test
async requestWithMethodPost() {
const client = new HttpClient();
nock('http://www.example.com')
.post('/resource')
.reply(200, 'foo');
const response = await client.request({
method: 'POST',
url: new URL('http://www.example.com/resource'),
});
expect(response.body).to.be.equal('foo');
}
}