2017-11-27 07:15:09 +01:00
|
|
|
import _ from 'lodash';
|
2021-03-14 08:42:46 +01:00
|
|
|
import smartRequest, { PromiseAssert } from '../../lib/request';
|
|
|
|
import { mockServer } from '../__helper/mock';
|
|
|
|
import { HTTP_STATUS } from '../../../src/lib/constants';
|
2019-08-16 21:20:18 +02:00
|
|
|
import { IRequestPromise } from '../../types';
|
|
|
|
import { VerdaccioError } from '@verdaccio/commons-api';
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2018-06-25 08:38:02 +02:00
|
|
|
describe('Request Functional', () => {
|
2019-08-25 19:16:43 +02:00
|
|
|
jest.setTimeout(20000);
|
2018-06-25 08:38:02 +02:00
|
|
|
const mockServerPort = 55547;
|
2019-07-16 08:40:01 +02:00
|
|
|
const restTest = `http://localhost:${55547}/jquery`;
|
2018-06-25 08:38:02 +02:00
|
|
|
let mockRegistry;
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2017-12-03 22:23:06 +01:00
|
|
|
describe('Request Functional', () => {
|
|
|
|
test('PromiseAssert', () => {
|
|
|
|
expect(_.isFunction(smartRequest)).toBeTruthy();
|
|
|
|
});
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2017-12-03 22:23:06 +01:00
|
|
|
test('basic resolve', (done) => {
|
2021-03-14 08:42:46 +01:00
|
|
|
const requestPromise: IRequestPromise = new PromiseAssert((resolve) => {
|
2019-07-16 08:40:01 +02:00
|
|
|
resolve(1);
|
2017-12-03 22:23:06 +01:00
|
|
|
});
|
2019-08-16 21:20:18 +02:00
|
|
|
// @ts-ignore
|
2017-12-03 22:23:06 +01:00
|
|
|
requestPromise.then((result) => {
|
|
|
|
expect(result).toBe(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('smartRequest Rest', () => {
|
2018-06-25 08:38:02 +02:00
|
|
|
beforeAll(async () => {
|
|
|
|
mockRegistry = await mockServer(mockServerPort).init();
|
|
|
|
});
|
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
afterAll(function (done) {
|
2018-06-25 08:38:02 +02:00
|
|
|
mockRegistry[0].stop();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2017-12-03 22:23:06 +01:00
|
|
|
test('basic rest', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: restTest,
|
|
|
|
method: 'GET'
|
|
|
|
};
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2021-03-14 08:42:46 +01:00
|
|
|
smartRequest(options).then((result) => {
|
2017-12-03 22:23:06 +01:00
|
|
|
expect(_.isString(result)).toBeTruthy();
|
|
|
|
done();
|
2021-03-14 08:42:46 +01:00
|
|
|
});
|
2017-12-03 22:23:06 +01:00
|
|
|
});
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2017-12-03 22:23:06 +01:00
|
|
|
describe('smartRequest Status', () => {
|
|
|
|
test('basic check status 200', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: restTest,
|
|
|
|
method: 'GET'
|
|
|
|
};
|
2019-08-16 21:20:18 +02:00
|
|
|
// @ts-ignore
|
2021-03-14 08:42:46 +01:00
|
|
|
smartRequest(options)
|
|
|
|
// @ts-ignore
|
|
|
|
.status(HTTP_STATUS.OK)
|
|
|
|
.then((result) => {
|
|
|
|
expect(JSON.parse(result).name).toBe('jquery');
|
|
|
|
done();
|
|
|
|
});
|
2017-12-03 22:23:06 +01:00
|
|
|
});
|
2017-11-27 07:15:09 +01:00
|
|
|
|
2017-12-03 22:23:06 +01:00
|
|
|
test('basic check status 404', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: 'http://www.google.fake',
|
|
|
|
method: 'GET'
|
|
|
|
};
|
2019-08-16 21:20:18 +02:00
|
|
|
// @ts-ignore
|
2021-03-14 08:42:46 +01:00
|
|
|
smartRequest(options)
|
|
|
|
// @ts-ignore
|
|
|
|
.status(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
// we do not intent to resolve this
|
|
|
|
},
|
|
|
|
(error: VerdaccioError) => {
|
|
|
|
expect(error.code).toBe('ENOTFOUND');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2017-12-03 22:23:06 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|