2017-12-01 19:50:09 -05:00
|
|
|
// @flow
|
|
|
|
|
2017-11-27 01:15:09 -05:00
|
|
|
import _ from 'lodash';
|
|
|
|
import smartRequest, {PromiseAssert} from '../../functional/lib/request';
|
2017-12-02 05:31:40 -05:00
|
|
|
import type {IRequestPromise} from '../../functional/lib/types';
|
2017-11-27 01:15:09 -05:00
|
|
|
|
|
|
|
describe('Request Functional', () => {
|
|
|
|
|
2017-12-03 16:23:06 -05:00
|
|
|
const restTest: string = "http://registry.npmjs.org/aaa";
|
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, reject) => {
|
|
|
|
resolve(1);
|
|
|
|
});
|
|
|
|
// $FlowFixMe
|
|
|
|
requestPromise.then((result) => {
|
|
|
|
expect(result).toBe(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('smartRequest Rest', () => {
|
2017-11-27 01:15:09 -05:00
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
// $FlowFixMe
|
|
|
|
smartRequest(options).status(200).then((result)=> {
|
|
|
|
expect(JSON.parse(result).name).toBe('aaa');
|
|
|
|
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'
|
|
|
|
};
|
|
|
|
// $FlowFixMe
|
|
|
|
smartRequest(options).status(404).then((result)=> {
|
|
|
|
// this never is resolved
|
|
|
|
}, function(error) {
|
|
|
|
expect(error.code).toBe('ENOTFOUND');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|