mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
refactor: split api into api, api-cli & api-plugin
This commit is contained in:
@@ -13,14 +13,18 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {expect} from 'chai';
|
||||
import nock from 'nock';
|
||||
import {Interceptable, MockAgent, setGlobalDispatcher} from 'undici';
|
||||
import {HttpClient} from '../src/index.js';
|
||||
|
||||
// TODO: use after each to clean up the nock (then there is no need for numerated resource links)
|
||||
const mockAgent = new MockAgent();
|
||||
setGlobalDispatcher(mockAgent);
|
||||
|
||||
describe('HttpClient', function () {
|
||||
afterEach(function () {
|
||||
nock.cleanAll();
|
||||
let mockPool: Interceptable;
|
||||
|
||||
beforeEach(function () {
|
||||
mockPool?.close();
|
||||
mockPool = mockAgent.get('http://www.example.com');
|
||||
});
|
||||
|
||||
it('should construct', function () {
|
||||
@@ -32,32 +36,32 @@ describe('HttpClient', function () {
|
||||
it('should request', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com').get('/resource').reply(200, 'foo');
|
||||
mockPool.intercept({path: '/resource', method: 'GET'}).reply(200, {foo: 'bar'});
|
||||
|
||||
const response = await client.request({
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
expect(response.body).to.be.deep.equal({foo: 'bar'});
|
||||
});
|
||||
|
||||
it('should request with body', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com').get('/resource').reply(200, 'foo');
|
||||
mockPool.intercept({path: '/resource', method: 'GET'}).reply(200, {foo: 'foo'});
|
||||
|
||||
const response = await client.request({
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
expect(response.body).to.be.deep.equal({foo: 'foo'});
|
||||
});
|
||||
|
||||
it('should request with error', async function () {
|
||||
const client = new HttpClient();
|
||||
let caughtError;
|
||||
|
||||
nock('http://www.example.com').get('/resource').replyWithError('foo');
|
||||
mockPool.intercept({path: '/resource', method: 'GET'}).replyWithError(new Error('foo'));
|
||||
|
||||
try {
|
||||
await client.request({
|
||||
@@ -76,7 +80,15 @@ describe('HttpClient', function () {
|
||||
it('should request with headers', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com').get('/resource').reply(200, 'foo');
|
||||
mockPool
|
||||
.intercept({
|
||||
path: '/resource',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-StApps-Version': 'foo.bar.foobar',
|
||||
},
|
||||
})
|
||||
.reply(200, {foo: 'bar'});
|
||||
|
||||
const response = await client.request({
|
||||
headers: {
|
||||
@@ -85,32 +97,55 @@ describe('HttpClient', function () {
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
expect(response.body).to.be.deep.equal({foo: 'bar'});
|
||||
});
|
||||
|
||||
it('should request with body', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
mockPool
|
||||
.intercept({
|
||||
path: '/resource',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({bar: 'baz'}),
|
||||
})
|
||||
.reply(200, {foo: 'foo'});
|
||||
|
||||
const response = await client.request({
|
||||
body: {bar: 'baz'},
|
||||
method: 'POST',
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.deep.equal({foo: 'foo'});
|
||||
});
|
||||
|
||||
it('should request with method GET', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com').get('/resource').reply(200, 'foo');
|
||||
mockPool.intercept({path: '/resource', method: 'GET'}).reply(200, {foo: 'foo'});
|
||||
|
||||
const response = await client.request({
|
||||
method: 'GET',
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
expect(response.body).to.be.deep.equal({foo: 'foo'});
|
||||
});
|
||||
|
||||
it('should request with method POST', async function () {
|
||||
const client = new HttpClient();
|
||||
|
||||
nock('http://www.example.com').post('/resource').reply(200, 'foo');
|
||||
mockPool.intercept({path: '/resource', method: 'POST'}).reply(200, {foo: 'foo'});
|
||||
|
||||
const response = await client.request({
|
||||
method: 'POST',
|
||||
url: new URL('http://www.example.com/resource'),
|
||||
});
|
||||
|
||||
expect(response.body).to.be.equal('foo');
|
||||
expect(response.body).to.be.deep.equal({foo: 'foo'});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user