0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/unit/functionalLibs/request.spec.ts

83 lines
2.1 KiB
TypeScript
Raw Normal View History

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