2020-03-03 17:59:19 -05:00
|
|
|
import _ from 'lodash';
|
2021-10-29 10:33:05 -05:00
|
|
|
|
2021-09-25 17:08:00 -05:00
|
|
|
import { HTTP_STATUS, VerdaccioError } from '@verdaccio/core';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
import { mockServer } from '../src/mock';
|
2021-10-29 10:33:05 -05:00
|
|
|
import smartRequest, { PromiseAssert } from '../src/request';
|
2020-03-03 17:59:19 -05:00
|
|
|
import { IRequestPromise } from '../src/types';
|
|
|
|
|
|
|
|
describe('Request Functional', () => {
|
|
|
|
jest.setTimeout(20000);
|
|
|
|
const mockServerPort = 55547;
|
|
|
|
const domainTest = `http://localhost:${55547}`;
|
|
|
|
const restTest = `${domainTest}/jquery`;
|
|
|
|
let mockRegistry;
|
|
|
|
|
|
|
|
describe('Request Functional', () => {
|
|
|
|
test('PromiseAssert', () => {
|
|
|
|
expect(_.isFunction(smartRequest)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('basic resolve', (done) => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const requestPromise: IRequestPromise = new PromiseAssert((resolve) => {
|
2020-03-03 17:59:19 -05:00
|
|
|
resolve(1);
|
|
|
|
});
|
|
|
|
// @ts-ignore
|
|
|
|
requestPromise.then((result) => {
|
|
|
|
expect(result).toBe(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('smartRequest Rest', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
const binPath = require.resolve('verdaccio/bin/verdaccio');
|
|
|
|
mockRegistry = await mockServer(mockServerPort).init(binPath);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
afterAll(function (done) {
|
2020-03-03 17:59:19 -05:00
|
|
|
const [registry, pid] = mockRegistry;
|
|
|
|
registry.stop();
|
|
|
|
console.log(`registry ${pid} has been stopped`);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('basic rest', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: restTest,
|
2020-08-13 16:27:00 -05:00
|
|
|
method: 'GET',
|
2020-03-03 17:59:19 -05:00
|
|
|
};
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
smartRequest(options).then((result) => {
|
2020-03-03 17:59:19 -05:00
|
|
|
expect(_.isString(result)).toBeTruthy();
|
|
|
|
done();
|
2020-08-13 16:27:00 -05:00
|
|
|
});
|
2020-03-03 17:59:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('smartRequest Status', () => {
|
|
|
|
test('basic check status 200', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: restTest,
|
2020-08-13 16:27:00 -05:00
|
|
|
method: 'GET',
|
2020-03-03 17:59:19 -05:00
|
|
|
};
|
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
smartRequest(options)
|
|
|
|
.status(HTTP_STATUS.OK)
|
|
|
|
.then((result) => {
|
|
|
|
expect(JSON.parse(result).name).toBe('jquery');
|
|
|
|
done();
|
|
|
|
});
|
2020-03-03 17:59:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('basic ping status and empty response', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: `${domainTest}/-/ping`,
|
2020-08-13 16:27:00 -05:00
|
|
|
method: 'GET',
|
2020-03-03 17:59:19 -05:00
|
|
|
};
|
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
smartRequest(options)
|
|
|
|
.status(HTTP_STATUS.OK)
|
|
|
|
.then((result) => {
|
|
|
|
expect(JSON.parse(result)).toEqual({});
|
|
|
|
done();
|
|
|
|
});
|
2020-03-03 17:59:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('basic check status 404', (done) => {
|
|
|
|
const options: any = {
|
|
|
|
url: 'http://www.google.fake',
|
2020-08-13 16:27:00 -05:00
|
|
|
method: 'GET',
|
2020-03-03 17:59:19 -05:00
|
|
|
};
|
|
|
|
// @ts-ignore
|
2020-08-13 16:27:00 -05:00
|
|
|
smartRequest(options)
|
|
|
|
.status(HTTP_STATUS.NOT_FOUND)
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
// we do not intent to resolve this
|
|
|
|
},
|
|
|
|
(error: VerdaccioError) => {
|
|
|
|
expect(error.code).toBe('ENOTFOUND');
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
);
|
2020-03-03 17:59:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|